"""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']
"""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']
:param user_id: user to remove from aspect
:type user: int
"""
- data = {'authenticity_token': self._connection.get_token(),
+ 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)
+ request = self._connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data)
if request.status_code != 200:
raise errors.AspectError('cannot remove user from aspect: {0}'.format(request.status_code))
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
"""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
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.
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():
"""
self._connection = connection
self.id = id
- self.data = {}
+ self._data = {}
if fetch: self._fetch()
def _fetch(self):
"""
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))
headers={'accept': 'application/json'})
if request.status_code != 200:
raise errors.ConversationError('{0}: Answer could not be posted.'
- .format(request.status_code))
+ .format(request.status_code))
return request.json()
def delete(self):
data = {'authenticity_token': repr(self._connection)}
request = self._connection.delete('conversations/{0}/visibility/'
- .format(self.id),
- data=data,
- headers={'accept': 'application/json'})
+ .format(self.id),
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 404:
raise errors.ConversationError('{0}: Conversation could not be deleted.'
- .format(request.status_code))
+ .format(request.status_code))
def get_subject(self):
"""Returns the subject of this conversation
"""
- return self.data['subject']
+ return self._data['subject']
class Comment():
"""Represents comment on post.
-
+
Does not require Connection() object. Note that you should not manually
create `Comment()` objects -- they are designed to be created automatically
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.
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():
: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.
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:
data = {'authenticity_token': repr(self._connection)}
request = self._connection.post('posts/{0}/likes'.format(self.id),
- data=data,
- headers={'accept': 'application/json'})
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 201:
raise errors.PostError('{0}: Post could not be liked.'
- .format(request.status_code))
+ .format(request.status_code))
return request.json()
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',
- data=data,
- headers={'accept': 'application/json'})
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 201:
raise Exception('{0}: Post could not be reshared'.format(request.status_code))
return request.json()
data = {'text': text,
'authenticity_token': repr(self._connection)}
request = self._connection.post('posts/{0}/comments'.format(self.id),
- data=data,
- headers={'accept': 'application/json'})
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 201:
raise Exception('{0}: Comment could not be posted.'
"""
data = {'authenticity_token': repr(self._connection)}
request = self._connection.delete('posts/{0}'.format(self.id),
- data=data,
- headers={'accept': 'application/json'})
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 204:
raise errors.PostError('{0}: Post could not be deleted'.format(request.status_code))
"""
data = {'authenticity_token': repr(self._connection)}
request = self._connection.delete('posts/{0}/comments/{1}'
- .format(self.id,
- comment_id),
- data=data,
- headers={'accept': 'application/json'})
+ .format(self.id, comment_id),
+ data=data,
+ headers={'accept': 'application/json'})
if request.status_code != 204:
raise errors.PostError('{0}: Comment could not be deleted'
- .format(request.status_code))
+ .format(request.status_code))
def delete_like(self):
"""This function removes a like from a post
"""
- data = {'authenticity_token': self._connection.get_token()}
-
- request = self._connection.delete('posts/{0}/likes/{1}'
- .format(self.id,
- self.data['interactions']
- ['likes'][0]['id']),
- data=data)
+ 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.'
- .format(request.status_code))
+ .format(request.status_code))
def author(self, key='name'):
"""Returns author of the post.
:param key: all keys available in data['author']
"""
- return self.data['author'][key]
+ return self._data['author'][key]