diff options
Diffstat (limited to 'mastodon/polls.py')
-rw-r--r-- | mastodon/polls.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/mastodon/polls.py b/mastodon/polls.py new file mode 100644 index 0000000..d974531 --- /dev/null +++ b/mastodon/polls.py | |||
@@ -0,0 +1,61 @@ | |||
1 | # polls.py - poll related endpoints and tooling | ||
2 | |||
3 | from .versions import _DICT_VERSION_POLL | ||
4 | from .utility import api_version | ||
5 | |||
6 | from .internals import Mastodon as Internals | ||
7 | |||
8 | class Mastodon(Internals): | ||
9 | ### | ||
10 | # Reading data: Polls | ||
11 | ### | ||
12 | @api_version("2.8.0", "2.8.0", _DICT_VERSION_POLL) | ||
13 | def poll(self, id): | ||
14 | """ | ||
15 | Fetch information about the poll with the given id | ||
16 | |||
17 | Returns a :ref:`poll dict <poll dict>`. | ||
18 | """ | ||
19 | id = self.__unpack_id(id) | ||
20 | url = '/api/v1/polls/{0}'.format(str(id)) | ||
21 | return self.__api_request('GET', url) | ||
22 | |||
23 | ### | ||
24 | # Writing data: Polls | ||
25 | ### | ||
26 | @api_version("2.8.0", "2.8.0", _DICT_VERSION_POLL) | ||
27 | def poll_vote(self, id, choices): | ||
28 | """ | ||
29 | Vote in the given poll. | ||
30 | |||
31 | `choices` is the index of the choice you wish to register a vote for | ||
32 | (i.e. its index in the corresponding polls `options` field. In case | ||
33 | of a poll that allows selection of more than one option, a list of | ||
34 | indices can be passed. | ||
35 | |||
36 | You can only submit choices for any given poll once in case of | ||
37 | single-option polls, or only once per option in case of multi-option | ||
38 | polls. | ||
39 | |||
40 | Returns the updated :ref:`poll dict <poll dict>` | ||
41 | """ | ||
42 | id = self.__unpack_id(id) | ||
43 | if not isinstance(choices, list): | ||
44 | choices = [choices] | ||
45 | params = self.__generate_params(locals(), ['id']) | ||
46 | |||
47 | url = '/api/v1/polls/{0}/votes'.format(id) | ||
48 | self.__api_request('POST', url, params) | ||
49 | |||
50 | def make_poll(self, options, expires_in, multiple=False, hide_totals=False): | ||
51 | """ | ||
52 | Generate a poll object that can be passed as the `poll` option when posting a status. | ||
53 | |||
54 | options is an array of strings with the poll options (Maximum, by default: 4), | ||
55 | expires_in is the time in seconds for which the poll should be open. | ||
56 | Set multiple to True to allow people to choose more than one answer. Set | ||
57 | hide_totals to True to hide the results of the poll until it has expired. | ||
58 | """ | ||
59 | poll_params = locals() | ||
60 | del poll_params["self"] | ||
61 | return poll_params \ No newline at end of file | ||