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