Mercurial > public > sg101
comparison gpp/news/models.py @ 204:b4305e18d3af
Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 01 May 2010 21:53:59 +0000 |
parents | 5c889b587416 |
children | 65016249bf35 |
comparison
equal
deleted
inserted
replaced
203:40e5903903e1 | 204:b4305e18d3af |
---|---|
23 class Meta: | 23 class Meta: |
24 verbose_name_plural = 'Categories' | 24 verbose_name_plural = 'Categories' |
25 ordering = ('title', ) | 25 ordering = ('title', ) |
26 | 26 |
27 | 27 |
28 class PendingStory(models.Model): | 28 class StoryBase(models.Model): |
29 """Stories submitted by users are held pending admin approval""" | 29 """Abstract model to collect common fields.""" |
30 title = models.CharField(max_length=255) | 30 title = models.CharField(max_length=255) |
31 submitter = models.ForeignKey(User) | 31 submitter = models.ForeignKey(User) |
32 category = models.ForeignKey(Category) | 32 category = models.ForeignKey(Category) |
33 short_text = models.TextField() | 33 short_text = models.TextField() |
34 long_text = models.TextField(blank=True) | 34 long_text = models.TextField(blank=True) |
35 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True) | 35 date_submitted = models.DateTimeField(db_index=True) |
36 allow_comments = models.BooleanField(default=True) | 36 allow_comments = models.BooleanField(default=True) |
37 approved = models.BooleanField(default=False) | |
38 tags = TagField() | 37 tags = TagField() |
39 | 38 |
39 class Meta: | |
40 abstract = True | |
41 | |
42 | |
43 class PendingStory(StoryBase): | |
44 """Stories submitted by users are held pending admin approval""" | |
45 | |
40 def save(self, *args, **kwargs): | 46 def save(self, *args, **kwargs): |
41 if self.approved: | 47 if not self.pk: |
42 Story.objects.create(title=self.title, | 48 self.date_submitted = datetime.datetime.now() |
43 submitter=self.submitter, | 49 |
44 category=self.category, | 50 super(PendingStory, self).save(*args, **kwargs) |
45 short_text=self.short_text, | |
46 long_text=self.long_text, | |
47 allow_comments=self.allow_comments, | |
48 date_published=datetime.datetime.now(), | |
49 tags=self.tags) | |
50 self.delete() | |
51 cache.delete('home_news') | |
52 else: | |
53 super(PendingStory, self).save(*args, **kwargs) | |
54 | 51 |
55 def __unicode__(self): | 52 def __unicode__(self): |
56 return self.title | 53 return self.title |
57 | 54 |
58 class Meta: | 55 class Meta: |
59 ordering = ('-date_submitted', ) | 56 ordering = ('-date_submitted', ) |
60 verbose_name_plural = 'Pending Stories' | 57 verbose_name_plural = 'Pending Stories' |
61 | 58 |
62 | 59 |
63 class Story(models.Model): | 60 class Story(StoryBase): |
64 """Model for news stories""" | 61 """Model for news stories""" |
65 title = models.CharField(max_length=255) | |
66 submitter = models.ForeignKey(User) | |
67 category = models.ForeignKey(Category) | |
68 short_text = models.TextField() | |
69 long_text = models.TextField(blank=True) | |
70 allow_comments = models.BooleanField(default=True) | |
71 date_published = models.DateTimeField(db_index=True) | |
72 tags = TagField() | |
73 | 62 |
74 @models.permalink | 63 @models.permalink |
75 def get_absolute_url(self): | 64 def get_absolute_url(self): |
76 return ('news.views.story', [str(self.id)]) | 65 return ('news.views.story', [str(self.id)]) |
77 | 66 |
78 def __unicode__(self): | 67 def __unicode__(self): |
79 return self.title | 68 return self.title |
80 | 69 |
81 class Meta: | 70 class Meta: |
82 ordering = ('-date_published', ) | 71 ordering = ('-date_submitted', ) |
83 verbose_name_plural = 'Stories' | 72 verbose_name_plural = 'Stories' |
84 | 73 |
85 def can_comment_on(self): | 74 def can_comment_on(self): |
86 now = datetime.datetime.now() | 75 now = datetime.datetime.now() |
87 delta = now - self.date_published | 76 delta = now - self.date_submitted |
88 return delta.days < 30 | 77 return self.allow_comments and delta.days < 30 |
89 | 78 |
90 def save(self, *args, **kwargs): | 79 def save(self, *args, **kwargs): |
91 super(Story, self).save(*args, **kwargs) | 80 super(Story, self).save(*args, **kwargs) |
92 cache.delete('home_news') | 81 cache.delete('home_news') |
93 | 82 |