gremmie@1
|
1 """
|
gremmie@1
|
2 Models for the gcalendar application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4 from django.db import models
|
gremmie@1
|
5 from django.db.models import Q
|
gremmie@1
|
6 from django.contrib.auth.models import User
|
gremmie@1
|
7 from django.template.loader import render_to_string
|
gremmie@1
|
8
|
gremmie@1
|
9
|
gremmie@1
|
10 class PendingEventManager(models.Manager):
|
gremmie@1
|
11 """A manager for pending events."""
|
gremmie@1
|
12
|
gremmie@1
|
13 def get_query_set(self):
|
gremmie@1
|
14 """Returns a queryset of events that have been approved to update
|
gremmie@1
|
15 the Google calendar."""
|
gremmie@1
|
16 return super(PendingEventManager, self).get_query_set().filter(
|
gremmie@1
|
17 Q(status=Event.NEW_APRV) | \
|
gremmie@1
|
18 Q(status=Event.EDIT_APRV) | \
|
gremmie@1
|
19 Q(status=Event.DEL_APRV)
|
gremmie@1
|
20 )
|
gremmie@1
|
21
|
gremmie@1
|
22
|
gremmie@1
|
23 class Event(models.Model):
|
gremmie@1
|
24 """Model to represent calendar events."""
|
gremmie@1
|
25
|
gremmie@1
|
26 # Event status codes:
|
gremmie@1
|
27 (NEW, NEW_APRV, EDIT_REQ, EDIT_APRV, DEL_REQ, DEL_APRV, ON_CAL) = range(7)
|
gremmie@1
|
28
|
gremmie@1
|
29 STATUS_CHOICES = (
|
gremmie@1
|
30 (NEW, 'New'),
|
gremmie@1
|
31 (NEW_APRV, 'New Approved'),
|
gremmie@1
|
32 (EDIT_REQ, 'Edit Request'),
|
gremmie@1
|
33 (EDIT_APRV, 'Edit Approved'),
|
gremmie@1
|
34 (DEL_REQ, 'Delete Request'),
|
gremmie@1
|
35 (DEL_APRV, 'Delete Approved'),
|
gremmie@1
|
36 (ON_CAL, 'On Calendar'),
|
gremmie@1
|
37 )
|
gremmie@1
|
38
|
gremmie@1
|
39 REPEAT_CHOICES = (
|
gremmie@1
|
40 ('none', 'Does not repeat'),
|
gremmie@1
|
41 ('daily', 'Daily'),
|
gremmie@1
|
42 ('weekly', 'Weekly'),
|
gremmie@1
|
43 ('monthly', 'Monthly'),
|
gremmie@1
|
44 ('yearly', 'Yearly')
|
gremmie@1
|
45 )
|
gremmie@1
|
46
|
gremmie@1
|
47 user = models.ForeignKey(User)
|
gremmie@1
|
48 what = models.CharField(max_length=255)
|
gremmie@1
|
49 start_date = models.DateField()
|
gremmie@1
|
50 start_time = models.TimeField(null=True, blank=True)
|
gremmie@1
|
51 end_date = models.DateField()
|
gremmie@1
|
52 end_time = models.TimeField(null=True, blank=True)
|
gremmie@1
|
53 time_zone = models.CharField(max_length=64, blank=True)
|
gremmie@1
|
54 all_day = models.BooleanField(default=False)
|
gremmie@1
|
55 repeat = models.CharField(max_length=7, choices=REPEAT_CHOICES)
|
gremmie@1
|
56 repeat_interval = models.IntegerField(null=True, blank=True,
|
gremmie@1
|
57 help_text='Only valid for repeating events.')
|
gremmie@1
|
58 until_date = models.DateField(null=True, blank=True,
|
gremmie@1
|
59 help_text='Only valid for repeating events; leave blank for no end date.')
|
gremmie@1
|
60 weekly_sun = models.BooleanField(default=False, verbose_name='Weekly on Sun',
|
gremmie@1
|
61 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
62 weekly_mon = models.BooleanField(default=False, verbose_name='Weekly on Mon',
|
gremmie@1
|
63 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
64 weekly_tue = models.BooleanField(default=False, verbose_name='Weekly on Tue',
|
gremmie@1
|
65 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
66 weekly_wed = models.BooleanField(default=False, verbose_name='Weekly on Wed',
|
gremmie@1
|
67 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
68 weekly_thu = models.BooleanField(default=False, verbose_name='Weekly on Thu',
|
gremmie@1
|
69 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
70 weekly_fri = models.BooleanField(default=False, verbose_name='Weekly on Fri',
|
gremmie@1
|
71 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
72 weekly_sat = models.BooleanField(default=False, verbose_name='Weekly on Sat',
|
gremmie@1
|
73 help_text='Only valid for weekly repeats.')
|
gremmie@1
|
74 monthly_by_day = models.BooleanField(default=False,
|
gremmie@1
|
75 help_text='Only valid for monthly repeats; Checked: By day of the month, Unchecked: By day of the week.')
|
gremmie@1
|
76 where = models.CharField(max_length=255, blank=True)
|
gremmie@1
|
77 description = models.TextField(blank=True)
|
gremmie@1
|
78 html = models.TextField(blank=True)
|
gremmie@1
|
79 date_submitted = models.DateTimeField(auto_now_add=True)
|
gremmie@1
|
80 google_id = models.CharField(max_length=255, blank=True)
|
gremmie@1
|
81 status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW, db_index=True)
|
gremmie@1
|
82
|
gremmie@1
|
83 objects = models.Manager()
|
gremmie@1
|
84 pending_events = PendingEventManager()
|
gremmie@1
|
85
|
gremmie@1
|
86 def __unicode__(self):
|
gremmie@1
|
87 return self.what
|
gremmie@1
|
88
|
gremmie@1
|
89 class Meta:
|
gremmie@1
|
90 ordering = ('-date_submitted', )
|
gremmie@1
|
91
|
gremmie@1
|
92 def save(self, *args, **kwargs):
|
gremmie@1
|
93 html = render_to_string('gcalendar/markdown.html', {'data': self.description})
|
gremmie@1
|
94 self.html = html.strip()
|
gremmie@1
|
95 super(Event, self).save(*args, **kwargs)
|
gremmie@1
|
96
|
gremmie@1
|
97 def needs_approval(self):
|
gremmie@1
|
98 return self.status in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
|
gremmie@1
|
99
|
gremmie@1
|
100
|
gremmie@1
|
101 # vim: ts=4 sw=4
|