New Aspect() model and Client() refactored to use it
authorMarek Marecki <triviuss@gmail.com>
Sun, 5 May 2013 22:29:29 +0000 (00:29 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 5 May 2013 22:29:29 +0000 (00:29 +0200)
diaspy/client.py
diaspy/models.py

index 071d9688d17237102f1f6614a885fd67d3fb5f34..85b73269af349abf088513379df4cdb0ba5b409c 100644 (file)
@@ -132,6 +132,16 @@ class Client:
         return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
                 for conversation in mailbox]
 
+    def add_aspect(self, aspect_name, visible=0):
+        """This function adds a new aspect.
+        """
+        diaspy.streams.Aspects(self.connection).add(aspect_name, visible)
+
+    def remove_aspect(self, aspect_id):
+        """This function removes an aspect.
+        """
+        diaspy.streams.Aspects(self.connection).remove(aspect_id)
+
     def add_user_to_aspect(self, user_id, aspect_id):
         """ this function adds a user to an aspect.
 
@@ -141,21 +151,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.get_token(),
-                'aspect_id': aspect_id,
-                'person_id': user_id}
-
-        r = self.connection.post('aspect_memberships.json', data=data)
-
-        if r.status_code != 201:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-        return r.json()
-
-    def add_aspect(self, aspect_name, visible=0):
-        """ This function adds a new aspect.
-        """
-        aspects = diaspy.streams.Aspects(self.connection)
-        aspects.add(aspect_name, visible)
+        return diaspy.models.Aspect(self.connection, aspect_id).addUser(user_id)
 
     def remove_user_from_aspect(self, user_id, aspect_id):
         """ this function removes a user from an aspect.
@@ -166,23 +162,7 @@ class Client:
         :type aspect_id: str
 
         """
-        data = {'authenticity_token': self.connection.get_token(),
-                'aspect_id': aspect_id,
-                'person_id': user_id}
-
-        r = self.connection.delete('aspect_memberships/42.json',
-                                   data=data)
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-
-        return r.json()
-
-    def remove_aspect(self, aspect_id):
-        """ This function removes an aspect.
-        """
-        aspects = diaspy.streams.Aspects(self.connection)
-        aspects.remove(aspect_id)
+        return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id)
 
     def new_conversation(self, contacts, subject, text):
         """Start a new conversation.
index b7e7d76194412c865a9447fc1ed2fb6a21a9ba8a..0a0fe646afa01fc7fda2dfe3b6958dd0eb249d32 100644 (file)
@@ -128,3 +128,44 @@ 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()