Merge pull request #6 from marekjm/tests
[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 Remember that you also need to be logged in.
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 """This function retrieves data of the post.
29 """
30 r = self._client.session.get('{0}/posts/{1}.json'.format(self._client.pod, self.post_id))
31 if r.status_code == 200:
32 return r.json()
33 else:
34 raise Exception('wrong status code: {0}'.format(r.status_code))
35
36 def like(self):
37 """This function likes a post.
38 It abstracts the 'Like' functionality.
39
40 :returns: dict -- json formatted like object.
41
42 """
43 data = {'authenticity_token': self._client.get_token()}
44
45 r = self._client.session.post('{0}/posts/{1}/likes'.format(self._client.pod, self.post_id),
46 data=data,
47 headers={'accept': 'application/json'})
48
49 if r.status_code != 201:
50 raise Exception('{0}: Post could not be liked.'.format(r.status_code))
51
52 return r.json()
53
54 def delete_like(self):
55 """This function removes a like from a post
56
57 """
58 data = {'authenticity_token': self._client.get_token()}
59
60 post_data = self.get_data()
61
62 r = self._client.session.delete('{0}/posts/{1}/likes/{2}'.format(self._client.pod, self.post_id, post_data['interactions']['likes'][0]['id']),
63 data=data)
64
65 if r.status_code != 204:
66 raise Exception('{0}: Like could not be removed.'.format(r.status_code))
67
68 def reshare(self):
69 """This function reshares a post
70
71 """
72 post_data = self.get_data()
73
74 data = {'root_guid': post_data['guid'],
75 'authenticity_token': self._client.get_token()}
76
77 r = self._client.session.post('{0}/reshares'.format(self._client.pod),
78 data=data,
79 headers={'accept': 'application/json'})
80
81 if r.status_code != 201:
82 raise Exception('{0}: Post could not be reshared.'.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'.format(self._client.pod, self.post_id),
97 data=data,
98 headers={'accept': 'application/json'})
99
100 if r.status_code != 201:
101 raise Exception('{0}: Comment could not be posted.'.format(r.status_code))
102
103 return r.json()
104
105 def delete_comment(self, comment_id):
106 """This function removes a comment from a post
107
108 :param comment_id: id of the comment to remove.
109 :type comment_id: str
110
111 """
112 data = {'authenticity_token': self._client.get_token()}
113
114 r = self._client.session.delete('{0}/posts/{1}/comments/{2}'.format(self._client.pod, self.post_id, comment_id),
115 data=data,
116 headers={'accept': 'application/json'})
117
118 if r.status_code != 204:
119 raise Exception('{0}: Comment could not be deleted.'.format(r.status_code))
120
121 def delete(self):
122 """ This function deletes this post
123
124 """
125 data = {'authenticity_token': self._client.get_token()}
126
127 r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
128 data=data,
129 headers={'accept': 'application/json'})