comparison gpp/forums/models.py @ 102:e67c4dd98db5

Forums: new topic form sprouts boolean fields for sticky and locking if the user has rights. Implemented the locked logic. Fixed a bug where topics where getting out of order (the view_count was bumping the update_date because of auto_now).
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 02:01:57 +0000
parents eb9f99382476
children e94398f5e027
comparison
equal deleted inserted replaced
101:4bbb6a9aa317 102:e67c4dd98db5
1 """ 1 """
2 Models for the forums application. 2 Models for the forums application.
3 """ 3 """
4 import datetime
5
4 from django.db import models 6 from django.db import models
5 from django.db.models import Q 7 from django.db.models import Q
6 from django.contrib.auth.models import User, Group 8 from django.contrib.auth.models import User, Group
7 from django.template.loader import render_to_string 9 from django.template.loader import render_to_string
8 10
119 sticky = models.BooleanField(blank=True, default=False) 121 sticky = models.BooleanField(blank=True, default=False)
120 locked = models.BooleanField(blank=True, default=False) 122 locked = models.BooleanField(blank=True, default=False)
121 123
122 # denormalized fields to reduce database hits 124 # denormalized fields to reduce database hits
123 post_count = models.IntegerField(blank=True, default=0) 125 post_count = models.IntegerField(blank=True, default=0)
124 update_date = models.DateTimeField(auto_now=True) 126 update_date = models.DateTimeField()
125 last_post = models.OneToOneField('Post', blank=True, null=True, 127 last_post = models.OneToOneField('Post', blank=True, null=True,
126 related_name='parent_topic') 128 related_name='parent_topic')
127 129
128 class Meta: 130 class Meta:
129 ordering = ('-sticky', '-update_date', ) 131 ordering = ('-sticky', '-update_date', )
156 """ 158 """
157 if self.post_count > 1: 159 if self.post_count > 1:
158 return self.post_count - 1 160 return self.post_count - 1
159 return 0 161 return 0
160 162
163 def save(self, *args, **kwargs):
164 if not self.id:
165 now = datetime.datetime.now()
166 self.creation_date = now
167 self.update_date = now
168
169 super(Topic, self).save(*args, **kwargs)
170
161 171
162 class Post(models.Model): 172 class Post(models.Model):
163 """ 173 """
164 A post is an instance of a user's single contribution to a topic. 174 A post is an instance of a user's single contribution to a topic.
165 """ 175 """