summaryrefslogtreecommitdiff
path: root/commands/event.py
blob: b4839edb3d85b09663780d9b5b9249688b99539d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# This file is part of python-cinema-club-bot
# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>

# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.

# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see:
# <http://creativecommons.org/publicdomain/zero/1.0/>

from telegram import Update, error
from telegram.ext import ContextTypes
from datetime import datetime

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_FINISHED, NO_EVENTS, EVENTS_LIST_PLANNING, EVENTS_LIST_MOVIE_SET


async def create_event(
        update: Update,
        context: ContextTypes.DEFAULT_TYPE
) -> None:
    context_init(context)
    users = context.chat_data["users"]
    events = context.chat_data["events"]
    movies = context.chat_data["movies"]

    last_movie = movies[-1] if movies != [] else None

    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)

    argument_when = context.args[0]

    try:
        event_when = datetime.strptime(argument_when, "%d.%m.%Y/%H:%M")
    except ValueError:
        raise error.TelegramError(EVENT_INVALID_DATETIME)

    if is_past(event_when):
        raise error.TelegramError(
            EVENT_CANT_BE_IN_PAST.format(
                when=event_when,
                today=datetime.today(),
            )
        )

    arguments_where = context.args[1:]

    if arguments_where == [] and events == []:
        raise error.TelegramError(EVENT_WHERE_NOT_PROVIDED)

    old_when = events[-2]["when"] if len(events) >= 2 else None
    old_where = events[-2]["where"] if len(events) >= 2 else None

    event_where = " ".join(arguments_where) if arguments_where != [] else old_where

    event_dict = dict(
        when=event_when,
        where=event_where,
        movie=last_movie["title"],
        user=last_movie["user"]
    )

    if len(events) > 0:
        events[-1] = event_dict
    else:
        events.append(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(
        update: Update,
        context: ContextTypes.DEFAULT_TYPE
) -> 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_status = "FINISHED" \
            if is_past(event["when"]) else "PLANNED"

        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

    await update.message.reply_text(events_formatted)