Methods refactored to use _sessionpost(), tox.ini file added
[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 Remember that you also need to be logged in.
10 """
11 def __init__(self, post_id, client):
12 """
13 :param post_id: id or guid of the post
14 :type post_id: str
15 :param client: client object used to authenticate
16 :type client: client.Client
17 """
18 self._client = client
19 self.post_id = post_id
20
21 def get_data(self):
22 """This function retrieves data of the post.
23 """
24 r = self._client._sessionget('posts/{1}.json'.format(self.post_id))
25 if r.status_code == 200:
26 return r.json()
27 else:
28 raise Exception('wrong status code: {0}'.format(r.status_code))
29
30 def like(self):
31 """This function likes a post.
32 It abstracts the 'Like' functionality.
33
34 :returns: dict -- json formatted like object.
35 """
36 data = {'authenticity_token': self._client.get_token()}
37
38 r = self._client._sessionpost('posts/{0}/likes'.format(self.post_id),
39 data=data,
40 headers={'accept': 'application/json'})
41
42 if r.status_code != 201:
43 raise Exception('{0}: Post could not be liked.'
44 .format(r.status_code))
45
46 return r.json()
47
48 def delete_like(self):
49 """This function removes a like from a post
50 """
51 data = {'authenticity_token': self._client.get_token()}
52
53 post_data = self.get_data()
54
55 r = self._client.session.delete('{0}/posts/{1}/likes/{2}'
56 .format(self._client.pod,
57 self.post_id,
58 post_data['interactions']
59 ['likes'][0]['id']),
60 data=data)
61
62 if r.status_code != 204:
63 raise Exception('{0}: Like could not be removed.'
64 .format(r.status_code))
65
66 def reshare(self):
67 """This function reshares a post
68
69 """
70 post_data = self.get_data()
71
72 data = {'root_guid': post_data['guid'],
73 'authenticity_token': self._client.get_token()}
74
75 r = self._client._sessionpost('reshares',
76 data=data,
77 headers={'accept': 'application/json'})
78
79 if r.status_code != 201:
80 raise Exception('{0}: Post could not be reshared.'
81 .format(r.status_code))
82
83 return r.json()
84
85 def comment(self, text):
86 """This function comments on a post
87
88 :param text: text to comment.
89 :type text: str
90
91 """
92 data = {'text': text,
93 'authenticity_token': self._client.get_token()}
94
95 r = self._client._sessionpost('posts/{0}/comments'.format(self.post_id),
96 data=data,
97 headers={'accept': 'application/json'})
98
99 if r.status_code != 201:
100 raise Exception('{0}: Comment could not be posted.'
101 .format(r.status_code))
102
103 return r.json()
104
105 def delete_comment(self, comment_id):
106 """This function removes a comment from a post
107
108 :param comment_id: id of the comment to remove.
109 :type comment_id: str
110
111 """
112 data = {'authenticity_token': self._client.get_token()}
113
114 r = self._client.session.delete('{0}/posts/{1}/comments/{2}'
115 .format(self._client.pod,
116 self.post_id,
117 comment_id),
118 data=data,
119 headers={'accept': 'application/json'})
120
121 if r.status_code != 204:
122 raise Exception('{0}: Comment could not be deleted.'
123 .format(r.status_code))
124
125 def delete(self):
126 """ This function deletes this post
127 """
128 data = {'authenticity_token': self._client.get_token()}
129 r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
130 data=data,
131 headers={'accept': 'application/json'})
132 if r.status_code != 204:
133 raise Exception('{0}: Post could not be deleted'.format(r.status_code))