gremmie@1: """ gremmie@1: Models for the news application. gremmie@1: """ gremmie@1: gremmie@1: import datetime gremmie@1: from django.db import models bgneal@49: from django.contrib.auth.models import User bgneal@54: from django.core.cache import cache gremmie@1: from tagging.fields import TagField gremmie@1: gremmie@1: gremmie@1: class Category(models.Model): bgneal@54: """News stories belong to categories""" bgneal@54: title = models.CharField(max_length = 64) bgneal@54: icon = models.ImageField(upload_to='news/categories/', blank=True) gremmie@1: bgneal@54: def __unicode__(self): bgneal@54: return self.title gremmie@1: bgneal@54: def num_stories(self): bgneal@54: return News.objects.filter(category = self.pk).count() gremmie@1: bgneal@54: class Meta: bgneal@54: verbose_name_plural = 'Categories' bgneal@54: ordering = ('title', ) gremmie@1: gremmie@1: gremmie@1: class PendingStory(models.Model): bgneal@54: """Stories submitted by users are held pending admin approval""" bgneal@54: title = models.CharField(max_length=255) bgneal@54: submitter = models.ForeignKey(User) bgneal@54: category = models.ForeignKey(Category) bgneal@54: short_text = models.TextField() bgneal@54: long_text = models.TextField(blank=True) bgneal@54: date_submitted = models.DateTimeField(auto_now_add=True, db_index=True) bgneal@54: allow_comments = models.BooleanField(default=True) bgneal@54: approved = models.BooleanField(default=False) bgneal@54: tags = TagField() gremmie@1: bgneal@182: def save(self, *args, **kwargs): bgneal@54: if self.approved: bgneal@54: Story.objects.create(title=self.title, bgneal@54: submitter=self.submitter, bgneal@54: category=self.category, bgneal@54: short_text=self.short_text, bgneal@54: long_text=self.long_text, bgneal@54: allow_comments=self.allow_comments, bgneal@54: date_published=datetime.datetime.now(), bgneal@54: tags=self.tags) bgneal@54: self.delete() bgneal@54: cache.delete('home_news') bgneal@54: else: bgneal@182: super(PendingStory, self).save(*args, **kwargs) gremmie@1: bgneal@54: def __unicode__(self): bgneal@54: return self.title gremmie@1: bgneal@54: class Meta: bgneal@54: ordering = ('-date_submitted', ) bgneal@54: verbose_name_plural = 'Pending Stories' gremmie@1: gremmie@1: gremmie@1: class Story(models.Model): bgneal@54: """Model for news stories""" bgneal@54: title = models.CharField(max_length=255) bgneal@54: submitter = models.ForeignKey(User) bgneal@54: category = models.ForeignKey(Category) bgneal@54: short_text = models.TextField() bgneal@54: long_text = models.TextField(blank=True) bgneal@54: allow_comments = models.BooleanField(default=True) bgneal@54: date_published = models.DateTimeField(db_index=True) bgneal@54: tags = TagField() gremmie@1: bgneal@54: @models.permalink bgneal@54: def get_absolute_url(self): bgneal@54: return ('news.views.story', [str(self.id)]) gremmie@1: bgneal@54: def __unicode__(self): bgneal@54: return self.title gremmie@1: bgneal@54: class Meta: bgneal@54: ordering = ('-date_published', ) bgneal@54: verbose_name_plural = 'Stories' gremmie@1: bgneal@54: def can_comment_on(self): bgneal@54: now = datetime.datetime.now() bgneal@54: delta = now - self.date_published bgneal@54: return delta.days < 30 gremmie@1: bgneal@182: def save(self, *args, **kwargs): bgneal@182: super(Story, self).save(*args, **kwargs) bgneal@54: cache.delete('home_news') bgneal@54: