Changeset 69dd60c in python-cinema-club-bot for commands/event.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
  • commands/event.py

    r694d823 r69dd60c  
    1414from datetime import datetime
    1515
    16 from utils import context_init
    17 from predicates import is_past, has_finished_event
     16from utils import context_init, normalize_username
     17from predicates import is_past, has_finished_movie, has_event_without_movie
    1818from strings import EVENT_ARGS_NOT_PROVIDED, EVENT_INVALID_DATETIME, \
    1919    EVENT_CREATED, EVENT_EDITED, EVENT_CANT_BE_IN_PAST, \
    2020    EVENT_WHERE_NOT_PROVIDED, EVENT_MOVIE_NOT_CHOOSEN, EVENTS_LIST, \
    21     EVENT_USER_HAD_EVENT, NO_EVENTS
     21    EVENT_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET
    2222
    2323
     
    2727) -> None:
    2828    context_init(context)
     29    users = context.chat_data["users"]
    2930    events = context.chat_data["events"]
    3031    movies = context.chat_data["movies"]
    3132
    32     username = update.message.from_user.username
    3333    last_movie = movies[-1] if movies != [] else None
    3434
    35     if has_finished_event(context, username):
    36         raise error.TelegramError(EVENT_USER_HAD_EVENT)
     35    if last_movie is None:
     36        raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
    3737
    38     if last_movie is None or last_movie["user"] != username:
    39         raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)
     38    if has_finished_movie(context):
     39        raise error.TelegramError(EVENT_FINISHED)
    4040
    4141    if context.args == []:
     
    6262        raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED)
    6363
    64     event_where = " ".join(arguments_where)
     64    old_when = events[-2]["when"] if len(events) >= 2 else None
     65    old_where = events[-2]["where"] if len(events) >= 2 else None
    6566
    66     if event_where == "":
    67         event_where = events[-1]["where"]
     67    event_where = " ".join(arguments_where) if arguments_where != [] else old_where
    6868
    6969    event_dict = dict(
     
    7474    )
    7575
    76     if events == [] or is_past(events[-1]["when"]):
    77         events.append(event_dict)
    78         await update.message.reply_text(
    79             EVENT_CREATED.format(
    80                 when=event_when,
    81                 movie=event_dict["movie"],
    82             )
     76    events[-1] = event_dict
     77
     78    await update.message.reply_text(
     79        EVENT_EDITED.format(
     80            old_when=old_when,
     81            old_where=old_where,
     82            when=event_when,
     83            where=event_where,
     84            movie=event_dict["movie"],
    8385        )
    84     else:
    85         old_when = events[-1]["when"]
    86         old_where = events[-1]["where"]
    87         events[-1] = event_dict
    88 
    89         await update.message.reply_text(
    90             EVENT_EDITED.format(
    91                 old_when=old_when,
    92                 old_where=old_where,
    93                 when=event_when,
    94                 where=event_where,
    95                 movie=event_dict["movie"],
    96             )
    97         )
     86    )
    9887
    9988
     
    10493    context_init(context)
    10594
     95    users = context.chat_data["users"]
    10696    events = context.chat_data["events"]
     97    last_event = events[-1] if events != [] else None
     98
     99    if last_event and last_event["movie"] is None:
     100        last_event["user"] = normalize_username(users[0])
    107101
    108102    events_formatted: str = ""
    109103
    110104    for event in events:
    111         event_finished = "FINISHED" \
     105        event_status = "FINISHED" \
    112106            if is_past(event["when"]) else "PLANNED"
    113107
    114         events_formatted += EVENTS_LIST.format(
    115             movie=event["movie"],
    116             user=event["user"],
    117             when=event["when"],
    118             where=event["where"],
    119             finished=event_finished
    120         )
     108        if event["movie"] is None:
     109            breakpoint()
     110            event_status = "PLANNING"
     111            events_formatted += EVENTS_LIST_PLANNING.format(
     112                user=event["user"],
     113                status=event_status
     114            )
     115        elif event["when"] is None or event["where"] is None:
     116            event_status = "PLANNING"
     117            events_formatted += EVENTS_LIST_MOVIE_SET.format(
     118                user=event["user"],
     119                movie=event["movie"],
     120                status=event_status
     121            )
     122        else:
     123            events_formatted += EVENTS_LIST.format(
     124                movie=event["movie"],
     125                user=event["user"],
     126                when=event["when"],
     127                where=event["where"],
     128                status=event_status
     129            )
    121130
    122131    if events_formatted == "":
Note: See TracChangeset for help on using the changeset viewer.