From 69dd60cc024063daf1591168a64a32e771bd7f1b Mon Sep 17 00:00:00 2001 From: Mikhail Kirillov Date: Fri, 11 Oct 2024 01:53:24 +0400 Subject: Task #30. Allow anyone create or edit upcoming event --- commands/event.py | 89 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 40 deletions(-) (limited to 'commands/event.py') diff --git a/commands/event.py b/commands/event.py index e06ea83..034ce36 100644 --- a/commands/event.py +++ b/commands/event.py @@ -13,12 +13,12 @@ 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 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_USER_HAD_EVENT, NO_EVENTS + EVENT_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET async def create_event( @@ -26,18 +26,18 @@ async def create_event( context: ContextTypes.DEFAULT_TYPE ) -> None: context_init(context) + users = context.chat_data["users"] 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: + 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) @@ -61,10 +61,10 @@ async def create_event( if arguments_where == [] and events == []: raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED) - event_where = " ".join(arguments_where) + old_when = events[-2]["when"] if len(events) >= 2 else None + old_where = events[-2]["where"] if len(events) >= 2 else None - if event_where == "": - event_where = events[-1]["where"] + event_where = " ".join(arguments_where) if arguments_where != [] else old_where event_dict = dict( when=event_when, @@ -73,28 +73,17 @@ async def create_event( 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, - movie=event_dict["movie"], - ) - ) - else: - old_when = events[-1]["when"] - old_where = events[-1]["where"] - events[-1] = 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"], - ) + events[-1] = 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( @@ -103,21 +92,41 @@ async def list_events( ) -> 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_finished = "FINISHED" \ + event_status = "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 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 -- cgit v1.2.3