From: Marek Marecki Date: Sun, 5 May 2013 22:29:29 +0000 (+0200) Subject: New Aspect() model and Client() refactored to use it X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=90bf7079c367db25c5b54c0d7539b9a7abb0b91a;p=diaspy.git New Aspect() model and Client() refactored to use it --- diff --git a/diaspy/client.py b/diaspy/client.py index 071d968..85b7326 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -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. diff --git a/diaspy/models.py b/diaspy/models.py index b7e7d76..0a0fe64 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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()