1 | # This file is part of python-cinema-club-bot
|
---|
2 | # contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
|
---|
3 |
|
---|
4 | # To the extent possible under law, the author(s) have dedicated all copyright
|
---|
5 | # and related and neighboring rights to this software to the public domain
|
---|
6 | # worldwide. This software is distributed without any warranty.
|
---|
7 |
|
---|
8 | # You should have received a copy of the CC0 Public Domain Dedication along
|
---|
9 | # with this software. If not, see:
|
---|
10 | # <http://creativecommons.org/publicdomain/zero/1.0/>
|
---|
11 |
|
---|
12 | from telegram import Update, error
|
---|
13 | from telegram.ext import ContextTypes
|
---|
14 | from datetime import datetime
|
---|
15 |
|
---|
16 | from utils import context_init
|
---|
17 | from predicates import is_past, has_finished_event
|
---|
18 | from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \
|
---|
19 | EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \
|
---|
20 | EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \
|
---|
21 | EVENT_USER_HAD_EVENT, NO_EVENTS
|
---|
22 |
|
---|
23 |
|
---|
24 | async def create_event(
|
---|
25 | update: Update,
|
---|
26 | context: ContextTypes.DEFAULT_TYPE
|
---|
27 | ) -> None:
|
---|
28 | context_init(context)
|
---|
29 | events = context.chat_data["events"]
|
---|
30 | movies = context.chat_data["movies"]
|
---|
31 |
|
---|
32 | username = update.message.from_user.username
|
---|
33 | last_movie = movies[-1] if movies != [] else None
|
---|
34 |
|
---|
35 | if has_finished_event(context, username):
|
---|
36 | raise error.TelegramError(EVENT_USER_HAD_EVENT)
|
---|
37 |
|
---|
38 | if last_movie is None or last_movie["user"] != username:
|
---|
39 | raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
|
---|
40 |
|
---|
41 | if context.args == []:
|
---|
42 | raise error.TelegramError(EVENT_ARGS_NOT_PROVIDED)
|
---|
43 |
|
---|
44 | argument_when = context.args[0]
|
---|
45 |
|
---|
46 | try:
|
---|
47 | event_when = datetime.strptime(argument_when, "%d.%m.%Y/%H:%M")
|
---|
48 | except ValueError:
|
---|
49 | raise error.TelegramError(EVENT_INVALID_DATETIME)
|
---|
50 |
|
---|
51 | if is_past(event_when):
|
---|
52 | raise error.TelegramError(
|
---|
53 | EVENT_CANT_BE_IN_PAST.format(
|
---|
54 | when=event_when,
|
---|
55 | today=datetime.today(),
|
---|
56 | )
|
---|
57 | )
|
---|
58 |
|
---|
59 | arguments_where = context.args[1:]
|
---|
60 |
|
---|
61 | if arguments_where == [] and events == []:
|
---|
62 | raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED)
|
---|
63 |
|
---|
64 | event_where = " ".join(arguments_where)
|
---|
65 |
|
---|
66 | if event_where == "":
|
---|
67 | event_where = events[-1]["where"]
|
---|
68 |
|
---|
69 | event_dict = dict(
|
---|
70 | when=event_when,
|
---|
71 | where=event_where,
|
---|
72 | movie=last_movie["title"],
|
---|
73 | user=last_movie["user"]
|
---|
74 | )
|
---|
75 |
|
---|
76 | if events == [] or is_past(events[-1]["when"]):
|
---|
77 | events.append(event_dict)
|
---|
78 | await update.message.reply_text(EVENT_CREATED.format(when=event_when))
|
---|
79 | else:
|
---|
80 | events[-1] = event_dict
|
---|
81 | await update.message.reply_text(EVENT_EDITED.format(when=event_when))
|
---|
82 |
|
---|
83 |
|
---|
84 | async def list_events(
|
---|
85 | update: Update,
|
---|
86 | context: ContextTypes.DEFAULT_TYPE
|
---|
87 | ) -> None:
|
---|
88 | context_init(context)
|
---|
89 |
|
---|
90 | events = context.chat_data["events"]
|
---|
91 | movies = context.chat_data["movies"]
|
---|
92 |
|
---|
93 | events_formatted: str = ""
|
---|
94 |
|
---|
95 | for event in events:
|
---|
96 | event_finished = "FINISHED" \
|
---|
97 | if is_past(event["when"]) else "PLANNED"
|
---|
98 |
|
---|
99 | events_formatted += EVENTS_LIST.format(
|
---|
100 | movie=event["movie"],
|
---|
101 | user=event["user"],
|
---|
102 | when=event["when"],
|
---|
103 | where=event["where"],
|
---|
104 | finished=event_finished
|
---|
105 | )
|
---|
106 |
|
---|
107 | if events_formatted == "":
|
---|
108 | events_formatted = NO_EVENTS
|
---|
109 |
|
---|
110 | await update.message.reply_text(events_formatted)
|
---|