From 42f75f9a6700c2730208d5e79d405f0e30588640 Mon Sep 17 00:00:00 2001 From: Lucas Brandstaetter <lucas@brandstaetter.tech> Date: Sun, 20 Oct 2024 23:33:53 +0000 Subject: [PATCH] Add interactive mode to create_conference command When the name is given all other attributes are generated automatically. The user can use --interactive to be asked for missing arguments. --- .../management/commands/create_conference.py | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/management/commands/create_conference.py b/src/core/management/commands/create_conference.py index 33121260e..986a0fe5c 100644 --- a/src/core/management/commands/create_conference.py +++ b/src/core/management/commands/create_conference.py @@ -4,7 +4,8 @@ from datetime import datetime from zoneinfo import ZoneInfo from django.core.management import call_command -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError +from django.utils.text import slugify from core.models.assemblies import Assembly from core.models.conference import Conference @@ -351,25 +352,37 @@ class Command(BaseCommand): '--slug', type=str, help='The url slug of the new conference. If not given, it will be slugified from the name.', + default=None, ) parser.add_argument( '-y', '--year', type=_validate_date, - help='The year of the CCC congress. If not given, it will use the current year.', + help=( + 'The year of the CCC congress. If not given, it will use the current year.' + ' Setting this to 0 will skip any year-specific setup (e.g., nav entries).' + ), + default=None, ) parser.add_argument( '--bootstrap', action=BooleanOptionalAction, - help=("Initialize the conference permission groups (e.g. 'Assembly-Teams')."), + help=("Initialize the conference permission groups (e.g. 'Assembly-Teams'). If not given, execution is determined by group presence in database."), ) + parser.add_argument('-i', '--interactive', action='store_true', help='Ask for missing arguments', default=False) def handle(self, *args, **options): - if not (slug := options.get('slug')): - slug = input('SLUG of the new conference, e.g. "42c3": ') + interactive = options.get('interactive', False) if not (name := options.get('name')): + if not interactive: + self.print_help('manage.py', 'create_conference') + raise CommandError('Missing required argument: name. Use --interactive to be asked for missing arguments.') name = input('NAME of the new conference (long title): ') - if not (year := options.get('year')): + if not (slug := options.get('slug')) and interactive: + slug = input('SLUG of the new conference, e.g. "42c3": ') + elif not (slug := options.get('slug')): + slug = slugify(name) + if not (year := options.get('year')) and interactive: year_valid = False while not year_valid: year = input("YEAR of the CCC congress (leave empty if it's something else): ") @@ -378,6 +391,8 @@ class Command(BaseCommand): year_valid = True except ValueError: self.stderr.write('Invalid year format. Please use YYYY.') + elif not (year := options.get('year')): + year = datetime.now() # noqa: DTZ005 year = year.year print(f'Creating conference {slug} ({name}) for year {year}') -- GitLab