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@54
|
13 title = models.CharField(max_length = 64)
|
bgneal@54
|
14 icon = models.ImageField(upload_to='news/categories/', blank=True)
|
gremmie@1
|
15
|
bgneal@54
|
16 def __unicode__(self):
|
bgneal@54
|
17 return self.title
|
gremmie@1
|
18
|
bgneal@54
|
19 def num_stories(self):
|
bgneal@54
|
20 return News.objects.filter(category = self.pk).count()
|
gremmie@1
|
21
|
bgneal@54
|
22 class Meta:
|
bgneal@54
|
23 verbose_name_plural = 'Categories'
|
bgneal@54
|
24 ordering = ('title', )
|
gremmie@1
|
25
|
gremmie@1
|
26
|
bgneal@204
|
27 class StoryBase(models.Model):
|
bgneal@204
|
28 """Abstract model to collect common fields."""
|
bgneal@54
|
29 title = models.CharField(max_length=255)
|
bgneal@54
|
30 submitter = models.ForeignKey(User)
|
bgneal@54
|
31 category = models.ForeignKey(Category)
|
bgneal@54
|
32 short_text = models.TextField()
|
bgneal@54
|
33 long_text = models.TextField(blank=True)
|
bgneal@204
|
34 date_submitted = models.DateTimeField(db_index=True)
|
bgneal@54
|
35 allow_comments = models.BooleanField(default=True)
|
bgneal@54
|
36 tags = TagField()
|
gremmie@1
|
37
|
bgneal@204
|
38 class Meta:
|
bgneal@204
|
39 abstract = True
|
bgneal@204
|
40
|
bgneal@204
|
41
|
bgneal@204
|
42 class PendingStory(StoryBase):
|
bgneal@204
|
43 """Stories submitted by users are held pending admin approval"""
|
bgneal@204
|
44
|
bgneal@182
|
45 def save(self, *args, **kwargs):
|
bgneal@204
|
46 if not self.pk:
|
bgneal@204
|
47 self.date_submitted = datetime.datetime.now()
|
bgneal@204
|
48
|
bgneal@204
|
49 super(PendingStory, self).save(*args, **kwargs)
|
gremmie@1
|
50
|
bgneal@54
|
51 def __unicode__(self):
|
bgneal@54
|
52 return self.title
|
gremmie@1
|
53
|
bgneal@54
|
54 class Meta:
|
bgneal@54
|
55 ordering = ('-date_submitted', )
|
bgneal@54
|
56 verbose_name_plural = 'Pending Stories'
|
gremmie@1
|
57
|
gremmie@1
|
58
|
bgneal@204
|
59 class Story(StoryBase):
|
bgneal@54
|
60 """Model for news stories"""
|
gremmie@1
|
61
|
bgneal@54
|
62 @models.permalink
|
bgneal@54
|
63 def get_absolute_url(self):
|
bgneal@54
|
64 return ('news.views.story', [str(self.id)])
|
gremmie@1
|
65
|
bgneal@54
|
66 def __unicode__(self):
|
bgneal@54
|
67 return self.title
|
gremmie@1
|
68
|
bgneal@54
|
69 class Meta:
|
bgneal@204
|
70 ordering = ('-date_submitted', )
|
bgneal@54
|
71 verbose_name_plural = 'Stories'
|
gremmie@1
|
72
|
bgneal@54
|
73 def can_comment_on(self):
|
bgneal@54
|
74 now = datetime.datetime.now()
|
bgneal@204
|
75 delta = now - self.date_submitted
|
bgneal@204
|
76 return self.allow_comments and delta.days < 30
|