From 57ceb864703faa46e2eb727a94121b15b4dc1c34 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 23 Apr 2017 15:45:07 +0200 Subject: [PATCH] Fix removing users from aspects, fixes #13 --- diaspy/errors.py | 3 +++ diaspy/models.py | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/diaspy/errors.py b/diaspy/errors.py index 3dbf9e7..443249b 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -81,6 +81,9 @@ class AspectError(DiaspyError): """ pass +class UserIsNotMemberOfAspect(AspectError): + pass + class PostError(DiaspyError): """Exception raised when something related to posts goes wrong. diff --git a/diaspy/models.py b/diaspy/models.py index 5f1c538..e908097 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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() -- 2.25.1