annotate gpp/news/models.py @ 218:6dbb8faef085

Implement #86, add a front page expiration date to news stories.
author Brian Neal <bgneal@gmail.com>
date Tue, 01 Jun 2010 05:16:40 +0000
parents 65016249bf35
children 71fd8454688b
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the news application.
gremmie@1 3 """
gremmie@1 4
gremmie@1 5 import datetime
gremmie@1 6 from django.db import models
bgneal@49 7 from django.contrib.auth.models import User
gremmie@1 8 from tagging.fields import TagField
gremmie@1 9
gremmie@1 10
gremmie@1 11 class Category(models.Model):
bgneal@54 12 """News stories belong to categories"""
bgneal@54 13 title = models.CharField(max_length = 64)
bgneal@54 14 icon = models.ImageField(upload_to='news/categories/', blank=True)
gremmie@1 15
bgneal@54 16 def __unicode__(self):
bgneal@54 17 return self.title
gremmie@1 18
bgneal@54 19 def num_stories(self):
bgneal@54 20 return News.objects.filter(category = self.pk).count()
gremmie@1 21
bgneal@54 22 class Meta:
bgneal@54 23 verbose_name_plural = 'Categories'
bgneal@54 24 ordering = ('title', )
gremmie@1 25
gremmie@1 26
bgneal@204 27 class StoryBase(models.Model):
bgneal@204 28 """Abstract model to collect common fields."""
bgneal@54 29 title = models.CharField(max_length=255)
bgneal@54 30 submitter = models.ForeignKey(User)
bgneal@54 31 category = models.ForeignKey(Category)
bgneal@54 32 short_text = models.TextField()
bgneal@54 33 long_text = models.TextField(blank=True)
bgneal@204 34 date_submitted = models.DateTimeField(db_index=True)
bgneal@54 35 allow_comments = models.BooleanField(default=True)
bgneal@54 36 tags = TagField()
bgneal@218 37 front_page_expiration = models.DateField(null=True, blank=True)
gremmie@1 38
bgneal@204 39 class Meta:
bgneal@204 40 abstract = True
bgneal@204 41
bgneal@204 42
bgneal@204 43 class PendingStory(StoryBase):
bgneal@204 44 """Stories submitted by users are held pending admin approval"""
bgneal@204 45
bgneal@182 46 def save(self, *args, **kwargs):
bgneal@204 47 if not self.pk:
bgneal@204 48 self.date_submitted = datetime.datetime.now()
bgneal@204 49
bgneal@204 50 super(PendingStory, self).save(*args, **kwargs)
gremmie@1 51
bgneal@54 52 def __unicode__(self):
bgneal@54 53 return self.title
gremmie@1 54
bgneal@54 55 class Meta:
bgneal@54 56 ordering = ('-date_submitted', )
bgneal@54 57 verbose_name_plural = 'Pending Stories'
gremmie@1 58
gremmie@1 59
bgneal@204 60 class Story(StoryBase):
bgneal@54 61 """Model for news stories"""
gremmie@1 62
bgneal@54 63 @models.permalink
bgneal@54 64 def get_absolute_url(self):
bgneal@54 65 return ('news.views.story', [str(self.id)])
gremmie@1 66
bgneal@54 67 def __unicode__(self):
bgneal@54 68 return self.title
gremmie@1 69
bgneal@54 70 class Meta:
bgneal@204 71 ordering = ('-date_submitted', )
bgneal@54 72 verbose_name_plural = 'Stories'
gremmie@1 73
bgneal@54 74 def can_comment_on(self):
bgneal@54 75 now = datetime.datetime.now()
bgneal@204 76 delta = now - self.date_submitted
bgneal@204 77 return self.allow_comments and delta.days < 30