Stuff done
[diaspy.git] / diaspy / models.py
index bedd1ee2207485f44957f6e40fbe123ff444299a..72041a52416ae272ca02b228cd6323b5c975962a 100644 (file)
@@ -1,7 +1,117 @@
 #!/usr/bin/env python3
 
 
-class Post:
+import json
+import re
+
+
+"""This module is only imported in other diaspy modules and
+MUST NOT import anything.
+"""
+
+
+class Aspect():
+    """This class represents an aspect.
+    """
+    def __init__(self, connection, id=-1):
+        self._connection = connection
+        self.id = id
+        self.name = ''
+
+    def getUsers(self):
+        """Returns list of users who are listed in this aspect.
+        """
+        return []
+
+    def addUser(self, user_id):
+        """Add user to current aspect.
+
+        :param user_id: user to add to aspect
+        :type user: int
+        """
+        data = {'authenticity_token': self._connection.get_token(),
+                'aspect_id': self.id,
+                'person_id': user_id}
+
+        request = self._connection.post('aspect_memberships.json', data=data)
+
+        if request.status_code != 201:
+            raise Exception('wrong status code: {0}'.format(request.status_code))
+        return request.json()
+
+    def removeUser(self, user_id):
+        """Remove user from current aspect.
+
+        :param user_id: user to remove from aspect
+        :type user: int
+        """
+        data = {'authenticity_token': self._connection.get_token(),
+                'aspect_id': self.id,
+                'person_id': user_id}
+
+        request = self.connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data)
+
+        if request.status_code != 200:
+            raise Exception('wrong status code: {0}'.format(request.status_code))
+        return request.json()
+
+
+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 __str__(self):
+        """Returns notification note.
+        """
+        string = re.sub('</?[a-z]+( *[a-z_-]+=["\'][\w():.,!?#/\- ]*["\'])* */?>', '', self.data['note_html'])
+        string = string.strip().split('\n')[0]
+        while '  ' in string: string = string.replace('  ', ' ')
+        return string
+
+    def __repr__(self):
+        """Returns notification note with more details.
+        """
+        return '{0}: {1}'.format(self.when(), str(self))
+
+    def who(self):
+        """Returns list of guids of the users who caused you to get the notification.
+        """
+        return [who[8:24] for who in self._who_regexp.findall(self.data['note_html'])]
+
+    def when(self):
+        """Returns UTC time as found in note_html.
+        """
+        return self._when_regexp.search(self.data['note_html']).group(0)
+
+    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)}
+        self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers)
+        self.data['unread'] = unread
+
+
+class Post():
     """This class represents a post.
 
     .. note::
@@ -136,44 +246,3 @@ class Post:
                                     headers={'accept': 'application/json'})
         if r.status_code != 204:
             raise Exception('{0}: Post could not be deleted'.format(r.status_code))
-
-
-class Aspect():
-    """This class represents an aspect.
-    """
-    def __init__(self, connection, id=-1):
-        self._connection = connection
-        self.id = id
-        self.name = ''
-
-    def addUser(self, user_id):
-        """Add user to current aspect.
-
-        :param user_id: user to add to aspect
-        :type user: int
-        """
-        data = {'authenticity_token': self._connection.get_token(),
-                'aspect_id': self.id,
-                'person_id': user_id}
-
-        request = self._connection.post('aspect_memberships.json', data=data)
-
-        if request.status_code != 201:
-            raise Exception('wrong status code: {0}'.format(request.status_code))
-        return request.json()
-
-    def removeUser(self, user_id):
-        """Remove user from current aspect.
-
-        :param user_id: user to remove from aspect
-        :type user: int
-        """
-        data = {'authenticity_token': self._connection.get_token(),
-                'aspect_id': self.id,
-                'person_id': user_id}
-
-        request = self.connection.delete('aspect_memberships/42.json', data=data)
-
-        if request.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(request.status_code))
-        return request.json()