From 77e7901e1fa4972db21a21aa654ccbf35ea143d8 Mon Sep 17 00:00:00 2001 From: Adi Sieker Date: Wed, 9 Mar 2011 11:00:03 +0100 Subject: [PATCH] implemented topics and messages. groups now know their topics and topics know their messages. the relevant convore apis are: - /groups/:group_id/topics.json - /topics/:topic_id/messages.json --- convore/api.py | 69 +++++++++++++++++++++++++++++++++++++++++------ convore/models.py | 59 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 10 deletions(-) diff --git a/convore/api.py b/convore/api.py index 16c8861..ef49ad5 100644 --- a/convore/api.py +++ b/convore/api.py @@ -41,7 +41,7 @@ def get(*path, **kwargs): """ Accepts optional error parameter, which will be passed in the event of a non-401 HTTP error. - + api.get('groups') api.get('groups', 'id') api.get('accounts', 'verify') @@ -57,7 +57,7 @@ def get(*path, **kwargs): def post(params, *path): - + url = '%s%s%s' % (API_URL, '/'.join(map(str, path)), '.json') r = requests.post(url, params=params, auth=auth) return _safe_response(r) @@ -78,7 +78,7 @@ class APIError(RuntimeError): def login(username, password): """Configured API Credentials""" global auth - + auth = (username, password) # print requests.auth_manager.__dict__ @@ -120,15 +120,68 @@ class Groups(SyncedList): group = models.Group() group.import_from_api(_group) group.joined = True + group.topics = Topics(group) self.data.append(group) class Topics(SyncedList): - __data_keys__ = [] + __data_keys__ = ['id', 'slug'] - def __init__(self): - pass + def __init__(self, group): + super(Topics, self).__init__() + self.group = group + + def list(self): + return self.data + + def get(self, key): + r = get('topics', key) + + topic = models.Topic() + topic.import_from_api(deserialize(r.content)['topic']) + return topic + + def sync(self): + + self.data = [] + + r = get('groups', self.group.id, 'topics') + for _topic in deserialize(r.content)['topics']: + + topic = models.Topic() + topic.import_from_api(_topic) + topic.messages = Messages(topic) + topic.group = self.group + self.data.append(topic) + +class Messages(SyncedList): + + __data_keys__ = ['id', 'slug'] + + def __init__(self, topic): + super(Messages, self).__init__() + self.topic = topic + + def list(self): + return self.data + + def get(self, key): + r = get('topics', key) + + topic = models.Topic() + topic.import_from_api(deserialize(r.content)['topic']) + return topic + + def sync(self): + + self.data = [] + + r = get('topics', self.topic.id, 'messages') + for _message in deserialize(r.content)['messages']: + + message = models.Message() + message.import_from_api(_message) + message.topic = self.topic + self.data.append(message) - def import_from_api(self, d): - print d diff --git a/convore/models.py b/convore/models.py index 8e55347..3ec00df 100644 --- a/convore/models.py +++ b/convore/models.py @@ -3,7 +3,7 @@ convore.models ~~~~~~~~~~~~~~ - This module implements the internal models for + This module implements the internal models for Convore API object storage. :copyright: (c) 2011 by Kenneth Reitz. @@ -54,7 +54,7 @@ class Group(object): self.id = None self.joined = False - + def import_from_api(self, d): """Constructs Group from Deserialized API Response.""" @@ -88,6 +88,61 @@ class Group(object): def __repr__(self): return '' % (self.slug) +class Topic(object): + """Convore topic object""" + + def __init__(self): + self.id = None + self.name = None + self.slug = None + self.url = None + self.message_count = None + self.unread = None + self.date_created = None + self.date_latest_message = None + self.creator = None + + + def import_from_api(self, data): + self.creator = User() + + self.id = data.get('id', None) + self.name = data.get('name', None) + self.slug = data.get('slug', None) + self.url = data.get('url', None) + self.message_count = data.get('message_count', None) + self.unread = data.get('unread', None) + self.date_created = datetime.utcfromtimestamp( + data.get('date_created', None) + ) + self.date_latest_message = datetime.utcfromtimestamp( + data.get('date_latest_message', None) + ) + self.creator.import_from_api(data.get('creator', None)) + + +class Message(object): + """Convore message object""" + + def __init__(self): + self.id = None + self.message = None + self.date_created = None + self.user = None + + + def import_from_api(self, data): + self.user = User() + + self.id = data.get('id', None) + self.message = data.get('message', None) + self.date_created = datetime.utcfromtimestamp( + data.get('date_created', None) + ) + self.user.import_from_api(data.get('user', None)) + + + class Category(object): def __init__(self):