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 --- predicates.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'predicates.py') diff --git a/predicates.py b/predicates.py index c341ccf..07b84d7 100644 --- a/predicates.py +++ b/predicates.py @@ -11,28 +11,44 @@ from telegram.ext import ContextTypes from datetime import datetime +from typing import Optional -from utils import normalize_username +def is_past(dt: Optional[datetime]) -> bool: + if dt is None: + return False -def is_past(dt: datetime) -> bool: return dt < datetime.today() -def has_finished_event( - context: ContextTypes.DEFAULT_TYPE, - username: str -) -> bool: +def has_finished_movie(context: ContextTypes.DEFAULT_TYPE) -> bool: events = context.chat_data["events"] + movies = context.chat_data["movies"] + last_event = events[-1] if events != [] else None + last_movie = movies[-1] if movies != [] else None - return last_event is not None and \ - last_event["user"] == normalize_username(username) and \ + return last_event is not None and last_movie is not None and \ + last_event["user"] == last_movie["user"] and \ is_past(last_event["when"]) +def has_event_without_movie(context: ContextTypes.DEFAULT_TYPE) -> bool: + events = context.chat_data["events"] + last_event = events[-1] if events != [] else None + + return last_event and last_event["movie"] is None + + def has_unfinished_event(context: ContextTypes.DEFAULT_TYPE) -> bool: events = context.chat_data["events"] last_event = events[-1] if events != [] else None - return False if last_event is None else not is_past(last_event["when"]) + if last_event is None: + return False + + if last_event["movie"] is None: + return False + + return not is_past(last_event["when"]) + -- cgit v1.2.3