annotate gpp/news/models.py @ 277:d424b8bae71d

Fixing #128 and #129. Add elsewhere weblinks to search content. Add support for haystack's get_update_field() method.
author Brian Neal <bgneal@gmail.com>
date Sat, 02 Oct 2010 23:24:39 +0000
parents 1246a4f1ab4f
children 53fdaf0da539
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@240 13 title = models.CharField(max_length=64)
bgneal@240 14 slug = models.SlugField(max_length=64)
bgneal@54 15 icon = models.ImageField(upload_to='news/categories/', blank=True)
gremmie@1 16
bgneal@54 17 def __unicode__(self):
bgneal@54 18 return self.title
gremmie@1 19
bgneal@54 20 def num_stories(self):
bgneal@54 21 return News.objects.filter(category = self.pk).count()
gremmie@1 22
bgneal@54 23 class Meta:
bgneal@54 24 verbose_name_plural = 'Categories'
bgneal@54 25 ordering = ('title', )
gremmie@1 26
gremmie@1 27
bgneal@204 28 class StoryBase(models.Model):
bgneal@204 29 """Abstract model to collect common fields."""
bgneal@54 30 title = models.CharField(max_length=255)
bgneal@54 31 submitter = models.ForeignKey(User)
bgneal@54 32 category = models.ForeignKey(Category)
bgneal@54 33 short_text = models.TextField()
bgneal@54 34 long_text = models.TextField(blank=True)
bgneal@204 35 date_submitted = models.DateTimeField(db_index=True)
bgneal@54 36 allow_comments = models.BooleanField(default=True)
bgneal@54 37 tags = TagField()
bgneal@218 38 front_page_expiration = models.DateField(null=True, blank=True)
bgneal@277 39 update_date = models.DateTimeField(db_index=True, blank=True)
gremmie@1 40
bgneal@204 41 class Meta:
bgneal@204 42 abstract = True
bgneal@204 43
bgneal@204 44
bgneal@204 45 class PendingStory(StoryBase):
bgneal@204 46 """Stories submitted by users are held pending admin approval"""
bgneal@204 47
bgneal@182 48 def save(self, *args, **kwargs):
bgneal@204 49 if not self.pk:
bgneal@277 50 if not self.date_submitted:
bgneal@277 51 self.date_submitted = datetime.datetime.now()
bgneal@277 52 self.update_date = self.date_submitted
bgneal@277 53 else:
bgneal@277 54 self.update_date = datetime.datetime.now()
bgneal@204 55
bgneal@204 56 super(PendingStory, self).save(*args, **kwargs)
gremmie@1 57
bgneal@54 58 def __unicode__(self):
bgneal@54 59 return self.title
gremmie@1 60
bgneal@54 61 class Meta:
bgneal@54 62 ordering = ('-date_submitted', )
bgneal@54 63 verbose_name_plural = 'Pending Stories'
gremmie@1 64
gremmie@1 65
bgneal@204 66 class Story(StoryBase):
bgneal@54 67 """Model for news stories"""
gremmie@1 68
bgneal@54 69 @models.permalink
bgneal@54 70 def get_absolute_url(self):
bgneal@54 71 return ('news.views.story', [str(self.id)])
gremmie@1 72
bgneal@54 73 def __unicode__(self):
bgneal@54 74 return self.title
gremmie@1 75
bgneal@54 76 class Meta:
bgneal@204 77 ordering = ('-date_submitted', )
bgneal@226 78 verbose_name = 'news story'
bgneal@226 79 verbose_name_plural = 'news stories'
gremmie@1 80
bgneal@277 81 def save(self, *args, **kwargs):
bgneal@277 82 if not self.pk:
bgneal@277 83 self.date_submitted = datetime.datetime.now()
bgneal@277 84 self.update_date = self.date_submitted
bgneal@277 85 else:
bgneal@277 86 self.update_date = datetime.datetime.now()
bgneal@277 87
bgneal@277 88 super(Story, self).save(*args, **kwargs)
bgneal@277 89
bgneal@54 90 def can_comment_on(self):
bgneal@54 91 now = datetime.datetime.now()
bgneal@204 92 delta = now - self.date_submitted
bgneal@204 93 return self.allow_comments and delta.days < 30
bgneal@220 94
bgneal@220 95 def search_title(self):
bgneal@220 96 return self.title
bgneal@220 97
bgneal@220 98 def search_summary(self):
bgneal@220 99 return u"\n".join((self.short_text, self.long_text))