Mercurial > public > sg101
comparison news/models.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/news/models.py@7854d75427af |
children | 76525f5ac2b1 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Models for the news application. | |
3 """ | |
4 | |
5 import datetime | |
6 from django.db import models | |
7 from django.contrib.auth.models import User | |
8 from tagging.fields import TagField | |
9 | |
10 | |
11 class Category(models.Model): | |
12 """News stories belong to categories""" | |
13 title = models.CharField(max_length=64) | |
14 slug = models.SlugField(max_length=64) | |
15 icon = models.ImageField(upload_to='news/categories/', blank=True) | |
16 | |
17 def __unicode__(self): | |
18 return self.title | |
19 | |
20 def num_stories(self): | |
21 return News.objects.filter(category = self.pk).count() | |
22 | |
23 class Meta: | |
24 verbose_name_plural = 'Categories' | |
25 ordering = ('title', ) | |
26 | |
27 | |
28 class StoryBase(models.Model): | |
29 """Abstract model to collect common fields.""" | |
30 title = models.CharField(max_length=255) | |
31 submitter = models.ForeignKey(User) | |
32 category = models.ForeignKey(Category) | |
33 short_text = models.TextField() | |
34 long_text = models.TextField(blank=True) | |
35 date_submitted = models.DateTimeField(db_index=True) | |
36 allow_comments = models.BooleanField(default=True) | |
37 tags = TagField() | |
38 front_page_expiration = models.DateField(null=True, blank=True) | |
39 update_date = models.DateTimeField(db_index=True, blank=True) | |
40 priority = models.IntegerField(db_index=True, default=0, blank=True) | |
41 meta_description = models.TextField(blank=True) | |
42 | |
43 class Meta: | |
44 abstract = True | |
45 | |
46 | |
47 class PendingStory(StoryBase): | |
48 """Stories submitted by users are held pending admin approval""" | |
49 | |
50 def save(self, *args, **kwargs): | |
51 if not self.pk: | |
52 if not self.date_submitted: | |
53 self.date_submitted = datetime.datetime.now() | |
54 self.update_date = self.date_submitted | |
55 else: | |
56 self.update_date = datetime.datetime.now() | |
57 | |
58 super(PendingStory, self).save(*args, **kwargs) | |
59 | |
60 def __unicode__(self): | |
61 return self.title | |
62 | |
63 class Meta: | |
64 ordering = ('-date_submitted', ) | |
65 verbose_name_plural = 'Pending Stories' | |
66 | |
67 | |
68 class Story(StoryBase): | |
69 """Model for news stories""" | |
70 | |
71 @models.permalink | |
72 def get_absolute_url(self): | |
73 return ('news.views.story', [str(self.id)]) | |
74 | |
75 def __unicode__(self): | |
76 return self.title | |
77 | |
78 class Meta: | |
79 ordering = ('-date_submitted', ) | |
80 verbose_name = 'news story' | |
81 verbose_name_plural = 'news stories' | |
82 | |
83 def save(self, *args, **kwargs): | |
84 if not self.pk: | |
85 self.date_submitted = datetime.datetime.now() | |
86 self.update_date = self.date_submitted | |
87 else: | |
88 self.update_date = datetime.datetime.now() | |
89 | |
90 super(Story, self).save(*args, **kwargs) | |
91 | |
92 def can_comment_on(self): | |
93 now = datetime.datetime.now() | |
94 delta = now - self.date_submitted | |
95 return self.allow_comments and delta.days < 30 | |
96 | |
97 def search_title(self): | |
98 return self.title | |
99 | |
100 def search_summary(self): | |
101 return u"\n".join((self.short_text, self.long_text)) | |
102 | |
103 def ogp_tags(self): | |
104 """ | |
105 Returns a dict of Open Graph Protocol meta tags. | |
106 | |
107 """ | |
108 desc = self.meta_description.strip() | |
109 if not desc: | |
110 desc = 'News article submitted by %s on %s.' % ( | |
111 self.submitter.username, | |
112 self.date_submitted.strftime('%B %d, %Y')) | |
113 | |
114 return { | |
115 'og:title': self.title, | |
116 'og:type': 'article', | |
117 'og:url': self.get_absolute_url(), | |
118 'og:image': self.category.icon.url, | |
119 'og:description': desc, | |
120 } |