comparison gpp/news/models.py @ 54:e249b5f9d180

#3, #4: clear caches when saving profile, bulletins, and news. Broke up the stuff on the home page for finer control of caching.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Jun 2009 00:11:00 +0000
parents e66e718e1e1e
children 5c889b587416
comparison
equal deleted inserted replaced
53:f882ac8ce255 54:e249b5f9d180
3 """ 3 """
4 4
5 import datetime 5 import datetime
6 from django.db import models 6 from django.db import models
7 from django.contrib.auth.models import User 7 from django.contrib.auth.models import User
8 from django.core.cache import cache
8 from tagging.fields import TagField 9 from tagging.fields import TagField
9 10
10 11
11 class Category(models.Model): 12 class Category(models.Model):
12 """News stories belong to categories""" 13 """News stories belong to categories"""
13 title = models.CharField(max_length = 64) 14 title = models.CharField(max_length = 64)
14 icon = models.ImageField(upload_to='news/categories/', blank=True) 15 icon = models.ImageField(upload_to='news/categories/', blank=True)
15 16
16 def __unicode__(self): 17 def __unicode__(self):
17 return self.title 18 return self.title
18 19
19 def num_stories(self): 20 def num_stories(self):
20 return News.objects.filter(category = self.pk).count() 21 return News.objects.filter(category = self.pk).count()
21 22
22 class Meta: 23 class Meta:
23 verbose_name_plural = 'Categories' 24 verbose_name_plural = 'Categories'
24 ordering = ('title', ) 25 ordering = ('title', )
25 26
26 27
27 class PendingStory(models.Model): 28 class PendingStory(models.Model):
28 """Stories submitted by users are held pending admin approval""" 29 """Stories submitted by users are held pending admin approval"""
29 title = models.CharField(max_length=255) 30 title = models.CharField(max_length=255)
30 submitter = models.ForeignKey(User) 31 submitter = models.ForeignKey(User)
31 category = models.ForeignKey(Category) 32 category = models.ForeignKey(Category)
32 short_text = models.TextField() 33 short_text = models.TextField()
33 long_text = models.TextField(blank=True) 34 long_text = models.TextField(blank=True)
34 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True) 35 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True)
35 allow_comments = models.BooleanField(default=True) 36 allow_comments = models.BooleanField(default=True)
36 approved = models.BooleanField(default=False) 37 approved = models.BooleanField(default=False)
37 tags = TagField() 38 tags = TagField()
38 39
39 def save(self, force_insert = False, force_update = False): 40 def save(self, force_insert = False, force_update = False):
40 if self.approved: 41 if self.approved:
41 Story.objects.create(title=self.title, 42 Story.objects.create(title=self.title,
42 submitter=self.submitter, 43 submitter=self.submitter,
43 category=self.category, 44 category=self.category,
44 short_text=self.short_text, 45 short_text=self.short_text,
45 long_text=self.long_text, 46 long_text=self.long_text,
46 allow_comments=self.allow_comments, 47 allow_comments=self.allow_comments,
47 date_published=datetime.datetime.now(), 48 date_published=datetime.datetime.now(),
48 tags=self.tags) 49 tags=self.tags)
49 self.delete() 50 self.delete()
50 else: 51 cache.delete('home_news')
51 super(PendingStory, self).save(force_insert, force_update) 52 else:
53 super(PendingStory, self).save(force_insert, force_update)
52 54
53 def __unicode__(self): 55 def __unicode__(self):
54 return self.title 56 return self.title
55 57
56 class Meta: 58 class Meta:
57 ordering = ('-date_submitted', ) 59 ordering = ('-date_submitted', )
58 verbose_name_plural = 'Pending Stories' 60 verbose_name_plural = 'Pending Stories'
59 61
60 62
61 class Story(models.Model): 63 class Story(models.Model):
62 """Model for news stories""" 64 """Model for news stories"""
63 title = models.CharField(max_length=255) 65 title = models.CharField(max_length=255)
64 submitter = models.ForeignKey(User) 66 submitter = models.ForeignKey(User)
65 category = models.ForeignKey(Category) 67 category = models.ForeignKey(Category)
66 short_text = models.TextField() 68 short_text = models.TextField()
67 long_text = models.TextField(blank=True) 69 long_text = models.TextField(blank=True)
68 allow_comments = models.BooleanField(default=True) 70 allow_comments = models.BooleanField(default=True)
69 date_published = models.DateTimeField(db_index=True) 71 date_published = models.DateTimeField(db_index=True)
70 tags = TagField() 72 tags = TagField()
71 73
72 @models.permalink 74 @models.permalink
73 def get_absolute_url(self): 75 def get_absolute_url(self):
74 return ('news.views.story', [str(self.id)]) 76 return ('news.views.story', [str(self.id)])
75 77
76 def __unicode__(self): 78 def __unicode__(self):
77 return self.title 79 return self.title
78 80
79 class Meta: 81 class Meta:
80 ordering = ('-date_published', ) 82 ordering = ('-date_published', )
81 verbose_name_plural = 'Stories' 83 verbose_name_plural = 'Stories'
82 84
83 def can_comment_on(self): 85 def can_comment_on(self):
84 now = datetime.datetime.now() 86 now = datetime.datetime.now()
85 delta = now - self.date_published 87 delta = now - self.date_published
86 return delta.days < 30 88 return delta.days < 30
87 89
90 def save(self, force_insert=False, force_update=False):
91 super(Story, self).save(force_insert, force_update)
92 cache.delete('home_news')
93