annotate gpp/news/models.py @ 21:884839ddbfde

Webinks: added a navigation template tag so views don't have to keep constructing the searc form. Also provided a count of search results on the template.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Apr 2009 21:00:06 +0000
parents dbd703f7d63a
children e66e718e1e1e
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
gremmie@1 7 from django.contrib import auth
gremmie@1 8 from tagging.fields import TagField
gremmie@1 9
gremmie@1 10
gremmie@1 11 class Category(models.Model):
gremmie@1 12 """News stories belong to categories"""
gremmie@1 13 title = models.CharField(max_length = 64)
gremmie@1 14 icon = models.ImageField(upload_to='news/categories/', blank=True)
gremmie@1 15
gremmie@1 16 def __unicode__(self):
gremmie@1 17 return self.title
gremmie@1 18
gremmie@1 19 def num_stories(self):
gremmie@1 20 return News.objects.filter(category = self.pk).count()
gremmie@1 21
gremmie@1 22 class Meta:
gremmie@1 23 verbose_name_plural = 'Categories'
gremmie@1 24 ordering = ('title', )
gremmie@1 25
gremmie@1 26
gremmie@1 27 class PendingStory(models.Model):
gremmie@1 28 """Stories submitted by users are held pending admin approval"""
gremmie@1 29 title = models.CharField(max_length=255)
gremmie@1 30 submitter = models.ForeignKey(auth.models.User)
gremmie@1 31 category = models.ForeignKey(Category)
gremmie@1 32 short_text = models.TextField()
gremmie@1 33 long_text = models.TextField(blank=True)
gremmie@1 34 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True)
gremmie@1 35 allow_comments = models.BooleanField(default=True)
gremmie@1 36 approved = models.BooleanField(default=False)
gremmie@1 37 tags = TagField()
gremmie@1 38
gremmie@1 39 def save(self, force_insert = False, force_update = False):
gremmie@1 40 if self.approved:
gremmie@1 41 Story.objects.create(title=self.title,
gremmie@1 42 submitter=self.submitter,
gremmie@1 43 category=self.category,
gremmie@1 44 short_text=self.short_text,
gremmie@1 45 long_text=self.long_text,
gremmie@1 46 allow_comments=self.allow_comments,
gremmie@1 47 date_published=datetime.datetime.now(),
gremmie@1 48 tags=self.tags)
gremmie@1 49 self.delete()
gremmie@1 50 else:
gremmie@1 51 super(PendingStory, self).save(force_insert, force_update)
gremmie@1 52
gremmie@1 53 def __unicode__(self):
gremmie@1 54 return self.title
gremmie@1 55
gremmie@1 56 class Meta:
gremmie@1 57 ordering = ('-date_submitted', )
gremmie@1 58 verbose_name_plural = 'Pending Stories'
gremmie@1 59
gremmie@1 60
gremmie@1 61 class Story(models.Model):
gremmie@1 62 """Model for news stories"""
gremmie@1 63 title = models.CharField(max_length=255)
gremmie@1 64 submitter = models.ForeignKey(auth.models.User)
gremmie@1 65 category = models.ForeignKey(Category)
gremmie@1 66 short_text = models.TextField()
gremmie@1 67 long_text = models.TextField(blank=True)
gremmie@1 68 allow_comments = models.BooleanField(default=True)
gremmie@1 69 date_published = models.DateTimeField(db_index=True)
gremmie@1 70 tags = TagField()
gremmie@1 71
gremmie@1 72 @models.permalink
gremmie@1 73 def get_absolute_url(self):
gremmie@1 74 return ('news.views.story', [str(self.id)])
gremmie@1 75
gremmie@1 76 def __unicode__(self):
gremmie@1 77 return self.title
gremmie@1 78
gremmie@1 79 class Meta:
gremmie@1 80 ordering = ('-date_published', )
gremmie@1 81 verbose_name_plural = 'Stories'
gremmie@1 82
gremmie@1 83 def can_comment_on(self):
gremmie@1 84 now = datetime.datetime.now()
gremmie@1 85 delta = now - self.date_published
gremmie@1 86 return delta.days < 30
gremmie@1 87