Style check (flake8)
[diaspy.git] / diaspy / models.py
1 #!/usr/bin/env python3
2
3
4 class Post:
5 """This class represents a post.
6
7 .. note::
8 Remember that you need to have access to the post.
9 """
10 def __init__(self, post_id, connection):
11 """
12 :param post_id: id or guid of the post
13 :type post_id: str
14 :param connection: connection object used to authenticate
15 :type connection: connection.Connection
16 """
17 self._connection = connection
18 self.post_id = post_id
19
20 def __str__(self):
21 """Returns text of a post.
22 """
23 return self.get_data['text']
24
25 def get_data(self):
26 """This function retrieves data of the post.
27 """
28 r = self._connection.get('posts/{0}.json'.format(self.post_id))
29 if r.status_code != 200:
30 raise Exception('wrong status code: {0}'.format(r.status_code))
31 return r.json()
32
33 def like(self):
34 """This function likes a post.
35 It abstracts the 'Like' functionality.
36
37 :returns: dict -- json formatted like object.
38 """
39 data = {'authenticity_token': self._connection.get_token()}
40
41 r = self._connection.post('posts/{0}/likes'.format(self.post_id),
42 data=data,
43 headers={'accept': 'application/json'})
44
45 if r.status_code != 201:
46 raise Exception('{0}: Post could not be liked.'
47 .format(r.status_code))
48
49 return r.json()
50
51 def delete_like(self):
52 """This function removes a like from a post
53 """
54 data = {'authenticity_token': self._connection.get_token()}
55
56 post_data = self.get_data()
57
58 r = self._connection.delete('posts/{0}/likes/{1}'
59 .format(self.post_id,
60 post_data['interactions']
61 ['likes'][0]['id']),
62 data=data)
63
64 if r.status_code != 204:
65 raise Exception('{0}: Like could not be removed.'
66 .format(r.status_code))
67
68 def reshare(self):
69 """This function reshares a post
70
71 """
72 post_data = self.get_data()
73
74 data = {'root_guid': post_data['guid'],
75 'authenticity_token': self._connection.get_token()}
76
77 r = self._connection.post('reshares',
78 data=data,
79 headers={'accept': 'application/json'})
80
81 if r.status_code != 201:
82 raise Exception('{0}: Post could not be reshared.'
83 .format(r.status_code))
84
85 return r.json()
86
87 def comment(self, text):
88 """This function comments on a post
89
90 :param text: text to comment.
91 :type text: str
92 """
93 data = {'text': text,
94 'authenticity_token': self._connection.get_token()}
95
96 r = self._connection.post('posts/{0}/comments'.format(self.post_id),
97 data=data,
98 headers={'accept': 'application/json'})
99
100 if r.status_code != 201:
101 raise Exception('{0}: Comment could not be posted.'
102 .format(r.status_code))
103
104 return r.json()
105
106 def delete_comment(self, comment_id):
107 """This function removes a comment from a post
108
109 :param comment_id: id of the comment to remove.
110 :type comment_id: str
111 """
112 data = {'authenticity_token': self._connection.get_token()}
113
114 r = self._connection.delete('posts/{0}/comments/{1}'
115 .format(self.post_id,
116 comment_id),
117 data=data,
118 headers={'accept': 'application/json'})
119
120 if r.status_code != 204:
121 raise Exception('{0}: Comment could not be deleted.'
122 .format(r.status_code))
123
124 def delete(self):
125 """ This function deletes this post
126 """
127 data = {'authenticity_token': self._connection.get_token()}
128 r = self._connection.delete('posts/{0}'.format(self.post_id),
129 data=data,
130 headers={'accept': 'application/json'})
131 if r.status_code != 204:
132 raise Exception('{0}: Post could not be deleted'.format(r.status_code))
133
134
135 class Aspect():
136 """This class represents an aspect.
137 """
138 def __init__(self, connection, id=-1):
139 self._connection = connection
140 self.id = id
141 self.name = ''
142
143 def addUser(self, user_id):
144 """Add user to current aspect.
145
146 :param user_id: user to add to aspect
147 :type user: int
148 """
149 data = {'authenticity_token': self._connection.get_token(),
150 'aspect_id': self.id,
151 'person_id': user_id}
152
153 request = self._connection.post('aspect_memberships.json', data=data)
154
155 if request.status_code != 201:
156 raise Exception('wrong status code: {0}'.format(request.status_code))
157 return request.json()
158
159 def removeUser(self, user_id):
160 """Remove user from current aspect.
161
162 :param user_id: user to remove from aspect
163 :type user: int
164 """
165 data = {'authenticity_token': self._connection.get_token(),
166 'aspect_id': self.id,
167 'person_id': user_id}
168
169 request = self.connection.delete('aspect_memberships/42.json', data=data)
170
171 if request.status_code != 200:
172 raise Exception('wrong status code: {0}'.format(request.status_code))
173 return request.json()