* __upd__: Add `Comment()` to `diaspy.models.Post.comments` on `diaspy.models.Post...
[diaspy.git] / diaspy / tagFollowings.py
CommitLineData
8bfe79c7
C
1#!/usr/bin/env python3
2from diaspy.models import FollowedTag
3from diaspy import errors
4class TagFollowings():
5 """This class represents the tags followed by the user.
6
7 Should return `dict`s in a `list` with the following keys:
8 `id`, `name`, `taggings_count`
9 """
10 def __init__(self, connection, fetch=True):
11 self._connection = connection
12 self._tags = []
13 if fetch: self.fetch()
14
15 def __iter__(self): return iter(self._tags)
16
17 def __getitem__(self, t): return self._tags[t]
18
19 def _finalise(self, tags):
20 return([FollowedTag(self._connection, t['id'], t['name'],
21 t['taggings_count']) for t in tags])
22
23 def fetch(self):
24 """(Re-)Fetches your followed tags.
25 """
26 self._tags = self.get()
27
28 def follow(self, name):
29 """Follows a tag by given name.
30
31 Returns FollowedTag object.
32 """
33 data = {'authenticity_token': repr(self._connection)}
34 params = {'name': name}
35 request = self._connection.post('tag_followings', data=data,
36 params=params, headers={'accept': 'application/json'})
37 if request.status_code != 201:
38 raise errors.TagError('{0}: Tag could not be followed.'
39 .format(request.status_code))
40 result = request.json()
41 self._tags.append(FollowedTag(self._connection, result['id'],
42 result['name'], result['taggings_count']))
43 return self._tags[(len(self._tags) - 1)]
44
45 def get(self):
46 request = self._connection.get('tag_followings.json')
47 if request.status_code != 200:
48 raise Exception('status code: {0}: cannot retreive tag_followings'
49 .format(request.status_code))
50 return self._finalise(request.json())