annotate gpp/gcalendar/models.py @ 456:5be072292e2d

Working on #220. Can't test locally, so committing in increments.
author Brian Neal <bgneal@gmail.com>
date Fri, 01 Jul 2011 00:43:44 +0000
parents d77e0dc772ad
children 9a4bffdf37c3
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the gcalendar application.
bgneal@456 3
gremmie@1 4 """
gremmie@1 5 from django.db import models
gremmie@1 6 from django.db.models import Q
gremmie@1 7 from django.contrib.auth.models import User
bgneal@124 8
bgneal@128 9 from core.markup import site_markup
bgneal@228 10 import forums.tools
gremmie@1 11
gremmie@1 12
bgneal@228 13 GIG_FORUM_SLUG = "gigs"
bgneal@228 14
gremmie@1 15 class PendingEventManager(models.Manager):
gremmie@1 16 """A manager for pending events."""
gremmie@1 17
gremmie@1 18 def get_query_set(self):
gremmie@1 19 """Returns a queryset of events that have been approved to update
gremmie@1 20 the Google calendar."""
gremmie@1 21 return super(PendingEventManager, self).get_query_set().filter(
bgneal@456 22 Q(status=Event.NEW_APRV) |
bgneal@456 23 Q(status=Event.EDIT_APRV) |
gremmie@1 24 Q(status=Event.DEL_APRV)
gremmie@1 25 )
gremmie@1 26
gremmie@1 27
gremmie@1 28 class Event(models.Model):
gremmie@1 29 """Model to represent calendar events."""
gremmie@1 30
gremmie@1 31 # Event status codes:
gremmie@1 32 (NEW, NEW_APRV, EDIT_REQ, EDIT_APRV, DEL_REQ, DEL_APRV, ON_CAL) = range(7)
gremmie@1 33
gremmie@1 34 STATUS_CHOICES = (
gremmie@1 35 (NEW, 'New'),
gremmie@1 36 (NEW_APRV, 'New Approved'),
gremmie@1 37 (EDIT_REQ, 'Edit Request'),
gremmie@1 38 (EDIT_APRV, 'Edit Approved'),
gremmie@1 39 (DEL_REQ, 'Delete Request'),
gremmie@1 40 (DEL_APRV, 'Delete Approved'),
gremmie@1 41 (ON_CAL, 'On Calendar'),
gremmie@1 42 )
gremmie@1 43
gremmie@1 44 user = models.ForeignKey(User)
gremmie@1 45 what = models.CharField(max_length=255)
gremmie@1 46 start_date = models.DateField()
gremmie@1 47 start_time = models.TimeField(null=True, blank=True)
gremmie@1 48 end_date = models.DateField()
gremmie@1 49 end_time = models.TimeField(null=True, blank=True)
gremmie@1 50 time_zone = models.CharField(max_length=64, blank=True)
gremmie@1 51 all_day = models.BooleanField(default=False)
gremmie@1 52 where = models.CharField(max_length=255, blank=True)
gremmie@1 53 description = models.TextField(blank=True)
gremmie@1 54 html = models.TextField(blank=True)
gremmie@1 55 date_submitted = models.DateTimeField(auto_now_add=True)
gremmie@1 56 google_id = models.CharField(max_length=255, blank=True)
bgneal@228 57 google_url = models.URLField(verify_exists=False, max_length=255,
bgneal@228 58 blank=True)
bgneal@228 59 status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW,
bgneal@228 60 db_index=True)
bgneal@228 61 create_forum_thread = models.BooleanField(default=False)
gremmie@1 62
gremmie@1 63 objects = models.Manager()
gremmie@1 64 pending_events = PendingEventManager()
gremmie@1 65
gremmie@1 66 def __unicode__(self):
gremmie@1 67 return self.what
gremmie@1 68
gremmie@1 69 class Meta:
gremmie@1 70 ordering = ('-date_submitted', )
gremmie@1 71
gremmie@1 72 def save(self, *args, **kwargs):
bgneal@128 73 self.html = site_markup(self.description)
gremmie@1 74 super(Event, self).save(*args, **kwargs)
gremmie@1 75
bgneal@139 76 def is_approved(self):
bgneal@139 77 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
bgneal@139 78 is_approved.boolean = True
gremmie@1 79
bgneal@228 80 def google_html(self):
bgneal@228 81 """Returns a HTML <a> tag to the event if it exits."""
bgneal@228 82 if self.google_url:
bgneal@228 83 return u'<a href="%s">On Google</a>' % self.google_url
bgneal@228 84 return u''
bgneal@228 85 google_html.allow_tags = True
bgneal@228 86 google_html.short_description = 'Google Link'
bgneal@228 87
bgneal@228 88 def notify_on_calendar(self):
bgneal@228 89 """
bgneal@228 90 This function should be called when the event has been added to the
bgneal@228 91 Google calendar for the first time. This gives us a chance to perform
bgneal@228 92 any first-time processing, like creating a forum thread.
bgneal@228 93 """
bgneal@228 94 if self.create_forum_thread:
bgneal@228 95 topic_name = '%s: %s' % (self.start_date.strftime('%m/%d/%Y'),
bgneal@228 96 self.what)
bgneal@228 97 post_body = "%s\n\n[Link to event on Google Calendar](%s)" % (
bgneal@228 98 self.description, self.google_url)
bgneal@228 99
bgneal@228 100 forums.tools.create_topic(
bgneal@228 101 forum_slug=GIG_FORUM_SLUG,
bgneal@456 102 user=self.user,
bgneal@228 103 topic_name=topic_name,
bgneal@228 104 post_body=post_body)
bgneal@228 105
bgneal@228 106 self.create_forum_thread = False
bgneal@228 107 self.save()
bgneal@456 108
bgneal@456 109
bgneal@456 110 class AccessToken(models.Model):
bgneal@456 111 """
bgneal@456 112 This model represents serialized OAuth access tokens for reading and
bgneal@456 113 updating the Google Calendar.
bgneal@456 114
bgneal@456 115 """
bgneal@456 116 auth_date = models.DateTimeField()
bgneal@456 117 token = models.TextField()
bgneal@456 118
bgneal@456 119 def __unicode__(self):
bgneal@456 120 return u'Access token created on ' + unicode(self.auth_date)
bgneal@456 121