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
|
bgneal@54
|
8 from django.core.cache import cache
|
gremmie@1
|
9 from tagging.fields import TagField
|
gremmie@1
|
10
|
gremmie@1
|
11
|
gremmie@1
|
12 class Category(models.Model):
|
bgneal@54
|
13 """News stories belong to categories"""
|
bgneal@54
|
14 title = models.CharField(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
|
gremmie@1
|
28 class PendingStory(models.Model):
|
bgneal@54
|
29 """Stories submitted by users are held pending admin approval"""
|
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@54
|
35 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True)
|
bgneal@54
|
36 allow_comments = models.BooleanField(default=True)
|
bgneal@54
|
37 approved = models.BooleanField(default=False)
|
bgneal@54
|
38 tags = TagField()
|
gremmie@1
|
39
|
bgneal@54
|
40 def save(self, force_insert = False, force_update = False):
|
bgneal@54
|
41 if self.approved:
|
bgneal@54
|
42 Story.objects.create(title=self.title,
|
bgneal@54
|
43 submitter=self.submitter,
|
bgneal@54
|
44 category=self.category,
|
bgneal@54
|
45 short_text=self.short_text,
|
bgneal@54
|
46 long_text=self.long_text,
|
bgneal@54
|
47 allow_comments=self.allow_comments,
|
bgneal@54
|
48 date_published=datetime.datetime.now(),
|
bgneal@54
|
49 tags=self.tags)
|
bgneal@54
|
50 self.delete()
|
bgneal@54
|
51 cache.delete('home_news')
|
bgneal@54
|
52 else:
|
bgneal@54
|
53 super(PendingStory, self).save(force_insert, force_update)
|
gremmie@1
|
54
|
bgneal@54
|
55 def __unicode__(self):
|
bgneal@54
|
56 return self.title
|
gremmie@1
|
57
|
bgneal@54
|
58 class Meta:
|
bgneal@54
|
59 ordering = ('-date_submitted', )
|
bgneal@54
|
60 verbose_name_plural = 'Pending Stories'
|
gremmie@1
|
61
|
gremmie@1
|
62
|
gremmie@1
|
63 class Story(models.Model):
|
bgneal@54
|
64 """Model for news stories"""
|
bgneal@54
|
65 title = models.CharField(max_length=255)
|
bgneal@54
|
66 submitter = models.ForeignKey(User)
|
bgneal@54
|
67 category = models.ForeignKey(Category)
|
bgneal@54
|
68 short_text = models.TextField()
|
bgneal@54
|
69 long_text = models.TextField(blank=True)
|
bgneal@54
|
70 allow_comments = models.BooleanField(default=True)
|
bgneal@54
|
71 date_published = models.DateTimeField(db_index=True)
|
bgneal@54
|
72 tags = TagField()
|
gremmie@1
|
73
|
bgneal@54
|
74 @models.permalink
|
bgneal@54
|
75 def get_absolute_url(self):
|
bgneal@54
|
76 return ('news.views.story', [str(self.id)])
|
gremmie@1
|
77
|
bgneal@54
|
78 def __unicode__(self):
|
bgneal@54
|
79 return self.title
|
gremmie@1
|
80
|
bgneal@54
|
81 class Meta:
|
bgneal@54
|
82 ordering = ('-date_published', )
|
bgneal@54
|
83 verbose_name_plural = 'Stories'
|
gremmie@1
|
84
|
bgneal@54
|
85 def can_comment_on(self):
|
bgneal@54
|
86 now = datetime.datetime.now()
|
bgneal@54
|
87 delta = now - self.date_published
|
bgneal@54
|
88 return delta.days < 30
|
gremmie@1
|
89
|
bgneal@54
|
90 def save(self, force_insert=False, force_update=False):
|
bgneal@54
|
91 super(Story, self).save(force_insert, force_update)
|
bgneal@54
|
92 cache.delete('home_news')
|
bgneal@54
|
93
|