diff --git a/src/core/management/commands/create_conference.py b/src/core/management/commands/create_conference.py index 44a930beaaeb2b3be75c7424d5c99c86fb5ecf5d..340d0fb14bd07d91f8628af3eb09630de39789b8 100644 --- a/src/core/management/commands/create_conference.py +++ b/src/core/management/commands/create_conference.py @@ -1,4 +1,4 @@ -from argparse import BooleanOptionalAction +from argparse import ArgumentTypeError, BooleanOptionalAction from datetime import datetime from zoneinfo import ZoneInfo @@ -331,6 +331,13 @@ def seed_conference(conf: Conference, year: int = 0, force: bool = False): ) +def _validate_date(s: str) -> datetime: + try: + return datetime.strptime(s, '%Y') # noqa: DTZ007 + except ValueError as exc: + raise ArgumentTypeError(f"Not a valid date: {s!r}. Use format 'YYYY'") from exc + + class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( @@ -348,7 +355,7 @@ class Command(BaseCommand): parser.add_argument( '-y', '--year', - type=int, + type=_validate_date, help='The year of the CCC congress. If not given, it will use the current year.', ) parser.add_argument( @@ -363,9 +370,15 @@ class Command(BaseCommand): if not (name := options.get('name')): name = input('NAME of the new conference (long title): ') if not (year := options.get('year')): - year = input("YEAR of the CCC congress (leave empty if it's something else): ") - year = int(year or '0') - + year_valid = False + while not year_valid: + year = input("YEAR of the CCC congress (leave empty if it's something else): ") + try: + year = datetime.strptime(year, '%Y') # noqa: DTZ007 + year_valid = True + except ValueError: + self.stderr.write('Invalid year format. Please use YYYY.') + year = year.year print(f'Creating conference {slug} ({name}) for year {year}') # load our bootstrap fixtures