Changeset 69dd60c


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

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • commands/announce.py

    r694d823 r69dd60c  
    1313from telegram.ext import ContextTypes
    1414
    15 from strings import ANNOUNCEMENT_TEMPLATE, ANNOUNCEMENT_FINISHED_EVENT
     15from strings import ANNOUNCEMENT_TEMPLATE, ANNOUNCEMENT_FINISHED_EVENT, \
     16    ANNOUNCEMENT_EVENT_NEEDS_EDITING
    1617from utils import context_init, create_users_string, choose_next_user
    1718from predicates import has_unfinished_event
     
    3233        last_movie = movies[-1]
    3334
    34         genres = " #".join(last_movie["genres"])
     35        if last_event["when"] is None or last_event["where"] is None or last_event["movie"] is None:
     36            raise error.TelegramError(ANNOUNCEMENT_EVENT_NEEDS_EDITING)
     37
     38        genres ="#" + " #".join(last_movie["genres"])
    3539
    3640        await update.message.reply_markdown(
  • 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 == "":
  • commands/movie.py

    r694d823 r69dd60c  
    1414from imdb import Cinemagoer
    1515
    16 from utils import context_init, choose_next_user
    17 from predicates import has_finished_event
     16from utils import context_init, choose_next_user, normalize_username, \
     17    init_new_event
     18from predicates import has_finished_movie
    1819from strings import MOVIE_NOT_PROVIDED, EXPECTED_ONE_MOVIE, \
    1920    MOVIE_ANOTHER_USER, FETCHING_MOVIE, FETCHING_ERROR, \
     
    3839    username = update.message.from_user.username
    3940
    40     if has_finished_event(context, chooser):
    41         users = context.chat_data["users"] = choose_next_user(users)
    42         raise error.TelegramError(MOVIE_ANOTHER_USER.format(users[0]))
     41    if has_finished_movie(context):
     42        users = context.chat_data["users"] = choose_next_user(context)
    4343
    4444    if "@"+username != chooser:
     
    6464        id=movie.getID(),
    6565        user=update.effective_user.username,
    66         poster=movie.data.get("cover url"),
    6766        rating=movie.data.get("rating"),
    6867        genres=movie.data.get("genres"),
     
    8180            context.chat_data["events"][-1] = last_event
    8281    else:
     82        init_new_event(context, users[0], movie_dict["title"])
    8383        context.chat_data["movies"].append(movie_dict)
    8484
     
    101101        movies_formatted += MOVIES_LIST.format(
    102102            title=movie["title"],
    103             id=movie["id"],
    104103            user=movie["user"]
    105104        )
  • commands/users.py

    r694d823 r69dd60c  
    1717    EXPECTED_ONE_USER, USER_SET, USER_REMOVE, ADD_MORE_USERS, \
    1818    NEXT_MOVIE_USER, USER_NOT_FOUND, USER_CHOOSE, NO_USERS, \
    19     EVENT_USER_HAD_EVENT, NEXT_MOVIE
    20 from utils import context_init, create_users_string, choose_next_user
    21 from predicates import has_finished_event
     19    NEXT_MOVIE
     20from utils import context_init, create_users_string, choose_next_user, \
     21    normalize_username
     22from predicates import has_finished_movie, has_event_without_movie
    2223
    2324
     
    6162    if users == []:
    6263        await update.message.reply_text(NO_USERS)
    63     else:
    64         if has_finished_event(context, users[0]):
    65             users = context.chat_data["users"] = choose_next_user(users)
     64        return
    6665
    67         await update.message.reply_markdown(create_users_string(users))
     66    if has_finished_movie(context):
     67        users = context.chat_data["users"] = choose_next_user(context)
     68
     69    await update.message.reply_markdown(create_users_string(users))
    6870
    6971
     
    8385        raise error.TelegramError(ADD_MORE_USERS)
    8486
    85     if has_finished_event(context, users[0]):
    86         users = context.chat_data["users"] = choose_next_user(users)
     87    if has_finished_movie(context):
     88        users = context.chat_data["users"] = choose_next_user(context)
    8789
    8890    next_user_string = NEXT_MOVIE_USER.format(user=users[0])
    8991
    90     last_movie = context.chat_data["movies"][0] \
     92    last_movie = context.chat_data["movies"][-1] \
    9193        if context.chat_data["movies"] != [] else None
    9294
    93     if last_movie and last_movie["user"] == users[0]:
     95    if last_movie and last_movie["user"] == normalize_username(users[0]):
    9496        next_user_string += NEXT_MOVIE.format(movie=last_movie["title"])
    9597
     
    142144    context.chat_data["users"] = users
    143145
    144     if has_finished_event(context, users[0]):
    145         await update.message.reply_text(
    146             EVENT_USER_HAD_EVENT.format(user=users[0])
    147         )
    148         users = context.chat_data["users"] = choose_next_user(users)
     146    if has_finished_movie(context):
     147        users = context.chat_data["users"] = choose_next_user(context)
     148
     149    if has_event_without_movie(context):
     150        context.chat_data["events"][-1]["user"] = users[0]
    149151
    150152    await update.message.reply_text(USER_CHOOSE.format(user=users[0]))
  • 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
  • strings.py

    r694d823 r69dd60c  
    2020MOVIE_REMOVE = "Movie \"{title}\" with id {id} has been removed"
    2121MOVIE_SET = "Movie \"{title}\" proposed by {user} is succesfully set as next to watch"
    22 MOVIES_LIST = "\"{title}\" IMDB:{id} by {user} \n"
     22MOVIES_LIST = "\"{title}\" by {user} \n"
    2323NO_MOVIES = "No movies.  You can add movie if you are next to choose (see /next). Too add a movie use /movie <imdb_id>"
    2424
     
    5555EVENT_CREATED = "Event \"{movie}\" at {when} was created"
    5656EVENT_EDITED = "Event \"{movie}\" at {old_when} in {old_where} was modified to {when} in {where}"
    57 EVENTS_LIST = "[{finished}] \"{movie}\" by {user} at {when} in {where}\n"
     57EVENTS_LIST = "[{status}] \"{movie}\" by {user} at {when} in {where}\n"
     58EVENTS_LIST_MOVIE_SET = "[{status}] \"{movie}\" by {user}\n"
     59EVENTS_LIST_PLANNING = "[{status}] by {user}\n"
    5860EVENT_MOVIE_NOT_CHOOSEN = "You should choose a film first using /movie <imdb_id>"
    5961EVENT_MOVIE_NOT_SET = "<None>"
    60 EVENT_USER_HAD_EVENT = "User {user} have already finished event, so he can't choose twice. See /next or /list"
     62EVENT_USER_HAD_MOVIE = "User {user} can't set the sa, so he can't choose twice. See /next or /list"
     63EVENT_FINISHED = "Event is finished. Use /next to see who chooses a film now."
    6164NO_EVENTS = "No events."
    6265
    6366UNDEFINED_ERROR = "Exception: something unexpected happened. Check the logs."
    6467
     68ANNOUNCEMENT_EVENT_NEEDS_EDITING = "Event is missing either Movie, When or Where fields. See /event"
    6569ANNOUNCEMENT_FINISHED_EVENT = "There is no planned events. Create one using /event <when> <where>"
    6670ANNOUNCEMENT_TEMPLATE = """
  • utils.py

    r694d823 r69dd60c  
    1212from telegram.ext import ContextTypes
    1313from collections import deque
     14from typing import Optional
    1415
    1516def context_init(context: ContextTypes.DEFAULT_TYPE):
     
    3839
    3940
    40 def choose_next_user(users: list[dict]) -> list[dict]:
    41     users = deque(users)
     41def choose_next_user(context: ContextTypes.DEFAULT_TYPE) -> list[dict]:
     42    users = deque(context.chat_data["users"])
    4243    users.rotate(-1)  # -1 moves list to left by 1 element
    4344
     45    init_new_event(context, users[0])
     46
    4447    return list(users)
     48
     49
     50def init_new_event(context: ContextTypes.DEFAULT_TYPE, user: dict, movie: Optional[str] = None):
     51    events = context.chat_data["events"]
     52
     53    last_event = events[-1] if events != [] else None
     54
     55    init_event = dict(
     56        when=None,
     57        where=None,
     58        movie=movie,
     59        user=user
     60    )
     61
     62    if last_event and last_event["movie"] is None:
     63        events[-1] = init_event
     64    else:
     65        events.append(init_event)
Note: See TracChangeset for help on using the changeset viewer.