all repos — pleroma_announce @ main

small tool for posting announcements to pleroma

poll.py (view raw)

 1"""poll wrapper class"""
 2
 3import datetime
 4
 5class PleromaPoll:
 6    """
 7    wrapper class for handling polls
 8    """
 9    def __init__(self,
10            options: list[str],
11            expires_in: datetime.timedelta,
12            multiple: bool = None,
13            hide_totals: bool = None):
14        self._options = options
15        self._expires_in = expires_in.total_seconds()
16        self._multiple = multiple
17        self._hide_totals = hide_totals
18
19    @classmethod
20    def from_response(cls, data: dict):
21        """
22        alternative constructor to create object from response of a server
23
24        as the app is used to only view scheduled posts, this constructor doesn't
25        retain information about vote counts.
26        """
27        return cls(expires_in=datetime.datetime.fromisoformat(data["expires_in"]),
28                multiple=data["multiple"],
29                options=[i["title"] for i in data["options"]])
30
31    @property
32    def options(self):
33        """
34        a list of strings, which are options for poll
35        """
36        return self._options
37
38    @property
39    def expires_in(self):
40        """
41        timedelta, which represents time in which poll will expire
42        """
43        return self._expires_in
44
45    @property
46    def multiple(self):
47        """
48        boolean flag, True if poll allows multiple choices
49        """
50        return self._multiple
51
52    @multiple.setter
53    def multiple(self, flag: bool):
54        self._multiple = flag
55
56    @property
57    def hide_totals(self):
58        """
59        boolean flag, True if counts are hidden until poll ends
60        """
61        return self._hide_totals
62
63    @hide_totals.setter
64    def hide_totals(self, flag: bool):
65        self._hide_totals = flag
66
67    def __repr__(self):
68        return f"PleromaPoll(options={self._options},\
69expires_in={self._expires_in},\
70multiple={self._multiple},\
71hide_totals={self._hide_totals})"