annotate gpp/news/models.py @ 484:bbbc357ac5f3

For #233; first commit for adding social media sharing buttons to news stories.
author Brian Neal <bgneal@gmail.com>
date Tue, 18 Oct 2011 00:04:37 +0000
parents 53fdaf0da539
children 7854d75427af
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)
bgneal@462 40 priority = models.IntegerField(db_index=True, default=0, blank=True)
gremmie@1 41
bgneal@204 42 class Meta:
bgneal@204 43 abstract = True
bgneal@204 44
bgneal@204 45
bgneal@204 46 class PendingStory(StoryBase):
bgneal@204 47 """Stories submitted by users are held pending admin approval"""
bgneal@204 48
bgneal@182 49 def save(self, *args, **kwargs):
bgneal@204 50 if not self.pk:
bgneal@277 51 if not self.date_submitted:
bgneal@277 52 self.date_submitted = datetime.datetime.now()
bgneal@277 53 self.update_date = self.date_submitted
bgneal@277 54 else:
bgneal@277 55 self.update_date = datetime.datetime.now()
bgneal@204 56
bgneal@204 57 super(PendingStory, self).save(*args, **kwargs)
gremmie@1 58
bgneal@54 59 def __unicode__(self):
bgneal@54 60 return self.title
gremmie@1 61
bgneal@54 62 class Meta:
bgneal@54 63 ordering = ('-date_submitted', )
bgneal@54 64 verbose_name_plural = 'Pending Stories'
gremmie@1 65
gremmie@1 66
bgneal@204 67 class Story(StoryBase):
bgneal@54 68 """Model for news stories"""
gremmie@1 69
bgneal@54 70 @models.permalink
bgneal@54 71 def get_absolute_url(self):
bgneal@54 72 return ('news.views.story', [str(self.id)])
gremmie@1 73
bgneal@54 74 def __unicode__(self):
bgneal@54 75 return self.title
gremmie@1 76
bgneal@54 77 class Meta:
bgneal@204 78 ordering = ('-date_submitted', )
bgneal@226 79 verbose_name = 'news story'
bgneal@226 80 verbose_name_plural = 'news stories'
gremmie@1 81
bgneal@277 82 def save(self, *args, **kwargs):
bgneal@277 83 if not self.pk:
bgneal@277 84 self.date_submitted = datetime.datetime.now()
bgneal@277 85 self.update_date = self.date_submitted
bgneal@277 86 else:
bgneal@277 87 self.update_date = datetime.datetime.now()
bgneal@277 88
bgneal@277 89 super(Story, self).save(*args, **kwargs)
bgneal@277 90
bgneal@54 91 def can_comment_on(self):
bgneal@54 92 now = datetime.datetime.now()
bgneal@204 93 delta = now - self.date_submitted
bgneal@204 94 return self.allow_comments and delta.days < 30
bgneal@220 95
bgneal@220 96 def search_title(self):
bgneal@220 97 return self.title
bgneal@220 98
bgneal@220 99 def search_summary(self):
bgneal@220 100 return u"\n".join((self.short_text, self.long_text))
bgneal@484 101
bgneal@484 102 def ogp_tags(self):
bgneal@484 103 """
bgneal@484 104 Returns a dict of Open Graph Protocol meta tags.
bgneal@484 105
bgneal@484 106 """
bgneal@484 107 return {
bgneal@484 108 'og:title': self.title,
bgneal@484 109 'og:type': 'article',
bgneal@484 110 'og:url': self.get_absolute_url(),
bgneal@484 111 'og:image': self.category.icon.url,
bgneal@484 112 'og:description': self.title,
bgneal@484 113 }