Fix removing users from aspects, fixes #13
authorMarek Marecki <marekjm@ozro.pw>
Sun, 23 Apr 2017 13:45:07 +0000 (15:45 +0200)
committerMarek Marecki <marekjm@ozro.pw>
Sun, 23 Apr 2017 13:45:07 +0000 (15:45 +0200)
diaspy/errors.py
diaspy/models.py

index 3dbf9e75b21cd2333dd3478aae91fc553c63e44d..443249b762302bb28b959f6e766167d99c11472f 100644 (file)
@@ -81,6 +81,9 @@ class AspectError(DiaspyError):
     """
     pass
 
+class UserIsNotMemberOfAspect(AspectError):
+    pass
+
 
 class PostError(DiaspyError):
     """Exception raised when something related to posts goes wrong.
index 5f1c5383c8a80e007f79d4bf658772d7d7e6c6ac..e908097703323aec132930b23e6124f0920ffe0c 100644 (file)
@@ -129,19 +129,26 @@ class Aspect():
             raise errors.AspectError('wrong status code: {0}'.format(request.status_code))
         return request.json()
 
-    def removeUser(self, user_id):
+    def removeUser(self, user):
         """Remove user from current aspect.
 
         :param user_id: user to remove from aspect
         :type user: int
         """
-        data = {'authenticity_token': repr(self._connection),
-                'aspect_id': self.id,
-                'person_id': user_id}
-        request = self._connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data)
+        membership_id = None
+        for each in user.aspectMemberships():
+            print(self.id, each)
+            if each.get('aspect', {}).get('id') == self.id:
+                membership_id = each.get('id')
+
+        if membership_id is None:
+            raise errors.UserIsNotMemberOfAspect(user, self)
+
+        request = self._connection.delete('aspect_memberships/{0}'.format(membership_id))
 
         if request.status_code != 200:
             raise errors.AspectError('cannot remove user from aspect: {0}'.format(request.status_code))
+
         return request.json()