From 63f1d9f11d7deec6e5ab1d032236a6b21f79b7d6 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 4 Aug 2013 15:30:43 +0200 Subject: [PATCH] Some refactoring in Notification model and streams --- Changelog.markdown | 1 + diaspy/models.py | 12 ++++++++++-- diaspy/streams.py | 9 ++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index fc71af8..304079c 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -24,6 +24,7 @@ up-to-date than manual and if conflicts appear they should follow the order: Version `0.3.1` (2013-07-12): * __upd__: `diaspy.people.sephandle()` raises `InvalidHandleError` instead of `UserError` +* __upd__: `models.Post()._fetch()` renamed to `_fetchdata()` (because of new `_fetchcomments()` method) * __new__: `models.Comment()` object: wrapper for comments, not to be created manually * __new__: `comments` parameter in `models.Post`: defines whether to fetch post's commets * __new__: `connection.Connection` has new parameter in `__init__()`: it's `schema` diff --git a/diaspy/models.py b/diaspy/models.py index dce4b63..ea88199 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -150,6 +150,7 @@ class Notification(): """ _who_regexp = re.compile(r'/people/[0-9a-z]+" class=\'hovercardable') _when_regexp = re.compile(r'[0-9]{4,4}(-[0-9]{2,2}){2,2} [0-9]{2,2}(:[0-9]{2,2}){2,2} UTC') + _aboutid_regexp = re.compile(r'/posts/[0-9]+') def __init__(self, connection, data): self._connection = connection @@ -166,8 +167,7 @@ class Notification(): def __str__(self): """Returns notification note. """ - print(self.data['note_html']) - string = re.sub('', '', self.data['note_html']) + string = re.sub('', '', self.data['note_html']) string = string.strip().split('\n')[0] while ' ' in string: string = string.replace(' ', ' ') return string @@ -177,6 +177,14 @@ class Notification(): """ return '{0}: {1}'.format(self.when(), str(self)) + def about(self): + """Returns id of post about which the notification is informing. + """ + about = self._aboutid_regexp.search(self.data['note_html']) + if about is None: about = self.who() + else: about = int(about.group(0)[7:]) + return about + def who(self): """Returns list of guids of the users who caused you to get the notification. """ diff --git a/diaspy/streams.py b/diaspy/streams.py index 6be8667..d1834b9 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -14,9 +14,6 @@ class Generic(): """Object representing generic stream. """ _location = 'stream.json' - _stream = [] - # since epoch - max_time = int(time.mktime(time.gmtime())) def __init__(self, connection, location=''): """ @@ -27,6 +24,9 @@ class Generic(): """ self._connection = connection if location: self._location = location + self._stream = [] + # since epoch + self.max_time = int(time.mktime(time.gmtime())) self.fill() def __contains__(self, post): @@ -232,8 +232,7 @@ class Activity(Stream): """ if type(post) == str: self._delid(post) elif type(post) == Post: post.delete() - else: - raise TypeError('this method accepts str or Post types: {0} given') + else: raise TypeError('this method accepts str or Post types: {0} given') self.fill() -- 2.25.1