# 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 from predicates import is_past, has_finished_event 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_USER_HAD_EVENT, NO_EVENTS async def create_event( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: context_init(context) events = context.chat_data["events"] movies = context.chat_data["movies"] username = update.message.from_user.username last_movie = movies[-1] if movies != [] else None if has_finished_event(context, username): raise error.TelegramError(EVENT_USER_HAD_EVENT) if last_movie is None or last_movie["user"] != username: raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN) 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) event_where = " ".join(arguments_where) if event_where == "": event_where = events[-1]["where"] event_dict = dict( when=event_when, where=event_where, movie=last_movie["title"], user=last_movie["user"] ) if events == [] or is_past(events[-1]["when"]): events.append(event_dict) await update.message.reply_text(EVENT_CREATED.format(when=event_when)) else: events[-1] = event_dict await update.message.reply_text(EVENT_EDITED.format(when=event_when)) async def list_events( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: context_init(context) events = context.chat_data["events"] movies = context.chat_data["movies"] events_formatted: str = "" for event in events: event_finished = "FINISHED" \ if is_past(event["when"]) else "PLANNED" events_formatted += EVENTS_LIST.format( movie=event["movie"], user=event["user"], when=event["when"], where=event["where"], finished=event_finished ) if events_formatted == "": events_formatted = NO_EVENTS await update.message.reply_text(events_formatted)