# This file is part of python-cinema-club-bot # contributed in 2024 by Mikhail Kirillov (~w96k) # To the extent possible under law, the author(s) have dedicated all copyright # and related and neighboring rights to this software to the public domain # worldwide. This software is distributed without any warranty. # You should have received a copy of the CC0 Public Domain Dedication along # with this software. If not, see: # from telegram import Update, error from telegram.ext import ContextTypes from datetime import datetime from utils import context_init, normalize_username from predicates import is_past, has_finished_movie, has_event_without_movie from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \ EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \ EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \ EVENT_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET async def create_event( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: context_init(context) users = context.chat_data["users"] events = context.chat_data["events"] movies = context.chat_data["movies"] last_movie = movies[-1] if movies != [] else None if last_movie is None: raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN) if has_finished_movie(context): raise error.TelegramError(EVENT_FINISHED) if context.args == []: raise error.TelegramError(EVENT_ARGS_NOT_PROVIDED) argument_when = context.args[0] try: event_when = datetime.strptime(argument_when, "%d.%m.%Y/%H:%M") except ValueError: raise error.TelegramError(EVENT_INVALID_DATETIME) if is_past(event_when): raise error.TelegramError( EVENT_CANT_BE_IN_PAST.format( when=event_when, today=datetime.today(), ) ) arguments_where = context.args[1:] if arguments_where == [] and events == []: raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED) old_when = events[-2]["when"] if len(events) >= 2 else None old_where = events[-2]["where"] if len(events) >= 2 else None event_where = " ".join(arguments_where) if arguments_where != [] else old_where event_dict = dict( when=event_when, where=event_where, movie=last_movie["title"], user=last_movie["user"] ) if len(events) > 0: events[-1] = event_dict else: events.append(event_dict) await update.message.reply_text( EVENT_EDITED.format( old_when=old_when, old_where=old_where, when=event_when, where=event_where, movie=event_dict["movie"], ) ) async def list_events( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: context_init(context) users = context.chat_data["users"] events = context.chat_data["events"] last_event = events[-1] if events != [] else None if last_event and last_event["movie"] is None: last_event["user"] = normalize_username(users[0]) events_formatted: str = "" for event in events: event_status = "FINISHED" \ if is_past(event["when"]) else "PLANNED" if event["movie"] is None: breakpoint() event_status = "PLANNING" events_formatted += EVENTS_LIST_PLANNING.format( user=event["user"], status=event_status ) elif event["when"] is None or event["where"] is None: event_status = "PLANNING" events_formatted += EVENTS_LIST_MOVIE_SET.format( user=event["user"], movie=event["movie"], status=event_status ) else: events_formatted += EVENTS_LIST.format( movie=event["movie"], user=event["user"], when=event["when"], where=event["where"], status=event_status ) if events_formatted == "": events_formatted = NO_EVENTS await update.message.reply_text(events_formatted)