1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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> <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:
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 = 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)
except TimeSlot.MultipleObjectsReturned as mor:
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')[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)
owner = show.owners.all()[0] if show.owners.count() > 0 else User.objects.get(pk=1)
note = Note(timeslot=timeslot, owner=owner, title=title, content=''.join(lines), status=status)
try:
note.validate_unique()
except ValidationError as ve:
raise CommandError(ve.messages[0])
else:
note.save()
print 'added note "%s" to "%s"' % (title, timeslot)
|