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 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@240: title = models.CharField(max_length=64) bgneal@240: slug = models.SlugField(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@987: return Story.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: bgneal@204: class StoryBase(models.Model): bgneal@204: """Abstract model to collect common fields.""" 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@204: date_submitted = models.DateTimeField(db_index=True) bgneal@54: allow_comments = models.BooleanField(default=True) bgneal@54: tags = TagField() bgneal@218: front_page_expiration = models.DateField(null=True, blank=True) bgneal@277: update_date = models.DateTimeField(db_index=True, blank=True) bgneal@462: priority = models.IntegerField(db_index=True, default=0, blank=True) bgneal@486: meta_description = models.TextField(blank=True) gremmie@1: bgneal@204: class Meta: bgneal@204: abstract = True bgneal@204: bgneal@204: bgneal@204: class PendingStory(StoryBase): bgneal@204: """Stories submitted by users are held pending admin approval""" bgneal@204: bgneal@182: def save(self, *args, **kwargs): bgneal@204: if not self.pk: bgneal@277: if not self.date_submitted: bgneal@277: self.date_submitted = datetime.datetime.now() bgneal@277: self.update_date = self.date_submitted bgneal@277: else: bgneal@277: self.update_date = datetime.datetime.now() bgneal@204: bgneal@204: 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: bgneal@204: class Story(StoryBase): bgneal@54: """Model for news stories""" 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@204: ordering = ('-date_submitted', ) bgneal@226: verbose_name = 'news story' bgneal@226: verbose_name_plural = 'news stories' gremmie@1: bgneal@277: def save(self, *args, **kwargs): bgneal@277: if not self.pk: bgneal@277: self.date_submitted = datetime.datetime.now() bgneal@277: self.update_date = self.date_submitted bgneal@277: else: bgneal@277: self.update_date = datetime.datetime.now() bgneal@277: bgneal@277: super(Story, self).save(*args, **kwargs) bgneal@277: bgneal@54: def can_comment_on(self): bgneal@54: now = datetime.datetime.now() bgneal@204: delta = now - self.date_submitted bgneal@204: return self.allow_comments and delta.days < 30 bgneal@220: bgneal@220: def search_title(self): bgneal@220: return self.title bgneal@220: bgneal@220: def search_summary(self): bgneal@220: return u"\n".join((self.short_text, self.long_text)) bgneal@484: bgneal@484: def ogp_tags(self): bgneal@484: """ bgneal@484: Returns a dict of Open Graph Protocol meta tags. bgneal@484: bgneal@484: """ bgneal@486: desc = self.meta_description.strip() bgneal@486: if not desc: bgneal@486: desc = 'News article submitted by %s on %s.' % ( bgneal@486: self.submitter.username, bgneal@486: self.date_submitted.strftime('%B %d, %Y')) bgneal@486: bgneal@484: return { bgneal@484: 'og:title': self.title, bgneal@484: 'og:type': 'article', bgneal@484: 'og:url': self.get_absolute_url(), bgneal@484: 'og:image': self.category.icon.url, bgneal@486: 'og:description': desc, bgneal@484: }