From 68a9cd40ae26f12ef87dbfd3f33c0aba1b4354f7 Mon Sep 17 00:00:00 2001 From: Ernesto Rico Schmidt <ernesto@helsinki.at> Date: Thu, 25 Jul 2024 18:41:56 -0400 Subject: [PATCH] feat: replace addnote management command with updatenote --- program/management/commands/addnote.py | 68 ----------------------- program/management/commands/updatenote.py | 68 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 program/management/commands/addnote.py create mode 100644 program/management/commands/updatenote.py diff --git a/program/management/commands/addnote.py b/program/management/commands/addnote.py deleted file mode 100644 index 567b30de..00000000 --- a/program/management/commands/addnote.py +++ /dev/null @@ -1,68 +0,0 @@ -import sys - -from django.core.exceptions import ValidationError -from django.core.management.base import BaseCommand, CommandError -from program.models import Note, Show, TimeSlot -from program.utils import parse_date - - -class Command(BaseCommand): - help = "adds a note to a timeslot" - args = "<show_id> <start_date> <status> [index]" - - def handle(self, *args, **options): - if len(args) == 3: - show_id = args[0] - start_date = args[1] - status = args[2] - elif len(args) == 4: - show_id = args[0] - start_date = args[1] - status = args[2] - index = args[3] - else: - raise CommandError("you must provide the show_id, start_date, status [index]") - - try: - show = Show.objects.get(id=show_id) - except Show.DoesNotExist as dne: - raise CommandError(dne) - - try: - start = parse_date(start_date) - except ValueError as ve: - raise CommandError(ve) - else: - year, month, day = start.year, start.month, start.day - - try: - timeslot = TimeSlot.objects.get( - show=show, start__year=year, start__month=month, start__day=day - ) - except TimeSlot.DoesNotExist as dne: - raise CommandError(dne) - except TimeSlot.MultipleObjectsReturned: - if not index: - raise CommandError("you must provide the show_id, start_date, status index") - try: - timeslot = TimeSlot.objects.filter( - show=show, start__year=year, start__month=month, start__day=day - ).order_by("start")[int(index)] - except IndexError as ie: - raise CommandError(ie) - - try: - title = sys.stdin.readline().rstrip() - lines = sys.stdin.readlines() - except Exception as e: - raise CommandError(e) - - note = Note(timeslot=timeslot, title=title, content="".join(lines), status=status) - - try: - note.validate_unique() - except ValidationError as ve: - raise CommandError(ve.messages[0]) - else: - note.save() - self.stdout.write(self.style.SUCCESS, f'added note "{title}" to "{timeslot}"') diff --git a/program/management/commands/updatenote.py b/program/management/commands/updatenote.py new file mode 100644 index 00000000..8c71bb03 --- /dev/null +++ b/program/management/commands/updatenote.py @@ -0,0 +1,68 @@ +import sys + +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist +from django.core.management.base import BaseCommand, CommandError +from program.models import Note, Show +from program.utils import parse_date + + +class Command(BaseCommand): + help = "updates title and content of a note for a timeslot by reading both from stdin." + + def add_arguments(self, parser): + parser.add_argument("show_id", help="ID of the show", type=int) + parser.add_argument("date", help="date of the timeslot", type=str) + parser.add_argument( + "index", + default=None, + help="index of the timeslot within the date. (default: 0)", + nargs="?", + type=int, + ) + + def handle(self, *args, **options): + try: + show = Show.objects.get(pk=options["show_id"]) + except ObjectDoesNotExist as e: + raise CommandError(e) + + try: + date = parse_date(options["date"]) + except ValueError as e: + raise CommandError(e) + else: + year, month, day = date.year, date.month, date.day + + try: + note = Note.objects.get( + timeslot__schedule__show=show, + timeslot__start__day=day, + timeslot__start__month=month, + timeslot__start__year=year, + ) + except ObjectDoesNotExist as e: + raise CommandError(e) + except MultipleObjectsReturned: + if not options["index"]: + raise CommandError(f"more than one note within {date}. Please provide an index.") + try: + note = Note.objects.filter( + timeslot__schedule__show=show, + timeslot__start__day=day, + timeslot__start__month=month, + timeslot__start__year=year, + ).order_by("timeslot__start")[options["index"]] + except IndexError as e: + raise CommandError(e) + + try: + title = sys.stdin.readline().strip() + content = sys.stdin.read() + except Exception as e: + raise CommandError(e) + + note.title = title + note.content = content + note.save() + + self.stdout.write(self.style.SUCCESS(f'updated note "{title}" for {note.timeslot}')) -- GitLab