From c8438f072451272198e99a4cd6d3554fba25c7d5 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Mon, 10 Jun 2013 23:24:12 +0200 Subject: [PATCH] Further work on `Notificatin()` model; `str()` and `repr()` support, `who()` and `when()` methods implemented --- diaspy/models.py | 31 ++++++++++++++++++++++++++++--- manual/notifications.mdown | 29 +++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/diaspy/models.py b/diaspy/models.py index 996c74f..25ad16d 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -2,6 +2,7 @@ import json +import re """This module is only imported in other diaspy modules and @@ -53,20 +54,44 @@ class Aspect(): class Notification(): """This class represents single 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') + def __init__(self, connection, data): self._connection = connection - self.type = list(data.keys())[0] self.data = data[self.type] self.id = self.data['id'] self.unread = self.data['unread'] def __getitem__(self, key): + """Returns a key from notification data. + """ return self.data[key] - def _refresh(self): - """Refreshes data of the notification. + def __str__(self): + """Returns notification note. + """ + string = re.sub('', '', self.data['note_html']) + string = string.strip().split('\n')[0] + return string + + def __repr__(self): + """Returns notification note with more details. + """ + return '{0}: {1}'.format(self.when(), str(self)) + + def who(self): + """Returns guid of user who caused you to get the notification. + """ + who = self._who_regexp.search(self.data['note_html']).group(0) + return who[8:24] + + def when(self): + """Returns UTC time as found in note_html. """ + when = self._when_regexp.search(self.data['note_html']).group(0) + return when def mark(self, unread=False): """Marks notification to read/unread. diff --git a/manual/notifications.mdown b/manual/notifications.mdown index 63e5a6f..7f0d6e7 100644 --- a/manual/notifications.mdown +++ b/manual/notifications.mdown @@ -1,11 +1,32 @@ +#### `Notifications()` object + In order to get list of notifications use `diaspy.notifications.Notifications()` object. +It support iteration and indexing. + +---- + +#### `Notification()` model Single notification (it should be obvious that it requires object of its own) is located in -`diaspy.models.Notification()`. +`diaspy.models.Notification()`. It has several methods you can use. +##### 1. `who()` ----- +This method will return guid of a user who caused you to get this notification. + +##### 2. `when()` +This method will return UTC time when you get the notification. + +##### 3. `mark()` + +To mark notification as `read` or `unread`. It has one parameter - `unread` which is boolean. + +  + +Also, you can use `str()` and `repr()` on the `Notification()` and you will get nice +string. + +---- -To mark notification as `read` or `unread` use `Notification().mark()` method. -It has only one parameter - `unread` which is boolean. +###### Manual for `diaspy`, written by Marek Marecki -- 2.25.1