X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;ds=sidebyside;f=diaspy%2Fmodels.py;h=e908097703323aec132930b23e6124f0920ffe0c;hb=57ceb864703faa46e2eb727a94121b15b4dc1c34;hp=9760a9cc3dbd70222ed8201c9915e4bb5883f795;hpb=f50cbea31f915755413c7b2b8f30def77489ed7f;p=diaspy.git diff --git a/diaspy/models.py b/diaspy/models.py index 9760a9c..e908097 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -33,7 +33,7 @@ class Aspect(): """Finds name for aspect. """ name = None - aspects = self._connection.getUserInfo()['aspects'] + aspects = self._connection.getUserData()['aspects'] for a in aspects: if a['id'] == self.id: name = a['name'] @@ -44,7 +44,7 @@ class Aspect(): """Finds id for aspect. """ id = None - aspects = self._connection.getUserInfo()['aspects'] + aspects = self._connection.getUserData()['aspects'] for a in aspects: if a['name'] == self.name: id = a['id'] @@ -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() @@ -156,19 +163,19 @@ class Notification(): def __init__(self, connection, data): self._connection = connection self.type = list(data.keys())[0] - self.data = data[self.type] - self.id = self.data['id'] - self.unread = self.data['unread'] + self._data = data[self.type] + self.id = self._data['id'] + self.unread = self._data['unread'] def __getitem__(self, key): """Returns a key from notification data. """ - return self.data[key] + return self._data[key] def __str__(self): """Returns notification note. """ - string = re.sub(self._htmltag_regexp, '', self.data['note_html']) + string = re.sub(self._htmltag_regexp, '', self._data['note_html']) string = string.strip().split('\n')[0] while ' ' in string: string = string.replace(' ', ' ') return string @@ -182,7 +189,7 @@ class Notification(): """Returns id of post about which the notification is informing OR: If the id is None it means that it's about user so .who() is called. """ - about = self._aboutid_regexp.search(self.data['note_html']) + about = self._aboutid_regexp.search(self._data['note_html']) if about is None: about = self.who() else: about = int(about.group(0)[7:]) return about @@ -190,12 +197,12 @@ class Notification(): def who(self): """Returns list of guids of the users who caused you to get the notification. """ - return [who[8:24] for who in self._who_regexp.findall(self.data['note_html'])] + return [who[8:24] for who in self._who_regexp.findall(self._data['note_html'])] def when(self): """Returns UTC time as found in note_html. """ - return self._when_regexp.search(self.data['note_html']).group(0) + return self._data['created_at'] def mark(self, unread=False): """Marks notification to read/unread. @@ -208,7 +215,7 @@ class Notification(): headers = {'x-csrf-token': repr(self._connection)} params = {'set_unread': json.dumps(unread)} self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers) - self.data['unread'] = unread + self._data['unread'] = unread class Conversation(): @@ -226,7 +233,7 @@ class Conversation(): """ self._connection = connection self.id = id - self.data = {} + self._data = {} if fetch: self._fetch() def _fetch(self): @@ -234,7 +241,7 @@ class Conversation(): """ request = self._connection.get('conversations/{}.json'.format(self.id)) if request.status_code == 200: - self.data = request.json()['conversation'] + self._data = request.json()['conversation'] else: raise errors.ConversationError('cannot download conversation data: {0}'.format(request.status_code)) @@ -274,7 +281,7 @@ class Conversation(): def get_subject(self): """Returns the subject of this conversation """ - return self.data['subject'] + return self._data['subject'] class Comment(): @@ -285,12 +292,12 @@ class Comment(): by `Post()` objects. """ def __init__(self, data): - self.data = data + self._data = data def __str__(self): """Returns comment's text. """ - return self.data['text'] + return self._data['text'] def __repr__(self): """Returns comments text and author. @@ -301,12 +308,12 @@ class Comment(): def when(self): """Returns time when the comment had been created. """ - return self.data['created_at'] + return self._data['created_at'] def author(self, key='name'): """Returns author of the comment. """ - return self.data['author'][key] + return self._data['author'][key] class Post(): @@ -328,34 +335,34 @@ class Post(): :param comments: defines whether to fetch post's comments or not (if True also data will be fetched) :type comments: bool """ - if not (guid or id): raise TypeError('guid and/or id missing') + if not (guid or id): raise TypeError('neither guid nor id was provided') self._connection = connection self.id = id self.guid = guid - self.data = {} + self._data = {} self.comments = [] if fetch: self._fetchdata() if comments: - if not self.data: self._fetchdata() + if not self._data: self._fetchdata() self._fetchcomments() def __repr__(self): """Returns string containing more information then str(). """ - return '{0} ({1}): {2}'.format(self.data['author']['name'], self.data['author']['guid'], self.data['text']) + return '{0} ({1}): {2}'.format(self._data['author']['name'], self._data['author']['guid'], self._data['text']) def __str__(self): """Returns text of a post. """ - return self.data['text'] + return self._data['text'] def __getitem__(self, key): - return self.data[key] + return self._data[key] def __dict__(self): """Returns dictionary of posts data. """ - return self.data + return self._data def _fetchdata(self): """This function retrieves data of the post. @@ -368,14 +375,15 @@ class Post(): if request.status_code != 200: raise errors.PostError('{0}: could not fetch data for post: {1}'.format(request.status_code, id)) else: - self.data = request.json() + self._data = request.json() return self['guid'] def _fetchcomments(self): """Retreives comments for this post. + Retrieving comments via GUID will result in 404 error. + DIASPORA* does not supply comments through /posts/:guid/ endpoint. """ - if self.id: id = self.id - if self.guid: id = self.guid + id = self._data['id'] if self['interactions']['comments_count']: request = self._connection.get('posts/{0}/comments.json'.format(id)) if request.status_code != 200: @@ -409,7 +417,7 @@ class Post(): def reshare(self): """This function reshares a post """ - data = {'root_guid': self.data['guid'], + data = {'root_guid': self._data['guid'], 'authenticity_token': repr(self._connection)} request = self._connection.post('reshares', @@ -465,8 +473,8 @@ class Post(): def delete_like(self): """This function removes a like from a post """ - data = {'authenticity_token': self._connection.get_token()} - url = 'posts/{0}/likes/{1}'.format(self.id, self.data['interactions']['likes'][0]['id']) + data = {'authenticity_token': repr(self._connection)} + url = 'posts/{0}/likes/{1}'.format(self.id, self._data['interactions']['likes'][0]['id']) request = self._connection.delete(url, data=data) if request.status_code != 204: raise errors.PostError('{0}: Like could not be removed.' @@ -476,4 +484,4 @@ class Post(): """Returns author of the post. :param key: all keys available in data['author'] """ - return self.data['author'][key] + return self._data['author'][key]