Skip to content

Commit

Permalink
Compute allowed years dynamically
Browse files Browse the repository at this point in the history
Closes #51
  • Loading branch information
olegeech-me committed Nov 9, 2024
1 parent 29d7052 commit 7271d7e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/bot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
IETF_LANGUAGE_MAP = {"en": "EN", "ru": "RU", "cs": "CZ", "uk": "UA"}
ALLOWED_TYPES = ["CD", "DO", "DP", "DV", "MK", "PP", "ST", "TP", "VP", "ZK", "ZM"]
POPULAR_ALLOWED_TYPES = ["DP", "TP", "ZM", "ST", "MK", "DV"]
ALLOWED_YEARS = [y for y in range(datetime.datetime.today().year - 3, datetime.datetime.today().year + 1)]


START, NUMBER, TYPE, YEAR, VALIDATE = range(5)
Expand All @@ -33,6 +32,11 @@
db = loader.db
rabbit = loader.rabbit

def get_allowed_years():
"""Compute allowed years to select"""
current_year = datetime.datetime.today().year
return [y for y in range(current_year - 3, current_year + 1)]


async def _set_menu_commands(update: Update, context: ContextTypes.DEFAULT_TYPE, lang="EN"):
"""Sets available bot menu commands"""
Expand Down Expand Up @@ -232,7 +236,7 @@ def _parse_application_number_full(num_str: str):
if not matched:
return
# check that type and number are among allowed
if matched[4] not in ALLOWED_TYPES or int(matched[5]) not in ALLOWED_YEARS:
if matched[4] not in ALLOWED_TYPES or int(matched[5]) not in get_allowed_years():
logger.info("Application type %s or year %s are unsupported", matched[4], matched[5])
return
return matched[2], (matched[3] or "0").lstrip("-"), matched[4], matched[5]
Expand Down Expand Up @@ -316,7 +320,7 @@ async def application_dialog_type(update: Update, context: ContextTypes.DEFAULT_
# XXX FIXME(fernflower) Later switch to i18n message
await query.edit_message_text(f"Unsupported application type {app_type}")
# Show keyboard for application year selection
keyboard = [[InlineKeyboardButton(str(year), callback_data=f"application_dialog_year_{year}") for year in ALLOWED_YEARS]]
keyboard = [[InlineKeyboardButton(str(year), callback_data=f"application_dialog_year_{year}") for year in get_allowed_years()]]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(message_texts[lang]["dialog_year"], reply_markup=reply_markup)
return YEAR
Expand Down

0 comments on commit 7271d7e

Please sign in to comment.