annotate gpp/gcalendar/models.py @ 124:9c18250972d5

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