4700d21f6bf943feb438eb0ad91ce73bb954fdf4
[diaspy.git] / diaspy / models.py
1 import requests
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 """
11
12 def __init__(self, post_id, client):
13 """
14 :param post_id: id or guid of the post
15 :type post_id: str
16 :param client: client object used to authenticate
17 :type client: client.Client
18
19 .. note::
20 The login function of the client should be called,
21 before calling any of the post functions.
22
23 """
24
25 self._client = client
26 r = self._client.session.get(self._client.pod +
27 '/posts/' +
28 post_id +
29 '.json')
30 if r.status_code == 200:
31 self.data = r.json()
32 else:
33 raise Exception('wrong status code: ' + str(r.status_code))
34
35 def like(self):
36 """This function likes a post
37
38 :returns: dict -- json formatted like object.
39
40 """
41
42 data = {'authenticity_token': self._client.get_token()}
43
44 r = self._client.session.post(self._client.pod +
45 '/posts/' +
46 str(self.data['id']) +
47 '/likes',
48 data=data,
49 headers={'accept': 'application/json'})
50
51 if r.status_code != 201:
52 raise Exception(str(r.status_code) + ': Post could not be liked.')
53
54 return r.json()
55
56 def rmlike(self):
57 """This function removes a like from a post
58
59 """
60
61 data = {'authenticity_token': self._client.get_token()}
62
63 r = self._client.session.delete(self._client.pod + '/posts/' +
64 str(self.data['id']) +
65 '/likes/' +
66 str(self.data['interactions']
67 ['likes'][0]['id']),
68 data=data)
69
70 if r.status_code != 204:
71 raise Exception(str(r.status_code) +
72 ': Like could not be removed.')
73
74 def reshare(self):
75 """This function reshares a post
76
77 """
78
79 data = {'root_guid': self.data['guid'],
80 'authenticity_token': self._client.get_token()}
81
82 r = self._client.session.post(self._client.pod +
83 '/reshares',
84 data=data,
85 headers={'accept': 'application/json'})
86
87 if r.status_code != 201:
88 raise Exception(str(r.status_code) +
89 ': Post could not be reshared.')
90
91 return r.json()
92
93 def comment(self, text):
94 """This function comments on a post
95
96 :param post_id: id of the post to comment on.
97 :type post_id: str
98 :param text: text to comment.
99 :type text: str
100
101 """
102
103 data = {'text': text,
104 'authenticity_token': self._client.get_token()}
105
106 r = self._client.session.post(self._client.pod +
107 '/posts/' +
108 str(self.data['id']) +
109 '/comments',
110 data=data,
111 headers={'accept': 'application/json'})
112
113 if r.status_code != 201:
114 raise Exception(str(r.status_code) +
115 ': Comment could not be posted.')
116
117 return r.json()
118
119 def rmcomment(self, comment_id):
120 """This function removes a comment from a post
121
122 :param post_id: id of the post to remove the like from.
123 :type post_id: str
124 :param like_id: id of the like to remove.
125 :type like_id: str
126
127 """
128
129 data = {'authenticity_token': self._client.get_token()}
130
131 r = self._client.session.delete(self._client.pod + '/posts/' +
132 str(self.data['id']) +
133 '/comments/' +
134 comment_id,
135 data=data,
136 headers={'accept': 'application/json'})
137
138 if r.status_code != 204:
139 raise Exception(str(r.status_code) +
140 ': Comment could not be deleted.')