Notifications can now be marked as `read` or `unread`
authorMarek Marecki <triviuss@gmail.com>
Sun, 9 Jun 2013 20:08:25 +0000 (22:08 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 9 Jun 2013 20:08:25 +0000 (22:08 +0200)
diaspy/client.py
diaspy/connection.py
diaspy/models.py
diaspy/notifications.py [new file with mode: 0644]
diaspy/streams.py
manual/notifications.mdown [new file with mode: 0644]

index e5c6ddc16a682479aed6747e6fa76581c5ec81a5..ce07990a70cf33be9fbcfaf94e89de21b47d9a44 100644 (file)
@@ -1,6 +1,7 @@
 import diaspy.models
 import diaspy.streams
 import diaspy.connection
+from diaspy import notifications
 
 
 class Client:
@@ -47,8 +48,7 @@ class Client:
 
         :returns: diaspy.streams.Activity
         """
-        activity = diaspy.streams.Activity(self.connection, 'activity.json')
-        return activity
+        return diaspy.streams.Activity(self.connection, 'activity.json')
 
     def get_stream(self):
         """This functions returns stream.
@@ -93,13 +93,7 @@ class Client:
 
         :returns: list -- list of json formatted notifications
         """
-        r = self.connection.get('notifications.json')
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-
-        notifications = r.json()
-        return notifications
+        return notifications.Notifications(self.connection)
 
     def get_mailbox(self):
         """This functions returns a list of messages found in the conversation.
index 5298565f90f77b6f8c80c7f339f75de2ba029766..e139afd4dfd13173504b173355ca61b78ce7ccdf 100644 (file)
@@ -5,6 +5,10 @@ import requests
 import json
 
 
+"""This module abstracts connection to pod.
+"""
+
+
 class LoginError(Exception):
     pass
 
@@ -61,6 +65,14 @@ class Connection():
         request = self.session.post(string, data, headers=headers, params=params)
         return request
 
+    def put(self, string, data=None, headers={}, params={}):
+        """This method PUTs to session.
+        """
+        string = '{0}/{1}'.format(self.pod, string)
+        if data is not None: request = self.session.put(string, data, headers=headers, params=params)
+        else: request = self.session.put(string, headers=headers, params=params)
+        return request
+
     def delete(self, string, data, headers={}):
         """This method lets you send delete request to session.
         Performs additional checks if needed.
index 28a16d62839000bfe957fe987c0148a81fe53d15..7961479aaf7310ab113477c7bd6ec3514d8e030f 100644 (file)
@@ -1,6 +1,14 @@
 #!/usr/bin/env python3
 
 
+import json
+
+
+"""This module is only imported in other diaspy modules and
+MUST NOT import anything.
+"""
+
+
 class Aspect():
     """This class represents an aspect.
     """
@@ -42,6 +50,35 @@ class Aspect():
         return request.json()
 
 
+class Notification():
+    """This class represents single notification.
+    """
+    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):
+        return self.data[key]
+
+    def mark(self, unread=False):
+        """Marks notification to read/unread.
+        Marks notification to read if `unread` is False.
+        Marks notification to unread if `unread` is True.
+
+        :param unread: which state set for notification
+        :type unread: bool
+        """
+        headers = {'x-csrf-token': self._connection.get_token()}
+        params = {'set_unread': json.dumps(unread)}
+        print(json.dumps(False))
+        self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers)
+        self.data['unread'] = unread
+
+
 class Post():
     """This class represents a post.
 
diff --git a/diaspy/notifications.py b/diaspy/notifications.py
new file mode 100644 (file)
index 0000000..55b7035
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+
+import time
+from diaspy.models import Notification
+
+
+"""This module abstracts notifications.
+"""
+
+
+class Notifications():
+    """This class represents notifications of a user.
+    """
+    def __init__(self, connection):
+        self._connection = connection
+        self._notifications = self.get()
+
+    def __iter__(self):
+        return iter(self._notifications)
+
+    def last(self):
+        """Returns list of most recent notifications.
+        """
+        params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)}
+        headers = {'x-csrf-token': self._connection.get_token()}
+
+        request = self._connection.get('notifications.json', headers=headers, params=params)
+
+        if request.status_code != 200:
+            raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
+        return [Notification(self._connection, n) for n in request.json()]
+
+
+    def get(self, per_page=5, page=1):
+        """Returns list of notifications.
+        """
+        params = {'per_page': per_page, 'page': page}
+        headers = {'x-csrf-token': self._connection.get_token()}
+
+        request = self._connection.get('notifications.json', headers=headers, params=params)
+        
+        if request.status_code != 200:
+            raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
+
+        notifications = request.json()
+        return [Notification(self._connection, n) for n in notifications]
index e1cae151127066ebf383f400c985b89435b44fc7..fa19d97a11859a231063866eee5dcead7a52c79f 100644 (file)
@@ -32,8 +32,6 @@ class Generic():
     def __contains__(self, post):
         """Returns True if stream contains given post.
         """
-        if type(post) is not Post:
-            raise TypeError('stream can contain only posts: checked for {0}'.format(type(post)))
         return post in self._stream
 
     def __iter__(self):
@@ -98,12 +96,13 @@ class Generic():
         """
         self._stream = self._obtain()
 
-    def more(self):
+    def more(self, max_time=0):
         """Tries to download more (older ones) Posts from Stream.
         """
-        self.max_time -= 3000000
+        if not max_time: max_time = self.max_time - 3000000
+        self.max_time = max_time
         params = {'max_time': self.max_time}
-        request = self._connection.get('{0}', params=params)
+        request = self._connection.get(self._location, params=params)
         if request.status_code != 200:
             raise Exception('wrong status code: {0}'.format(request.status_code))
 
diff --git a/manual/notifications.mdown b/manual/notifications.mdown
new file mode 100644 (file)
index 0000000..63e5a6f
--- /dev/null
@@ -0,0 +1,11 @@
+In order to get list of notifications use `diaspy.notifications.Notifications()` object. 
+
+Single notification (it should be obvious that it requires object of its own) is located in 
+`diaspy.models.Notification()`.
+
+
+----
+
+
+To mark notification as `read` or `unread` use `Notification().mark()` method. 
+It has only one parameter - `unread` which is boolean.