66624f37a33c4b5ff61068d79949454103711f45
[diaspy.git] / diaspy / models.py
1 #!/usr/bin/env python3
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 def __init__(self, post_id, connection):
12 """
13 :param post_id: id or guid of the post
14 :type post_id: str
15 :param connection: connection object used to authenticate
16 :type connection: connection.Connection
17 """
18 self._connection = connection
19 self.post_id = post_id
20
21 def get_data(self):
22 """This function retrieves data of the post.
23 """
24 r = self._connection.get('posts/{0}.json'.format(self.post_id))
25 if r.status_code != 200:
26 raise Exception('wrong status code: {0}'.format(r.status_code))
27 return r.json()
28
29 def like(self):
30 """This function likes a post.
31 It abstracts the 'Like' functionality.
32
33 :returns: dict -- json formatted like object.
34 """
35 data = {'authenticity_token': self._connection.getToken()}
36
37 r = self._connection.post('posts/{0}/likes'.format(self.post_id),
38 data=data,
39 headers={'accept': 'application/json'})
40
41 if r.status_code != 201:
42 raise Exception('{0}: Post could not be liked.'
43 .format(r.status_code))
44
45 return r.json()
46
47 def delete_like(self):
48 """This function removes a like from a post
49 """
50 data = {'authenticity_token': self._connection.getToken()}
51
52 post_data = self.get_data()
53
54 r = self._connection.delete('posts/{0}/likes/{1}'
55 .format(self.post_id,
56 post_data['interactions']
57 ['likes'][0]['id']),
58 data=data)
59
60 if r.status_code != 204:
61 raise Exception('{0}: Like could not be removed.'
62 .format(r.status_code))
63
64 def reshare(self):
65 """This function reshares a post
66
67 """
68 post_data = self.get_data()
69
70 data = {'root_guid': post_data['guid'],
71 'authenticity_token': self._connection.getToken()}
72
73 r = self._connection.post('reshares',
74 data=data,
75 headers={'accept': 'application/json'})
76
77 if r.status_code != 201:
78 raise Exception('{0}: Post could not be reshared.'
79 .format(r.status_code))
80
81 return r.json()
82
83 def comment(self, text):
84 """This function comments on a post
85
86 :param text: text to comment.
87 :type text: str
88
89 """
90 data = {'text': text,
91 'authenticity_token': self._connection.getToken()}
92
93 r = self._connection.post('posts/{0}/comments'.format(self.post_id),
94 data=data,
95 headers={'accept': 'application/json'})
96
97 if r.status_code != 201:
98 raise Exception('{0}: Comment could not be posted.'
99 .format(r.status_code))
100
101 return r.json()
102
103 def delete_comment(self, comment_id):
104 """This function removes a comment from a post
105
106 :param comment_id: id of the comment to remove.
107 :type comment_id: str
108
109 """
110 data = {'authenticity_token': self._connection.getToken()}
111
112 r = self._connection.delete('posts/{0}/comments/{1}'
113 .format(self.post_id,
114 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.'
120 .format(r.status_code))
121
122 def delete(self):
123 """ This function deletes this post
124 """
125 data = {'authenticity_token': self._connection.getToken()}
126 r = self._connection.delete('posts/{0}'.format(self.post_id),
127 data=data,
128 headers={'accept': 'application/json'})
129 if r.status_code != 204:
130 raise Exception('{0}: Post could not be deleted'.format(r.status_code))