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