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