Refactored `Aspect()` to use `repr()` on `Connection()`, refactored it
authorMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 13:25:07 +0000 (15:25 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 13:25:07 +0000 (15:25 +0200)
to use it's own type of exception

diaspy/errors.py
diaspy/models.py

index 74562c39612140e628036787dd578f4dc9c93c1d..43888cfe90e08e57deee3d3961543cca4633ecd4 100644 (file)
@@ -28,6 +28,12 @@ class ConversationError(DiaspyError):
     pass
 
 
+class AspectError(DiaspyError):
+    """Exception raised when something related to aspects goes wrong.
+    """
+    pass
+
+
 def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError):
     """This method tries to decides how to react
     to a response code passed to it. If it's an
index eb459f2d1140c58eb3b69e81e8f3bb2d9d7e4acf..b876c212f5276b1da050a8740267d0aa947b6e98 100644 (file)
@@ -4,6 +4,8 @@
 import json
 import re
 
+from diaspy import errors
+
 
 """This module is only imported in other diaspy modules and
 MUST NOT import anything.
@@ -113,18 +115,18 @@ class Aspect():
         :type user_id: int
         :returns: JSON from request
         """
-        data = {'authenticity_token': self._connection.get_token(),
+        data = {'authenticity_token': repr(self._connection),
                 'aspect_id': self.id,
                 'person_id': user_id}
 
         request = self._connection.post('aspect_memberships.json', data=data)
 
         if request.status_code == 400:
-            raise Exception('duplicate record, user already exists in aspect: {0}'.format(request.status_code))
+            raise errors.AspectError('duplicate record, user already exists in aspect: {0}'.format(request.status_code))
         elif request.status_code == 404:
-            raise Exception('user not found from this pod: {0}'.format(request.status_code))
+            raise errors.AspectError('user not found from this pod: {0}'.format(request.status_code))
         elif request.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(request.status_code))
+            raise errors.AspectError('wrong status code: {0}'.format(request.status_code))
         return request.json()
 
     def removeUser(self, user_id):
@@ -136,11 +138,10 @@ class Aspect():
         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))
+            raise errors.AspectError('cannot remove user from aspect: {0}'.format(request.status_code))
         return request.json()