gremmie@1: """ gremmie@1: Models for the gcalendar application. gremmie@1: """ gremmie@1: from django.db import models gremmie@1: from django.db.models import Q gremmie@1: from django.contrib.auth.models import User bgneal@124: bgneal@124: from core.markup import markdown bgneal@124: from smiley import smilify gremmie@1: gremmie@1: gremmie@1: class PendingEventManager(models.Manager): gremmie@1: """A manager for pending events.""" gremmie@1: gremmie@1: def get_query_set(self): gremmie@1: """Returns a queryset of events that have been approved to update gremmie@1: the Google calendar.""" gremmie@1: return super(PendingEventManager, self).get_query_set().filter( gremmie@1: Q(status=Event.NEW_APRV) | \ gremmie@1: Q(status=Event.EDIT_APRV) | \ gremmie@1: Q(status=Event.DEL_APRV) gremmie@1: ) gremmie@1: gremmie@1: gremmie@1: class Event(models.Model): gremmie@1: """Model to represent calendar events.""" gremmie@1: gremmie@1: # Event status codes: gremmie@1: (NEW, NEW_APRV, EDIT_REQ, EDIT_APRV, DEL_REQ, DEL_APRV, ON_CAL) = range(7) gremmie@1: gremmie@1: STATUS_CHOICES = ( gremmie@1: (NEW, 'New'), gremmie@1: (NEW_APRV, 'New Approved'), gremmie@1: (EDIT_REQ, 'Edit Request'), gremmie@1: (EDIT_APRV, 'Edit Approved'), gremmie@1: (DEL_REQ, 'Delete Request'), gremmie@1: (DEL_APRV, 'Delete Approved'), gremmie@1: (ON_CAL, 'On Calendar'), gremmie@1: ) gremmie@1: gremmie@1: REPEAT_CHOICES = ( gremmie@1: ('none', 'Does not repeat'), gremmie@1: ('daily', 'Daily'), gremmie@1: ('weekly', 'Weekly'), gremmie@1: ('monthly', 'Monthly'), gremmie@1: ('yearly', 'Yearly') gremmie@1: ) gremmie@1: gremmie@1: user = models.ForeignKey(User) gremmie@1: what = models.CharField(max_length=255) gremmie@1: start_date = models.DateField() gremmie@1: start_time = models.TimeField(null=True, blank=True) gremmie@1: end_date = models.DateField() gremmie@1: end_time = models.TimeField(null=True, blank=True) gremmie@1: time_zone = models.CharField(max_length=64, blank=True) gremmie@1: all_day = models.BooleanField(default=False) gremmie@1: repeat = models.CharField(max_length=7, choices=REPEAT_CHOICES) gremmie@1: repeat_interval = models.IntegerField(null=True, blank=True, gremmie@1: help_text='Only valid for repeating events.') gremmie@1: until_date = models.DateField(null=True, blank=True, gremmie@1: help_text='Only valid for repeating events; leave blank for no end date.') gremmie@1: weekly_sun = models.BooleanField(default=False, verbose_name='Weekly on Sun', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_mon = models.BooleanField(default=False, verbose_name='Weekly on Mon', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_tue = models.BooleanField(default=False, verbose_name='Weekly on Tue', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_wed = models.BooleanField(default=False, verbose_name='Weekly on Wed', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_thu = models.BooleanField(default=False, verbose_name='Weekly on Thu', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_fri = models.BooleanField(default=False, verbose_name='Weekly on Fri', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: weekly_sat = models.BooleanField(default=False, verbose_name='Weekly on Sat', gremmie@1: help_text='Only valid for weekly repeats.') gremmie@1: monthly_by_day = models.BooleanField(default=False, gremmie@1: help_text='Only valid for monthly repeats; Checked: By day of the month, Unchecked: By day of the week.') gremmie@1: where = models.CharField(max_length=255, blank=True) gremmie@1: description = models.TextField(blank=True) gremmie@1: html = models.TextField(blank=True) gremmie@1: date_submitted = models.DateTimeField(auto_now_add=True) gremmie@1: google_id = models.CharField(max_length=255, blank=True) gremmie@1: status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW, db_index=True) gremmie@1: gremmie@1: objects = models.Manager() gremmie@1: pending_events = PendingEventManager() gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.what gremmie@1: gremmie@1: class Meta: gremmie@1: ordering = ('-date_submitted', ) gremmie@1: gremmie@1: def save(self, *args, **kwargs): bgneal@124: self.html = smilify(markdown(self.description)) gremmie@1: super(Event, self).save(*args, **kwargs) gremmie@1: gremmie@1: def needs_approval(self): gremmie@1: return self.status in (self.NEW, self.EDIT_REQ, self.DEL_REQ) gremmie@1: