From 9896b77964d9c366157c81204fe841978e378f78 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 7 Jul 2013 15:25:07 +0200 Subject: [PATCH] Refactored `Aspect()` to use `repr()` on `Connection()`, refactored it to use it's own type of exception --- diaspy/errors.py | 6 ++++++ diaspy/models.py | 13 +++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/diaspy/errors.py b/diaspy/errors.py index 74562c3..43888cf 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -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 diff --git a/diaspy/models.py b/diaspy/models.py index eb459f2..b876c21 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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() -- 2.25.1