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
|
bgneal@204
|
28 class StoryBase(models.Model):
|
bgneal@204
|
29 """Abstract model to collect common fields."""
|
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@204
|
35 date_submitted = models.DateTimeField(db_index=True)
|
bgneal@54
|
36 allow_comments = models.BooleanField(default=True)
|
bgneal@54
|
37 tags = TagField()
|
gremmie@1
|
38
|
bgneal@204
|
39 class Meta:
|
bgneal@204
|
40 abstract = True
|
bgneal@204
|
41
|
bgneal@204
|
42
|
bgneal@204
|
43 class PendingStory(StoryBase):
|
bgneal@204
|
44 """Stories submitted by users are held pending admin approval"""
|
bgneal@204
|
45
|
bgneal@182
|
46 def save(self, *args, **kwargs):
|
bgneal@204
|
47 if not self.pk:
|
bgneal@204
|
48 self.date_submitted = datetime.datetime.now()
|
bgneal@204
|
49
|
bgneal@204
|
50 super(PendingStory, self).save(*args, **kwargs)
|
gremmie@1
|
51
|
bgneal@54
|
52 def __unicode__(self):
|
bgneal@54
|
53 return self.title
|
gremmie@1
|
54
|
bgneal@54
|
55 class Meta:
|
bgneal@54
|
56 ordering = ('-date_submitted', )
|
bgneal@54
|
57 verbose_name_plural = 'Pending Stories'
|
gremmie@1
|
58
|
gremmie@1
|
59
|
bgneal@204
|
60 class Story(StoryBase):
|
bgneal@54
|
61 """Model for news stories"""
|
gremmie@1
|
62
|
bgneal@54
|
63 @models.permalink
|
bgneal@54
|
64 def get_absolute_url(self):
|
bgneal@54
|
65 return ('news.views.story', [str(self.id)])
|
gremmie@1
|
66
|
bgneal@54
|
67 def __unicode__(self):
|
bgneal@54
|
68 return self.title
|
gremmie@1
|
69
|
bgneal@54
|
70 class Meta:
|
bgneal@204
|
71 ordering = ('-date_submitted', )
|
bgneal@54
|
72 verbose_name_plural = 'Stories'
|
gremmie@1
|
73
|
bgneal@54
|
74 def can_comment_on(self):
|
bgneal@54
|
75 now = datetime.datetime.now()
|
bgneal@204
|
76 delta = now - self.date_submitted
|
bgneal@204
|
77 return self.allow_comments and delta.days < 30
|
gremmie@1
|
78
|
bgneal@182
|
79 def save(self, *args, **kwargs):
|
bgneal@182
|
80 super(Story, self).save(*args, **kwargs)
|
bgneal@54
|
81 cache.delete('home_news')
|
bgneal@54
|
82
|