|
|
|
@ -11,6 +11,12 @@ if typing.TYPE_CHECKING:
|
|
|
|
|
from bot import Bot |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_to_date(x: typing.Union[int, date]) -> date: |
|
|
|
|
if isinstance(x, int): |
|
|
|
|
return date.fromordinal(x) |
|
|
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
|
|
class Community: |
|
|
|
|
chat_id: int # Telegram chat id |
|
|
|
@ -26,6 +32,10 @@ class Community:
|
|
|
|
|
|
|
|
|
|
def dict(self) -> dict: |
|
|
|
|
data = asdict(self) |
|
|
|
|
data['polled'] = {str(k): self.polled[k].toordinal() for k in self.polled} |
|
|
|
|
data['default_answers'] = {str(k): self.default_answers[k] for k in self.default_answers} |
|
|
|
|
data['scheduled_meetings'] = [(l, m, r.toordinal()) for l, m, r in self.scheduled_meetings] |
|
|
|
|
data['archived_meetings'] = [(l, m, r.toordinal()) for l, m, r in self.archived_meetings] |
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@ -35,6 +45,10 @@ class Community:
|
|
|
|
|
key in ['chat_id', 'name', 'members', 'pool', 'polled', 'scheduled_meetings', |
|
|
|
|
'archived_meetings', 'start_timestamp', 'default_answers']} |
|
|
|
|
self = cls(**data_) |
|
|
|
|
self.polled = {int(k): convert_to_date(self.polled[k]) for k in self.polled} |
|
|
|
|
self.default_answers = {int(k): self.default_answers[k] for k in self.default_answers} |
|
|
|
|
self.scheduled_meetings = [(int(l), int(m), convert_to_date(r)) for l, m, r in self.scheduled_meetings] |
|
|
|
|
self.archived_meetings = [(int(l), int(m), convert_to_date(r)) for l, m, r in self.archived_meetings] |
|
|
|
|
return self |
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@ -66,8 +80,10 @@ class Community:
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
def add_answer(self, user_id: int, answer: bool, bot: Bot): |
|
|
|
|
answer = bool(answer) |
|
|
|
|
if user_id not in self.users_to_poll(): |
|
|
|
|
return |
|
|
|
|
if user_id in self.pool: |
|
|
|
|
self.pool.remove(user_id) |
|
|
|
|
self.pool += [user_id] * answer |
|
|
|
|
self.default_answers[user_id] = bool(answer) |
|
|
|
|