ea22b45d3d6a76a212d0362584d86860d9ae5f14
[diaspy.git] / diaspy / models.py
1 import requests
2 class Post:
3
4 def __init__(self, post_id, client):
5 self._client = client
6 r = self._client.session.get(self._client.pod + '/posts/' + post_id + '.json')
7 if r.status_code == 200:
8 self.data = r.json()
9 else:
10 raise Exception('wrong status code: ' + str(r.status_code))
11
12 def like(self):
13 """This function likes a post
14
15 :returns: dict -- json formatted like object.
16
17 """
18
19 data = {'authenticity_token': self._client.get_token()}
20
21 r = self._client.session.post(self._client.pod +
22 "/posts/" +
23 str(self.data['id']) +
24 "/likes",
25 data=data,
26 headers={'accept': 'application/json'})
27 return r.json()
28
29 def rmlike(self):
30 """This function removes a like from a post
31
32 """
33
34 data = {'authenticity_token': self._client.get_token()}
35
36 r = self._client.session.delete(self._client.pod + '/posts/' +
37 str(self.data['id']) +
38 '/likes/' +
39 str(self.data['interactions']['likes'][0]['id']),
40 data=data)
41
42 def reshare(self):
43 """This function reshares a post
44
45 """
46
47 data = {'root_guid': self.data['guid'],
48 'authenticity_token': self._client.get_token()}
49
50 r = self._client.session.post(self._client.pod +
51 "/reshares",
52 data=data)
53
54 return r.json()
55
56 def comment(self, text):
57 """This function comments on a post
58
59 :param post_id: id of the post to comment on.
60 :type post_id: str
61 :param text: text to comment.
62 :type text: str
63
64 """
65
66 data = {'text': text,
67 'authenticity_token': self._client.get_token()}
68
69 r = self._client.session.post(self._client.pod + '/posts/' + str(self.data['id']) + '/comments', data=data)
70
71 return r.json()
72
73 def rmcomment(self, comment_id):
74 """This function removes a comment from a post
75
76 :param post_id: id of the post to remove the like from.
77 :type post_id: str
78 :param like_id: id of the like to remove.
79 :type like_id: str
80
81 """
82
83 data = {'authenticity_token': self._client.get_token()}
84
85 r = self._client.session.delete(self._client.pod + '/posts/' +
86 str(self.data['id']) +
87 '/comments/' +
88 comment_id,
89 data=data)