Mercurial > public > sg101
comparison gpp/news/models.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | e66e718e1e1e |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
1 """ | |
2 Models for the news application. | |
3 """ | |
4 | |
5 import datetime | |
6 from django.db import models | |
7 from django.contrib import auth | |
8 from tagging.fields import TagField | |
9 | |
10 | |
11 class Category(models.Model): | |
12 """News stories belong to categories""" | |
13 title = models.CharField(max_length = 64) | |
14 icon = models.ImageField(upload_to='news/categories/', blank=True) | |
15 | |
16 def __unicode__(self): | |
17 return self.title | |
18 | |
19 def num_stories(self): | |
20 return News.objects.filter(category = self.pk).count() | |
21 | |
22 class Meta: | |
23 verbose_name_plural = 'Categories' | |
24 ordering = ('title', ) | |
25 | |
26 | |
27 class PendingStory(models.Model): | |
28 """Stories submitted by users are held pending admin approval""" | |
29 title = models.CharField(max_length=255) | |
30 submitter = models.ForeignKey(auth.models.User) | |
31 category = models.ForeignKey(Category) | |
32 short_text = models.TextField() | |
33 long_text = models.TextField(blank=True) | |
34 date_submitted = models.DateTimeField(auto_now_add=True, db_index=True) | |
35 allow_comments = models.BooleanField(default=True) | |
36 approved = models.BooleanField(default=False) | |
37 tags = TagField() | |
38 | |
39 def save(self, force_insert = False, force_update = False): | |
40 if self.approved: | |
41 Story.objects.create(title=self.title, | |
42 submitter=self.submitter, | |
43 category=self.category, | |
44 short_text=self.short_text, | |
45 long_text=self.long_text, | |
46 allow_comments=self.allow_comments, | |
47 date_published=datetime.datetime.now(), | |
48 tags=self.tags) | |
49 self.delete() | |
50 else: | |
51 super(PendingStory, self).save(force_insert, force_update) | |
52 | |
53 def __unicode__(self): | |
54 return self.title | |
55 | |
56 class Meta: | |
57 ordering = ('-date_submitted', ) | |
58 verbose_name_plural = 'Pending Stories' | |
59 | |
60 | |
61 class Story(models.Model): | |
62 """Model for news stories""" | |
63 title = models.CharField(max_length=255) | |
64 submitter = models.ForeignKey(auth.models.User) | |
65 category = models.ForeignKey(Category) | |
66 short_text = models.TextField() | |
67 long_text = models.TextField(blank=True) | |
68 allow_comments = models.BooleanField(default=True) | |
69 date_published = models.DateTimeField(db_index=True) | |
70 tags = TagField() | |
71 | |
72 @models.permalink | |
73 def get_absolute_url(self): | |
74 return ('news.views.story', [str(self.id)]) | |
75 | |
76 def __unicode__(self): | |
77 return self.title | |
78 | |
79 class Meta: | |
80 ordering = ('-date_published', ) | |
81 verbose_name_plural = 'Stories' | |
82 | |
83 def can_comment_on(self): | |
84 now = datetime.datetime.now() | |
85 delta = now - self.date_published | |
86 return delta.days < 30 | |
87 |