gremmie@1
|
1 """
|
gremmie@1
|
2 Models for the news application.
|
gremmie@1
|
3 """
|
bgneal@997
|
4 import datetime
|
gremmie@1
|
5
|
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
|
bgneal@997
|
10 from core.markup import site_markup
|
bgneal@997
|
11
|
bgneal@997
|
12
|
bgneal@997
|
13 # News App versions
|
bgneal@997
|
14 NEWS_VERSION = 2
|
bgneal@997
|
15
|
gremmie@1
|
16
|
gremmie@1
|
17 class Category(models.Model):
|
bgneal@54
|
18 """News stories belong to categories"""
|
bgneal@240
|
19 title = models.CharField(max_length=64)
|
bgneal@240
|
20 slug = models.SlugField(max_length=64)
|
bgneal@54
|
21 icon = models.ImageField(upload_to='news/categories/', blank=True)
|
gremmie@1
|
22
|
bgneal@54
|
23 def __unicode__(self):
|
bgneal@54
|
24 return self.title
|
gremmie@1
|
25
|
bgneal@54
|
26 def num_stories(self):
|
bgneal@987
|
27 return Story.objects.filter(category=self.pk).count()
|
gremmie@1
|
28
|
bgneal@54
|
29 class Meta:
|
bgneal@54
|
30 verbose_name_plural = 'Categories'
|
bgneal@54
|
31 ordering = ('title', )
|
gremmie@1
|
32
|
gremmie@1
|
33
|
bgneal@204
|
34 class StoryBase(models.Model):
|
bgneal@204
|
35 """Abstract model to collect common fields."""
|
bgneal@54
|
36 title = models.CharField(max_length=255)
|
bgneal@54
|
37 submitter = models.ForeignKey(User)
|
bgneal@54
|
38 category = models.ForeignKey(Category)
|
bgneal@999
|
39 short_text = models.TextField(default='', blank=True)
|
bgneal@998
|
40 long_text = models.TextField(default='', blank=True)
|
bgneal@204
|
41 date_submitted = models.DateTimeField(db_index=True)
|
bgneal@54
|
42 allow_comments = models.BooleanField(default=True)
|
bgneal@54
|
43 tags = TagField()
|
bgneal@218
|
44 front_page_expiration = models.DateField(null=True, blank=True)
|
bgneal@277
|
45 update_date = models.DateTimeField(db_index=True, blank=True)
|
bgneal@462
|
46 priority = models.IntegerField(db_index=True, default=0, blank=True)
|
bgneal@486
|
47 meta_description = models.TextField(blank=True)
|
bgneal@997
|
48 short_markup = models.TextField(default='')
|
bgneal@997
|
49 long_markup = models.TextField(default='', blank=True)
|
bgneal@997
|
50 admin_content = models.TextField(default='', blank=True)
|
bgneal@997
|
51 version = models.SmallIntegerField(default=NEWS_VERSION)
|
gremmie@1
|
52
|
bgneal@204
|
53 class Meta:
|
bgneal@204
|
54 abstract = True
|
bgneal@204
|
55
|
bgneal@182
|
56 def save(self, *args, **kwargs):
|
bgneal@204
|
57 if not self.pk:
|
bgneal@997
|
58 self.date_submitted = datetime.datetime.now()
|
bgneal@277
|
59 self.update_date = self.date_submitted
|
bgneal@277
|
60 else:
|
bgneal@277
|
61 self.update_date = datetime.datetime.now()
|
bgneal@204
|
62
|
bgneal@1000
|
63 if self.version >= 2:
|
bgneal@1000
|
64 self.short_text = kwargs.pop('short_text', '')
|
bgneal@1000
|
65 if not self.short_text and self.short_markup:
|
bgneal@1000
|
66 self.short_text = site_markup(self.short_markup)
|
bgneal@997
|
67
|
bgneal@1000
|
68 self.long_text = kwargs.pop('long_text', '')
|
bgneal@1000
|
69 if not self.long_text and self.long_markup:
|
bgneal@1000
|
70 self.long_text = site_markup(self.long_markup)
|
bgneal@997
|
71
|
bgneal@997
|
72 super(StoryBase, self).save(*args, **kwargs)
|
bgneal@997
|
73
|
bgneal@997
|
74
|
bgneal@997
|
75 class PendingStory(StoryBase):
|
bgneal@997
|
76 """Stories submitted by users are held pending admin approval"""
|
gremmie@1
|
77
|
bgneal@54
|
78 def __unicode__(self):
|
bgneal@54
|
79 return self.title
|
gremmie@1
|
80
|
bgneal@54
|
81 class Meta:
|
bgneal@997
|
82 ordering = ['-date_submitted']
|
bgneal@54
|
83 verbose_name_plural = 'Pending Stories'
|
gremmie@1
|
84
|
gremmie@1
|
85
|
bgneal@204
|
86 class Story(StoryBase):
|
bgneal@54
|
87 """Model for news stories"""
|
gremmie@1
|
88
|
bgneal@54
|
89 @models.permalink
|
bgneal@54
|
90 def get_absolute_url(self):
|
bgneal@54
|
91 return ('news.views.story', [str(self.id)])
|
gremmie@1
|
92
|
bgneal@54
|
93 def __unicode__(self):
|
bgneal@54
|
94 return self.title
|
gremmie@1
|
95
|
bgneal@54
|
96 class Meta:
|
bgneal@997
|
97 ordering = ['-date_submitted']
|
bgneal@226
|
98 verbose_name = 'news story'
|
bgneal@226
|
99 verbose_name_plural = 'news stories'
|
gremmie@1
|
100
|
bgneal@54
|
101 def can_comment_on(self):
|
bgneal@54
|
102 now = datetime.datetime.now()
|
bgneal@204
|
103 delta = now - self.date_submitted
|
bgneal@204
|
104 return self.allow_comments and delta.days < 30
|
bgneal@220
|
105
|
bgneal@220
|
106 def search_title(self):
|
bgneal@220
|
107 return self.title
|
bgneal@220
|
108
|
bgneal@220
|
109 def search_summary(self):
|
bgneal@220
|
110 return u"\n".join((self.short_text, self.long_text))
|
bgneal@484
|
111
|
bgneal@484
|
112 def ogp_tags(self):
|
bgneal@484
|
113 """
|
bgneal@484
|
114 Returns a dict of Open Graph Protocol meta tags.
|
bgneal@484
|
115
|
bgneal@484
|
116 """
|
bgneal@486
|
117 desc = self.meta_description.strip()
|
bgneal@486
|
118 if not desc:
|
bgneal@486
|
119 desc = 'News article submitted by %s on %s.' % (
|
bgneal@486
|
120 self.submitter.username,
|
bgneal@486
|
121 self.date_submitted.strftime('%B %d, %Y'))
|
bgneal@486
|
122
|
bgneal@484
|
123 return {
|
bgneal@484
|
124 'og:title': self.title,
|
bgneal@484
|
125 'og:type': 'article',
|
bgneal@484
|
126 'og:url': self.get_absolute_url(),
|
bgneal@484
|
127 'og:image': self.category.icon.url,
|
bgneal@486
|
128 'og:description': desc,
|
bgneal@484
|
129 }
|