Changeset 69dd60c in python-cinema-club-bot for predicates.py


Ignore:
Timestamp:
Oct 11, 2024, 1:53:24 AM (5 weeks ago)
Author:
Mikhail Kirillov <w96k@…>
Branches:
master
Children:
ac7b16a
Parents:
694d823
Message:

Task #30. Allow anyone create or edit upcoming event

File:
1 edited

Legend:

Unmodified
Added
Removed
  • predicates.py

    r694d823 r69dd60c  
    1212from telegram.ext import ContextTypes
    1313from datetime import datetime
    14 
    15 from utils import normalize_username
     14from typing import Optional
    1615
    1716
    18 def is_past(dt: datetime) -> bool:
     17def is_past(dt: Optional[datetime]) -> bool:
     18    if dt is None:
     19        return False
     20
    1921    return dt < datetime.today()
    2022
    2123
    22 def has_finished_event(
    23         context: ContextTypes.DEFAULT_TYPE,
    24         username: str
    25 ) -> bool:
     24def has_finished_movie(context: ContextTypes.DEFAULT_TYPE) -> bool:
     25    events = context.chat_data["events"]
     26    movies = context.chat_data["movies"]
     27
     28    last_event = events[-1] if events != [] else None
     29    last_movie = movies[-1] if movies != [] else None
     30
     31    return last_event is not None and last_movie is not None and \
     32        last_event["user"] == last_movie["user"] and \
     33        is_past(last_event["when"])
     34
     35
     36def has_event_without_movie(context: ContextTypes.DEFAULT_TYPE) -> bool:
    2637    events = context.chat_data["events"]
    2738    last_event = events[-1] if events != [] else None
    2839
    29     return last_event is not None and \
    30         last_event["user"] == normalize_username(username) and \
    31         is_past(last_event["when"])
     40    return last_event and last_event["movie"] is None
    3241
    3342
     
    3645    last_event = events[-1] if events != [] else None
    3746
    38     return False if last_event is None else not is_past(last_event["when"])
     47    if last_event is None:
     48        return False
     49
     50    if last_event["movie"] is None:
     51        return False
     52
     53    return not is_past(last_event["when"])
     54
Note: See TracChangeset for help on using the changeset viewer.