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
This commit is contained in:
Adi Sieker
2011-03-09 11:00:03 +01:00
parent 54d78ebe46
commit 77e7901e1f
2 changed files with 118 additions and 10 deletions
+61 -8
View File
@@ -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
+57 -2
View File
@@ -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 '<group %s>' % (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):