Skip to content
Snippets Groups Projects
Commit af5a07cb authored by Roang's avatar Roang
Browse files

Add date validation for create_conference

parent 6ed61166
Branches
Tags
No related merge requests found
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_valid = False
while not year_valid:
year = input("YEAR of the CCC congress (leave empty if it's something else): ")
year = int(year or '0')
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment