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
|
gremmie@1
|
8 from tagging.fields import TagField
|
gremmie@1
|
9
|
gremmie@1
|
10
|
gremmie@1
|
11 class Category(models.Model):
|
bgneal@54
|
12 """News stories belong to categories"""
|
bgneal@240
|
13 title = models.CharField(max_length=64)
|
bgneal@240
|
14 slug = models.SlugField(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()
|
bgneal@218
|
38 front_page_expiration = models.DateField(null=True, blank=True)
|
gremmie@1
|
39
|
bgneal@204
|
40 class Meta:
|
bgneal@204
|
41 abstract = True
|
bgneal@204
|
42
|
bgneal@204
|
43
|
bgneal@204
|
44 class PendingStory(StoryBase):
|
bgneal@204
|
45 """Stories submitted by users are held pending admin approval"""
|
bgneal@204
|
46
|
bgneal@182
|
47 def save(self, *args, **kwargs):
|
bgneal@204
|
48 if not self.pk:
|
bgneal@204
|
49 self.date_submitted = datetime.datetime.now()
|
bgneal@204
|
50
|
bgneal@204
|
51 super(PendingStory, self).save(*args, **kwargs)
|
gremmie@1
|
52
|
bgneal@54
|
53 def __unicode__(self):
|
bgneal@54
|
54 return self.title
|
gremmie@1
|
55
|
bgneal@54
|
56 class Meta:
|
bgneal@54
|
57 ordering = ('-date_submitted', )
|
bgneal@54
|
58 verbose_name_plural = 'Pending Stories'
|
gremmie@1
|
59
|
gremmie@1
|
60
|
bgneal@204
|
61 class Story(StoryBase):
|
bgneal@54
|
62 """Model for news stories"""
|
gremmie@1
|
63
|
bgneal@54
|
64 @models.permalink
|
bgneal@54
|
65 def get_absolute_url(self):
|
bgneal@54
|
66 return ('news.views.story', [str(self.id)])
|
gremmie@1
|
67
|
bgneal@54
|
68 def __unicode__(self):
|
bgneal@54
|
69 return self.title
|
gremmie@1
|
70
|
bgneal@54
|
71 class Meta:
|
bgneal@204
|
72 ordering = ('-date_submitted', )
|
bgneal@226
|
73 verbose_name = 'news story'
|
bgneal@226
|
74 verbose_name_plural = 'news stories'
|
gremmie@1
|
75
|
bgneal@54
|
76 def can_comment_on(self):
|
bgneal@54
|
77 now = datetime.datetime.now()
|
bgneal@204
|
78 delta = now - self.date_submitted
|
bgneal@204
|
79 return self.allow_comments and delta.days < 30
|
bgneal@220
|
80
|
bgneal@220
|
81 def search_title(self):
|
bgneal@220
|
82 return self.title
|
bgneal@220
|
83
|
bgneal@220
|
84 def search_summary(self):
|
bgneal@220
|
85 return u"\n".join((self.short_text, self.long_text))
|