annotate gpp/bulletins/models.py @ 101:4bbb6a9aa317

Forums: use select_related() with get_object_or_404() to reduce queries.
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 00:39:27 +0000
parents e249b5f9d180
children 5c889b587416
rev   line source
gremmie@1 1 """Models for the bulletins app.
gremmie@1 2 Bulletins allow the sited admins to display and manage important notices for the website.
gremmie@1 3 """
gremmie@1 4
gremmie@1 5 import datetime
gremmie@1 6 from django.db import models
gremmie@1 7 from django.db.models import Q
bgneal@54 8 from django.core.cache import cache
gremmie@1 9
gremmie@1 10
gremmie@1 11 class BulletinManager(models.Manager):
bgneal@54 12 """Manager for the Bulletin model."""
gremmie@1 13
bgneal@54 14 def get_current(self):
bgneal@54 15 now = datetime.datetime.now()
bgneal@54 16 return self.filter(
bgneal@54 17 Q(is_enabled=True),
bgneal@54 18 Q(start_date__lte=now),
bgneal@54 19 Q(end_date__isnull=True) | Q(end_date__gte=now))
gremmie@1 20
gremmie@1 21
gremmie@1 22 class Bulletin(models.Model):
bgneal@54 23 """Model to represent site bulletins."""
bgneal@54 24 title = models.CharField(max_length=200)
bgneal@54 25 text = models.TextField()
bgneal@54 26 start_date = models.DateTimeField(db_index=True,
bgneal@54 27 help_text='Start date for when the bulletin will be active.',)
bgneal@54 28 end_date = models.DateTimeField(blank=True, null=True, db_index=True,
bgneal@54 29 help_text='End date for the bulletin. Leave blank to keep it open-ended.')
bgneal@54 30 is_enabled = models.BooleanField(default=True, db_index=True,
bgneal@54 31 help_text='Check to allow the bulletin to be viewed on the site.')
gremmie@1 32
bgneal@54 33 objects = BulletinManager()
gremmie@1 34
bgneal@54 35 class Meta:
bgneal@54 36 ordering = ('-start_date', )
gremmie@1 37
bgneal@54 38 def __unicode__(self):
bgneal@54 39 return self.title
bgneal@54 40
bgneal@54 41 def save(self, force_insert=False, force_update=False):
bgneal@54 42 super(Bulletin, self).save(force_insert, force_update)
bgneal@54 43 cache.delete('home_bulletins')
bgneal@54 44
bgneal@54 45