diff options
author | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2011-07-23 13:40:34 (GMT) |
---|---|---|
committer | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2011-07-23 13:40:34 (GMT) |
commit | b9aea9d71f2b01416e28eaaa5100072c3ef14673 (patch) | |
tree | 3db06f7e71079e45f69c5c7e779196c2d2eb9659 | |
parent | 6e527861f100efba2fd9c1f821782f20e2000d93 (diff) |
added management command to add a note.
-rw-r--r-- | program/management/commands/addnote.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/program/management/commands/addnote.py b/program/management/commands/addnote.py new file mode 100644 index 0000000..2ba2022 --- /dev/null +++ b/program/management/commands/addnote.py @@ -0,0 +1,54 @@ +from django.contrib.auth.models import User +from django.core.management.base import BaseCommand, CommandError +from django.core.exceptions import ValidationError + +from datetime import datetime +import sys + +from program.models import Show, TimeSlot, Note + +class Command(BaseCommand): + help = 'adds a note to a timeslot' + args = '<show_id> <date>' + + def handle(self, *args, **options): + if len(args) == 2: + show_id = args[0] + start_date = args[1] + else: + raise CommandError('you must provide the show_id date') + + try: + show = Show.objects.get(id=show_id) + except Show.DoesNotExist as dne: + raise CommandError(dne) + + try: + start = datetime.strptime(start_date, '%Y-%m-%d') + 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) + + try: + title = sys.stdin.readline().rstrip() + lines = sys.stdin.readlines() + except Exception as e: + raise CommandError(e) + + owner = show.owners[0] if show.owners.count() > 0 else User.objects.get(pk=1) + note = Note(timeslot=timeslot, owner=owner, title=title, content=''.join(lines)) + + try: + note.validate_unique() + except ValidationError as ve: + raise CommandError(ve.messages[0]) + else: + note.save() + print 'added note "%s" to "%s"' % (title, timeslot) +
\ No newline at end of file |