aboutsummaryrefslogtreecommitdiffstats
path: root/poll.py
blob: 4d58deae7928855d04457b4ee24a9a8a6ee39f0d (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
"""poll wrapper class"""

import datetime

class PleromaPoll:
    """
    wrapper class for handling polls
    """
    def __init__(self,
            options: list[str],
            expires_in: datetime.timedelta,
            multiple: bool = None,
            hide_totals: bool = None):
        self._options = options
        self._expires_in = expires_in.total_seconds()
        self._multiple = multiple
        self._hide_totals = hide_totals

    @classmethod
    def from_response(cls, data: dict):
        """
        alternative constructor to create object from response of a server

        as the app is used to only view scheduled posts, this constructor doesn't
        retain information about vote counts.
        """
        return cls(expires_in=datetime.datetime.fromisoformat(data["expires_in"]),
                multiple=data["multiple"],
                options=[i["title"] for i in data["options"]])

    @property
    def options(self):
        """
        a list of strings, which are options for poll
        """
        return self._options

    @property
    def expires_in(self):
        """
        timedelta, which represents time in which poll will expire
        """
        return self._expires_in

    @property
    def multiple(self):
        """
        boolean flag, True if poll allows multiple choices
        """
        return self._multiple

    @multiple.setter
    def multiple(self, flag: bool):
        self._multiple = flag

    @property
    def hide_totals(self):
        """
        boolean flag, True if counts are hidden until poll ends
        """
        return self._hide_totals

    @hide_totals.setter
    def hide_totals(self, flag: bool):
        self._hide_totals = flag

    def __repr__(self):
        return f"PleromaPoll(options={self._options},\
expires_in={self._expires_in},\
multiple={self._multiple},\
hide_totals={self._hide_totals})"