34a675b9bde1e1d5dcc3f88415611e4324eda606
[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.session.post('{0}/posts/{1}/likes'
39 .format(self._client.pod, self.post_id),
40 data=data,
41 headers={'accept': 'application/json'})
42
43 if r.status_code != 201:
44 raise Exception('{0}: Post could not be liked.'
45 .format(r.status_code))
46
47 return r.json()
48
49 def delete_like(self):
50 """This function removes a like from a post
51 """
52 data = {'authenticity_token': self._client.get_token()}
53
54 post_data = self.get_data()
55
56 r = self._client.session.delete('{0}/posts/{1}/likes/{2}'
57 .format(self._client.pod,
58 self.post_id,
59 post_data['interactions']
60 ['likes'][0]['id']),
61 data=data)
62
63 if r.status_code != 204:
64 raise Exception('{0}: Like could not be removed.'
65 .format(r.status_code))
66
67 def reshare(self):
68 """This function reshares a post
69
70 """
71 post_data = self.get_data()
72
73 data = {'root_guid': post_data['guid'],
74 'authenticity_token': self._client.get_token()}
75
76 r = self._client.session.post('{0}/reshares'.format(self._client.pod),
77 data=data,
78 headers={'accept': 'application/json'})
79
80 if r.status_code != 201:
81 raise Exception('{0}: Post could not be reshared.'
82 .format(r.status_code))
83
84 return r.json()
85
86 def comment(self, text):
87 """This function comments on a post
88
89 :param text: text to comment.
90 :type text: str
91
92 """
93 data = {'text': text,
94 'authenticity_token': self._client.get_token()}
95
96 r = self._client.session.post('{0}/posts/{1}/comments'
97 .format(self._client.pod, self.post_id),
98 data=data,
99 headers={'accept': 'application/json'})
100
101 if r.status_code != 201:
102 raise Exception('{0}: Comment could not be posted.'
103 .format(r.status_code))
104
105 return r.json()
106
107 def delete_comment(self, comment_id):
108 """This function removes a comment from a post
109
110 :param comment_id: id of the comment to remove.
111 :type comment_id: str
112
113 """
114 data = {'authenticity_token': self._client.get_token()}
115
116 r = self._client.session.delete('{0}/posts/{1}/comments/{2}'
117 .format(self._client.pod,
118 self.post_id,
119 comment_id),
120 data=data,
121 headers={'accept': 'application/json'})
122
123 if r.status_code != 204:
124 raise Exception('{0}: Comment could not be deleted.'
125 .format(r.status_code))
126
127 def delete(self):
128 """ This function deletes this post
129
130 """
131 data = {'authenticity_token': self._client.get_token()}
132 r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
133 data=data,
134 headers={'accept': 'application/json'})
135 if r.status_code != 204:
136 raise Exception('{0}: Post could not be deleted'.format(r.status_code))
137
138 r = self._client.session.delete('{0}/posts/{1}'
139 .format(self._client.pod,
140 self.post_id),
141 data=data,
142 headers={'accept': 'application/json'})
143 if r.status_code != 204:
144 raise Exception('{0}: Post could not be deleted.'
145 .format(r.status_code))