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