Further work being done on streams; all basic streams implemented
[diaspy.git] / diaspy / models.py
CommitLineData
88ec6cda 1#!/usr/bin/env python3
ae221396
MK
2
3
a993a4b6 4class Post:
ae221396
MK
5 """This class represents a post.
6
7 .. note::
8 Remember that you need to have access to the post.
df912114 9 Remember that you also need to be logged in.
ae221396 10 """
b95ffe83 11 def __init__(self, post_id, connection):
a7661afd
MK
12 """
13 :param post_id: id or guid of the post
14 :type post_id: str
b95ffe83
MM
15 :param connection: connection object used to authenticate
16 :type connection: connection.Connection
a7661afd 17 """
b95ffe83 18 self._connection = connection
8095ac8b
MK
19 self.post_id = post_id
20
7eb90eb7 21 def get_data(self):
0e858bba
MM
22 """This function retrieves data of the post.
23 """
505fc964
MM
24 r = self._connection.get('posts/{0}.json'.format(self.post_id))
25 if r.status_code != 200:
0e858bba 26 raise Exception('wrong status code: {0}'.format(r.status_code))
505fc964 27 return r.json()
a993a4b6
MK
28
29 def like(self):
88ec6cda 30 """This function likes a post.
df912114 31 It abstracts the 'Like' functionality.
a993a4b6
MK
32
33 :returns: dict -- json formatted like object.
a993a4b6 34 """
b95ffe83 35 data = {'authenticity_token': self._connection.getToken()}
a993a4b6 36
b95ffe83 37 r = self._connection.post('posts/{0}/likes'.format(self.post_id),
385e7ebe
MM
38 data=data,
39 headers={'accept': 'application/json'})
a7661afd
MK
40
41 if r.status_code != 201:
9088535d
MK
42 raise Exception('{0}: Post could not be liked.'
43 .format(r.status_code))
a7661afd 44
a993a4b6
MK
45 return r.json()
46
5e809c8b 47 def delete_like(self):
a993a4b6 48 """This function removes a like from a post
a993a4b6 49 """
b95ffe83 50 data = {'authenticity_token': self._connection.getToken()}
a993a4b6 51
7eb90eb7 52 post_data = self.get_data()
8095ac8b 53
b95ffe83 54 r = self._connection.delete('posts/{0}/likes/{1}'
385e7ebe
MM
55 .format(self.post_id,
56 post_data['interactions']
57 ['likes'][0]['id']),
58 data=data)
a993a4b6 59
a7661afd 60 if r.status_code != 204:
9088535d
MK
61 raise Exception('{0}: Like could not be removed.'
62 .format(r.status_code))
a7661afd 63
a993a4b6
MK
64 def reshare(self):
65 """This function reshares a post
66
67 """
7eb90eb7 68 post_data = self.get_data()
8095ac8b
MK
69
70 data = {'root_guid': post_data['guid'],
b95ffe83 71 'authenticity_token': self._connection.getToken()}
a993a4b6 72
b95ffe83 73 r = self._connection.post('reshares',
385e7ebe
MM
74 data=data,
75 headers={'accept': 'application/json'})
a7661afd
MK
76
77 if r.status_code != 201:
9088535d
MK
78 raise Exception('{0}: Post could not be reshared.'
79 .format(r.status_code))
a993a4b6
MK
80
81 return r.json()
82
83 def comment(self, text):
84 """This function comments on a post
85
a993a4b6
MK
86 :param text: text to comment.
87 :type text: str
88
89 """
a993a4b6 90 data = {'text': text,
b95ffe83 91 'authenticity_token': self._connection.getToken()}
a993a4b6 92
b95ffe83 93 r = self._connection.post('posts/{0}/comments'.format(self.post_id),
385e7ebe
MM
94 data=data,
95 headers={'accept': 'application/json'})
a7661afd
MK
96
97 if r.status_code != 201:
9088535d
MK
98 raise Exception('{0}: Comment could not be posted.'
99 .format(r.status_code))
a993a4b6
MK
100
101 return r.json()
ae221396 102
5e809c8b 103 def delete_comment(self, comment_id):
a993a4b6
MK
104 """This function removes a comment from a post
105
91d2d5dc
B
106 :param comment_id: id of the comment to remove.
107 :type comment_id: str
a993a4b6
MK
108
109 """
b95ffe83 110 data = {'authenticity_token': self._connection.getToken()}
a993a4b6 111
b95ffe83 112 r = self._connection.delete('posts/{0}/comments/{1}'
385e7ebe
MM
113 .format(self.post_id,
114 comment_id),
115 data=data,
116 headers={'accept': 'application/json'})
a7661afd
MK
117
118 if r.status_code != 204:
9088535d
MK
119 raise Exception('{0}: Comment could not be deleted.'
120 .format(r.status_code))
5e809c8b
MK
121
122 def delete(self):
123 """ This function deletes this post
5e809c8b 124 """
b95ffe83
MM
125 data = {'authenticity_token': self._connection.getToken()}
126 r = self._connection.delete('posts/{0}'.format(self.post_id),
385e7ebe
MM
127 data=data,
128 headers={'accept': 'application/json'})
65b91311
MM
129 if r.status_code != 204:
130 raise Exception('{0}: Post could not be deleted'.format(r.status_code))