source: python-cinema-club-bot/commands/event.py

Last change on this file was ac7b16a, checked in by Mikhail Kirillov <w96k@…>, on Oct 11, 2024 at 2:03:28 AM

fixup! Task #30. Allow anyone create or edit upcoming event

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
12from telegram import Update, error
13from telegram.ext import ContextTypes
14from datetime import datetime
15
16from utils import context_init, normalize_username
17from predicates import is_past, has_finished_movie, has_event_without_movie
18from 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_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET
22
23
24async def create_event(
25 update: Update,
26 context: ContextTypes.DEFAULT_TYPE
27) -> None:
28 context_init(context)
29 users = context.chat_data["users"]
30 events = context.chat_data["events"]
31 movies = context.chat_data["movies"]
32
33 last_movie = movies[-1] if movies != [] else None
34
35 if last_movie is None:
36 raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
37
38 if has_finished_movie(context):
39 raise error.TelegramError(EVENT_FINISHED)
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 old_when = events[-2]["when"] if len(events) >= 2 else None
65 old_where = events[-2]["where"] if len(events) >= 2 else None
66
67 event_where = " ".join(arguments_where) if arguments_where != [] else old_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 len(events) > 0:
77 events[-1] = event_dict
78 else:
79 events.append(event_dict)
80
81 await update.message.reply_text(
82 EVENT_EDITED.format(
83 old_when=old_when,
84 old_where=old_where,
85 when=event_when,
86 where=event_where,
87 movie=event_dict["movie"],
88 )
89 )
90
91
92async def list_events(
93 update: Update,
94 context: ContextTypes.DEFAULT_TYPE
95) -> None:
96 context_init(context)
97
98 users = context.chat_data["users"]
99 events = context.chat_data["events"]
100 last_event = events[-1] if events != [] else None
101
102 if last_event and last_event["movie"] is None:
103 last_event["user"] = normalize_username(users[0])
104
105 events_formatted: str = ""
106
107 for event in events:
108 event_status = "FINISHED" \
109 if is_past(event["when"]) else "PLANNED"
110
111 if event["movie"] is None:
112 breakpoint()
113 event_status = "PLANNING"
114 events_formatted += EVENTS_LIST_PLANNING.format(
115 user=event["user"],
116 status=event_status
117 )
118 elif event["when"] is None or event["where"] is None:
119 event_status = "PLANNING"
120 events_formatted += EVENTS_LIST_MOVIE_SET.format(
121 user=event["user"],
122 movie=event["movie"],
123 status=event_status
124 )
125 else:
126 events_formatted += EVENTS_LIST.format(
127 movie=event["movie"],
128 user=event["user"],
129 when=event["when"],
130 where=event["where"],
131 status=event_status
132 )
133
134 if events_formatted == "":
135 events_formatted = NO_EVENTS
136
137 await update.message.reply_text(events_formatted)
Note: See TracBrowser for help on using the repository browser.