-
Notifications
You must be signed in to change notification settings - Fork 169
/
poll_choice.py
59 lines (46 loc) · 2.05 KB
/
poll_choice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import absolute_import, division, print_function, unicode_literals
from six import python_2_unicode_compatible
from canvasapi.exceptions import RequiredFieldMissing
from canvasapi.canvas_object import CanvasObject
from canvasapi.util import combine_kwargs
@python_2_unicode_compatible
class PollChoice(CanvasObject):
def __str__(self):
return "{} ({})".format(self.text, self.id)
def update(self, poll_choice, **kwargs):
"""
Update an existing choice for this poll.
:calls: `PUT /api/v1/polls/:poll_id/poll_choices/:id \
<https://canvas.instructure.com/doc/api/poll_choices.html#method.polling/poll_choices.update>`_
:param poll_choice: List of arguments. 'text' is required and 'is_correct' and 'position' \
are optional.
:type poll_choice: list
:rtype: :class:`canvasapi.poll_choice.PollChoice`
"""
if (isinstance(poll_choice, list) and isinstance(poll_choice[0], dict)
and 'text' in poll_choice[0]):
kwargs['poll_choice'] = poll_choice
else:
raise RequiredFieldMissing(
"Dictionary with key 'text' is required."
)
response = self._requester.request(
'PUT',
'polls/{}/poll_choices/{}'.format(self.poll_id, self.id),
_kwargs=combine_kwargs(**kwargs)
)
return PollChoice(self._requester, response.json()['poll_choices'][0])
def delete(self, **kwargs):
"""
Delete a single poll, based on the poll id.
:calls: `DELETE /api/v1/polls/:poll_id/poll_choices/:id \
<https://canvas.instructure.com/doc/api/poll_choices.html#method.polling/poll_choices.destroy>`_
:returns: True if the deletion was successful, false otherwise.
:rtype: bool
"""
response = self._requester.request(
'DELETE',
'polls/{}/poll_choices/{}'.format(self.poll_id, self.id),
_kwargs=combine_kwargs(**kwargs)
)
return response.status_code == 204