summaryrefslogtreecommitdiff
path: root/commands/event.py
blob: e06ea831e0d137f673b8bd8ac07d5015bc552214 (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
# 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
from predicates import is_past, has_finished_event
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_USER_HAD_EVENT, NO_EVENTS


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

    username = update.message.from_user.username
    last_movie = movies[-1] if movies != [] else None

    if has_finished_event(context, username):
        raise error.TelegramError(EVENT_USER_HAD_EVENT)

    if last_movie is None or last_movie["user"] != username:
        raise error.TelegramError(EVENT_MOVIE_NOT_CHOOSEN)

    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)

    event_where = " ".join(arguments_where)

    if event_where == "":
        event_where = events[-1]["where"]

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

    if events == [] or is_past(events[-1]["when"]):
        events.append(event_dict)
        await update.message.reply_text(
            EVENT_CREATED.format(
                when=event_when,
                movie=event_dict["movie"],
            )
        )
    else:
        old_when = events[-1]["when"]
        old_where = events[-1]["where"]
        events[-1] = 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)

    events = context.chat_data["events"]

    events_formatted: str = ""

    for event in events:
        event_finished = "FINISHED" \
            if is_past(event["when"]) else "PLANNED"

        events_formatted += EVENTS_LIST.format(
            movie=event["movie"],
            user=event["user"],
            when=event["when"],
            where=event["where"],
            finished=event_finished
        )

    if events_formatted == "":
        events_formatted = NO_EVENTS

    await update.message.reply_text(events_formatted)