Small style and code fixes
[diaspy.git] / diaspy / models.py
1 #!/usr/bin/env python3
2
3
4 import json
5
6
7 """This module is only imported in other diaspy modules and
8 MUST NOT import anything.
9 """
10
11
12 class Aspect():
13 """This class represents an aspect.
14 """
15 def __init__(self, connection, id=-1):
16 self._connection = connection
17 self.id = id
18 self.name = ''
19
20 def addUser(self, user_id):
21 """Add user to current aspect.
22
23 :param user_id: user to add to aspect
24 :type user: int
25 """
26 data = {'authenticity_token': self._connection.get_token(),
27 'aspect_id': self.id,
28 'person_id': user_id}
29
30 request = self._connection.post('aspect_memberships.json', data=data)
31
32 if request.status_code != 201:
33 raise Exception('wrong status code: {0}'.format(request.status_code))
34 return request.json()
35
36 def removeUser(self, user_id):
37 """Remove user from current aspect.
38
39 :param user_id: user to remove from aspect
40 :type user: int
41 """
42 data = {'authenticity_token': self._connection.get_token(),
43 'aspect_id': self.id,
44 'person_id': user_id}
45
46 request = self.connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data)
47
48 if request.status_code != 200:
49 raise Exception('wrong status code: {0}'.format(request.status_code))
50 return request.json()
51
52
53 class Notification():
54 """This class represents single notification.
55 """
56 def __init__(self, connection, data):
57 self._connection = connection
58
59 self.type = list(data.keys())[0]
60 self.data = data[self.type]
61 self.id = self.data['id']
62 self.unread = self.data['unread']
63
64 def __getitem__(self, key):
65 return self.data[key]
66
67 def _refresh(self):
68 """Refreshes data of the notification.
69 """
70
71 def mark(self, unread=False):
72 """Marks notification to read/unread.
73 Marks notification to read if `unread` is False.
74 Marks notification to unread if `unread` is True.
75
76 :param unread: which state set for notification
77 :type unread: bool
78 """
79 headers = {'x-csrf-token': self._connection.get_token()}
80 params = {'set_unread': json.dumps(unread)}
81 self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers)
82 self.data['unread'] = unread
83
84
85 class Post():
86 """This class represents a post.
87
88 .. note::
89 Remember that you need to have access to the post.
90 """
91 def __init__(self, post_id, connection):
92 """
93 :param post_id: id or guid of the post
94 :type post_id: str
95 :param connection: connection object used to authenticate
96 :type connection: connection.Connection
97 """
98 self._connection = connection
99 self.post_id = post_id
100
101 def __repr__(self):
102 """Returns string containing more information then str().
103 """
104 data = self.get_data()
105 return '{0} ({1}): {2}'.format(data['author']['name'], data['author']['diaspora_id'], data['text'])
106
107 def __str__(self):
108 """Returns text of a post.
109 """
110 return self.get_data()['text']
111
112 def get_data(self):
113 """This function retrieves data of the post.
114 """
115 r = self._connection.get('posts/{0}.json'.format(self.post_id))
116 if r.status_code != 200:
117 raise Exception('wrong status code: {0}'.format(r.status_code))
118 return r.json()
119
120 def like(self):
121 """This function likes a post.
122 It abstracts the 'Like' functionality.
123
124 :returns: dict -- json formatted like object.
125 """
126 data = {'authenticity_token': self._connection.get_token()}
127
128 r = self._connection.post('posts/{0}/likes'.format(self.post_id),
129 data=data,
130 headers={'accept': 'application/json'})
131
132 if r.status_code != 201:
133 raise Exception('{0}: Post could not be liked.'
134 .format(r.status_code))
135
136 return r.json()
137
138 def delete_like(self):
139 """This function removes a like from a post
140 """
141 data = {'authenticity_token': self._connection.get_token()}
142
143 post_data = self.get_data()
144
145 r = self._connection.delete('posts/{0}/likes/{1}'
146 .format(self.post_id,
147 post_data['interactions']
148 ['likes'][0]['id']),
149 data=data)
150
151 if r.status_code != 204:
152 raise Exception('{0}: Like could not be removed.'
153 .format(r.status_code))
154
155 def reshare(self):
156 """This function reshares a post
157
158 """
159 post_data = self.get_data()
160
161 data = {'root_guid': post_data['guid'],
162 'authenticity_token': self._connection.get_token()}
163
164 r = self._connection.post('reshares',
165 data=data,
166 headers={'accept': 'application/json'})
167
168 if r.status_code != 201:
169 raise Exception('{0}: Post could not be reshared.'
170 .format(r.status_code))
171
172 return r.json()
173
174 def comment(self, text):
175 """This function comments on a post
176
177 :param text: text to comment.
178 :type text: str
179 """
180 data = {'text': text,
181 'authenticity_token': self._connection.get_token()}
182
183 r = self._connection.post('posts/{0}/comments'.format(self.post_id),
184 data=data,
185 headers={'accept': 'application/json'})
186
187 if r.status_code != 201:
188 raise Exception('{0}: Comment could not be posted.'
189 .format(r.status_code))
190
191 return r.json()
192
193 def delete_comment(self, comment_id):
194 """This function removes a comment from a post
195
196 :param comment_id: id of the comment to remove.
197 :type comment_id: str
198 """
199 data = {'authenticity_token': self._connection.get_token()}
200
201 r = self._connection.delete('posts/{0}/comments/{1}'
202 .format(self.post_id,
203 comment_id),
204 data=data,
205 headers={'accept': 'application/json'})
206
207 if r.status_code != 204:
208 raise Exception('{0}: Comment could not be deleted.'
209 .format(r.status_code))
210
211 def delete(self):
212 """ This function deletes this post
213 """
214 data = {'authenticity_token': self._connection.get_token()}
215 r = self._connection.delete('posts/{0}'.format(self.post_id),
216 data=data,
217 headers={'accept': 'application/json'})
218 if r.status_code != 204:
219 raise Exception('{0}: Post could not be deleted'.format(r.status_code))