import json
+import re
"""This module is only imported in other diaspy modules and
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('</?[a-z]+( *[a-z_-]+=["\'][a-zA-Z0-9/:_#.\- ]*["\'])* */?>', '', 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.
+#### `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