Skip to content
Snippets Groups Projects
Verified Commit 68a9cd40 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

feat: replace addnote management command with updatenote

parent 5723cc62
No related branches found
No related tags found
No related merge requests found
Pipeline #8372 passed
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}"')
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}'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment