annotate gpp/gcalendar/models.py @ 399:24f1230f3ee3

Fixing #193; reduce news query counts by grabbing tags and comment counts in bulk. Increased news items per page from 5 to 10.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Mar 2011 03:08:05 +0000
parents d77e0dc772ad
children 5be072292e2d
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@128 8 from core.markup import site_markup
bgneal@228 9 import forums.tools
gremmie@1 10
gremmie@1 11
bgneal@228 12 GIG_FORUM_SLUG = "gigs"
bgneal@228 13
gremmie@1 14 class PendingEventManager(models.Manager):
gremmie@1 15 """A manager for pending events."""
gremmie@1 16
gremmie@1 17 def get_query_set(self):
gremmie@1 18 """Returns a queryset of events that have been approved to update
gremmie@1 19 the Google calendar."""
gremmie@1 20 return super(PendingEventManager, self).get_query_set().filter(
gremmie@1 21 Q(status=Event.NEW_APRV) | \
gremmie@1 22 Q(status=Event.EDIT_APRV) | \
gremmie@1 23 Q(status=Event.DEL_APRV)
gremmie@1 24 )
gremmie@1 25
gremmie@1 26
gremmie@1 27 class Event(models.Model):
gremmie@1 28 """Model to represent calendar events."""
gremmie@1 29
gremmie@1 30 # Event status codes:
gremmie@1 31 (NEW, NEW_APRV, EDIT_REQ, EDIT_APRV, DEL_REQ, DEL_APRV, ON_CAL) = range(7)
gremmie@1 32
gremmie@1 33 STATUS_CHOICES = (
gremmie@1 34 (NEW, 'New'),
gremmie@1 35 (NEW_APRV, 'New Approved'),
gremmie@1 36 (EDIT_REQ, 'Edit Request'),
gremmie@1 37 (EDIT_APRV, 'Edit Approved'),
gremmie@1 38 (DEL_REQ, 'Delete Request'),
gremmie@1 39 (DEL_APRV, 'Delete Approved'),
gremmie@1 40 (ON_CAL, 'On Calendar'),
gremmie@1 41 )
gremmie@1 42
gremmie@1 43 user = models.ForeignKey(User)
gremmie@1 44 what = models.CharField(max_length=255)
gremmie@1 45 start_date = models.DateField()
gremmie@1 46 start_time = models.TimeField(null=True, blank=True)
gremmie@1 47 end_date = models.DateField()
gremmie@1 48 end_time = models.TimeField(null=True, blank=True)
gremmie@1 49 time_zone = models.CharField(max_length=64, blank=True)
gremmie@1 50 all_day = models.BooleanField(default=False)
gremmie@1 51 where = models.CharField(max_length=255, blank=True)
gremmie@1 52 description = models.TextField(blank=True)
gremmie@1 53 html = models.TextField(blank=True)
gremmie@1 54 date_submitted = models.DateTimeField(auto_now_add=True)
gremmie@1 55 google_id = models.CharField(max_length=255, blank=True)
bgneal@228 56 google_url = models.URLField(verify_exists=False, max_length=255,
bgneal@228 57 blank=True)
bgneal@228 58 status = models.SmallIntegerField(choices=STATUS_CHOICES, default=NEW,
bgneal@228 59 db_index=True)
bgneal@228 60 create_forum_thread = models.BooleanField(default=False)
gremmie@1 61
gremmie@1 62 objects = models.Manager()
gremmie@1 63 pending_events = PendingEventManager()
gremmie@1 64
gremmie@1 65 def __unicode__(self):
gremmie@1 66 return self.what
gremmie@1 67
gremmie@1 68 class Meta:
gremmie@1 69 ordering = ('-date_submitted', )
gremmie@1 70
gremmie@1 71 def save(self, *args, **kwargs):
bgneal@128 72 self.html = site_markup(self.description)
gremmie@1 73 super(Event, self).save(*args, **kwargs)
gremmie@1 74
bgneal@139 75 def is_approved(self):
bgneal@139 76 return self.status not in (self.NEW, self.EDIT_REQ, self.DEL_REQ)
bgneal@139 77 is_approved.boolean = True
gremmie@1 78
bgneal@228 79 def google_html(self):
bgneal@228 80 """Returns a HTML <a> tag to the event if it exits."""
bgneal@228 81 if self.google_url:
bgneal@228 82 return u'<a href="%s">On Google</a>' % self.google_url
bgneal@228 83 return u''
bgneal@228 84 google_html.allow_tags = True
bgneal@228 85 google_html.short_description = 'Google Link'
bgneal@228 86
bgneal@228 87 def notify_on_calendar(self):
bgneal@228 88 """
bgneal@228 89 This function should be called when the event has been added to the
bgneal@228 90 Google calendar for the first time. This gives us a chance to perform
bgneal@228 91 any first-time processing, like creating a forum thread.
bgneal@228 92 """
bgneal@228 93 if self.create_forum_thread:
bgneal@228 94 topic_name = '%s: %s' % (self.start_date.strftime('%m/%d/%Y'),
bgneal@228 95 self.what)
bgneal@228 96 post_body = "%s\n\n[Link to event on Google Calendar](%s)" % (
bgneal@228 97 self.description, self.google_url)
bgneal@228 98
bgneal@228 99 forums.tools.create_topic(
bgneal@228 100 forum_slug=GIG_FORUM_SLUG,
bgneal@228 101 user=self.user,
bgneal@228 102 topic_name=topic_name,
bgneal@228 103 post_body=post_body)
bgneal@228 104
bgneal@228 105 self.create_forum_thread = False
bgneal@228 106 self.save()