gremmie@1: """Models for the bulletins app. gremmie@1: Bulletins allow the sited admins to display and manage important notices for the website. gremmie@1: """ gremmie@1: gremmie@1: import datetime gremmie@1: from django.db import models gremmie@1: from django.db.models import Q bgneal@54: from django.core.cache import cache gremmie@1: gremmie@1: gremmie@1: class BulletinManager(models.Manager): bgneal@54: """Manager for the Bulletin model.""" gremmie@1: bgneal@54: def get_current(self): bgneal@54: now = datetime.datetime.now() bgneal@54: return self.filter( bgneal@54: Q(is_enabled=True), bgneal@54: Q(start_date__lte=now), bgneal@54: Q(end_date__isnull=True) | Q(end_date__gte=now)) gremmie@1: gremmie@1: gremmie@1: class Bulletin(models.Model): bgneal@54: """Model to represent site bulletins.""" bgneal@54: title = models.CharField(max_length=200) bgneal@54: text = models.TextField() bgneal@54: start_date = models.DateTimeField(db_index=True, bgneal@54: help_text='Start date for when the bulletin will be active.',) bgneal@54: end_date = models.DateTimeField(blank=True, null=True, db_index=True, bgneal@54: help_text='End date for the bulletin. Leave blank to keep it open-ended.') bgneal@54: is_enabled = models.BooleanField(default=True, db_index=True, bgneal@54: help_text='Check to allow the bulletin to be viewed on the site.') gremmie@1: bgneal@54: objects = BulletinManager() gremmie@1: bgneal@54: class Meta: bgneal@54: ordering = ('-start_date', ) gremmie@1: bgneal@54: def __unicode__(self): bgneal@54: return self.title bgneal@54: bgneal@54: def save(self, force_insert=False, force_update=False): bgneal@54: super(Bulletin, self).save(force_insert, force_update) bgneal@54: cache.delete('home_bulletins') bgneal@54: bgneal@54: