From 57f6c7c190d60ebd7b8203aea3d3ef92ac789824 Mon Sep 17 00:00:00 2001 From: Adi Sieker Date: Sat, 16 Apr 2011 22:03:34 +0200 Subject: [PATCH] split live api, LiveTopic model, sync fixes * The live api is now split into two methods. One that fetches the data and deserializes the json the second that creates the model instances. These two methods are wrapped by the live method. The split is useful when working with multiple threads. One worker thread that fetches the data and the main thread that creates the instances. * added LiveTopic model since the topic data in the live api doesn't contain all the data that a Topic contains. * makes sure that the _synced attribute is set when calling sync directly. --- convore/api.py | 4 +++- convore/core.py | 54 +++++++++++++++++++++++------------------------ convore/models.py | 35 +++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/convore/api.py b/convore/api.py index abaf564..9731155 100644 --- a/convore/api.py +++ b/convore/api.py @@ -114,6 +114,7 @@ class Groups(SyncedList): for _group in deserialize(r.content)['groups']: group = self._create_group_from_api(_group) self.data.append(group) + self._synced = True def _create_group_from_api(self, _group): group = models.Group() @@ -152,6 +153,7 @@ class Topics(SyncedList): for _topic in deserialize(r.content)['topics']: topic = self._create_topic_from_api(_topic) self.data.append(topic) + self._synced = True def _create_topic_from_api(self, _topic): topic = models.Topic() @@ -194,7 +196,7 @@ class Messages(SyncedList): message.topic = self.topic message.unread = idx > msg_count - unread_count self.data.append(message) - + self._synced = True def create(self, message): params = {'topic_id': self.topic.id, 'message': message} diff --git a/convore/core.py b/convore/core.py index 43788b4..b9f618f 100644 --- a/convore/core.py +++ b/convore/core.py @@ -61,32 +61,32 @@ class Convore(object): return deserialize(r.content)['messages'] def live(self, cursor=None): + messages = self.fetch_live_data(cursor) + (live_messages, next_cursor) = self.import_live_from_api(messages) + + return (live_messages, next_cursor) + + def import_live_from_api(self, messages): + live_messages = list() + next_cursor = None + for data in messages: + try: + class_ = LIVE_TYPES[data['kind']] + except KeyError: + continue + + message = class_() + message.import_from_api(data) + + if data['kind'] == 'topic': + message.group_id = data['group'] + elif data['kind'] == 'message': + message.group_id = data['group'] + message.topic = models.LiveTopic() + message.topic.import_from_api(data['topic']) + + live_messages.append({'kind': data['kind'], + 'message': message}) + next_cursor = data['_id'] - try: - next_cursor = None - live_messages = list() - messages = self.fetch_live_data(cursor) - for data in messages: - try: - class_ = LIVE_TYPES[data['kind']] - except KeyError: - continue - - message = class_() - message.import_from_api(data) - - if data['kind'] == 'read': - group = self.groups.get(data['group_id']) - message.topic = group.topics.get(data['topic_id']) - elif data['kind'] == 'topic': - message.group = self.groups.get(data['group']) - elif data['kind'] == 'message': - group = self.groups.get(data['group']) - message.topic = group.topics.get(data['topic']['id']) - - live_messages.append({'kind': data['kind'], - 'message': message}) - next_cursor = data['_id'] - except KeyError: - pass return (live_messages, next_cursor) diff --git a/convore/models.py b/convore/models.py index ad3fa27..fdc5147 100644 --- a/convore/models.py +++ b/convore/models.py @@ -88,21 +88,28 @@ class Group(object): return '' % (self.slug) def mark_topic_read(self, read): - if read.topic.id not in self.topics: + if not self.topics: + return + if read.topic_id not in self.topics: return self.unread = self.unread - read.unread_count - self.topics[read.topic.id].mark_read() + self.topics[read.topic_id].mark_read() def add_message(self, message): self.unread = self.unread + 1 + #If there are no topics we haven't synced this group yet. + #So there is nothing else todo. if not self.topics: return + #If the topic of the messages isn't in our topics + #resync the group topics. if message.topic.id not in self.topics: - self.topics.insert(0, message.topic) + self.topics.sync() + #we now assume the topic is there. topic = self.topics[message.topic.id] if topic.messages: @@ -153,7 +160,17 @@ class Topic(object): message.unread = True self.messages.append(message) self.unread = self.unread + 1 - self.group.unread = self.group.unread + 1 + +class LiveTopic(object): + def __init__(self): + self.id = None + self.name = None + self.url = None + + def import_from_api(self, data): + self.id = data.get('id', None) + self.name = data.get('name', None) + self.url = data.get('url', None) class Message(object): """Convore message object""" @@ -172,8 +189,7 @@ class Message(object): self.id = data.get('id', None) self.message = data.get('message', None) self.date_created = datetime.utcfromtimestamp( - data.get('date_created', None) - ) + data.get('date_created', None)) self.user.import_from_api(data.get('user', None)) @@ -196,7 +212,8 @@ class Category(object): class Read(object): def __init__(self): - self.topic = None + self.group_id = None + self.topic_id = None self.when = None self.user = None self.unread_count = 0 @@ -208,7 +225,9 @@ class Read(object): ) self.user = User() self.user.import_from_api(data.get('user', None)) - self.unread_count = data.get('unread_count') + self.unread_count = data.get('unread_count', 0) + self.group_id = data.get('group_id', None) + self.topic_id = data.get('topic_id', None) class Login(object): def __init__(self):