Mercurial > public > sg101
comparison gpp/forums/models.py @ 113:d97ceb95ce02
Forums: ForumLastVisit logic in place. Need to add code for topics and posts next.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 11 Oct 2009 19:10:54 +0000 |
parents | d1b0b86441c0 |
children | 0ce0104c7df3 |
comparison
equal
deleted
inserted
replaced
112:d1b0b86441c0 | 113:d97ceb95ce02 |
---|---|
5 | 5 |
6 from django.db import models | 6 from django.db import models |
7 from django.db.models import Q | 7 from django.db.models import Q |
8 from django.contrib.auth.models import User, Group | 8 from django.contrib.auth.models import User, Group |
9 from django.template.loader import render_to_string | 9 from django.template.loader import render_to_string |
10 | |
11 | |
12 POST_EDIT_DELTA = datetime.timedelta(seconds=3) | |
10 | 13 |
11 | 14 |
12 class Category(models.Model): | 15 class Category(models.Model): |
13 """ | 16 """ |
14 Forums belong to a category, whose access may be assigned to groups. | 17 Forums belong to a category, whose access may be assigned to groups. |
126 self.last_post = \ | 129 self.last_post = \ |
127 Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest() | 130 Post.objects.filter(topic__forum=self).exclude(pk=self.last_post.pk).latest() |
128 except Post.DoesNotExist: | 131 except Post.DoesNotExist: |
129 self.last_post = None | 132 self.last_post = None |
130 | 133 |
134 def catchup(self, user, flv=None): | |
135 """ | |
136 Call to mark this forum all caught up for the given user (i.e. mark all topics | |
137 read for this user). | |
138 """ | |
139 TopicLastVisit.objects.filter(user=user, topic__forum=self).delete() | |
140 if flv is None: | |
141 try: | |
142 flv = ForumLastVisit.objects.get(user=user, forum=self) | |
143 except ForumLastVisit.DoesNotExist: | |
144 flv = ForumLastVisit(user=user, forum=self) | |
145 | |
146 now = datetime.datetime.now() | |
147 flv.begin_date = now | |
148 flv.end_date = now | |
149 flv.save() | |
150 | |
131 | 151 |
132 class Topic(models.Model): | 152 class Topic(models.Model): |
133 """ | 153 """ |
134 A topic is a thread of discussion, consisting of a series of posts. | 154 A topic is a thread of discussion, consisting of a series of posts. |
135 """ | 155 """ |
239 first_post_id = self.topic.posts.all()[0].id | 259 first_post_id = self.topic.posts.all()[0].id |
240 super(Post, self).delete(*args, **kwargs) | 260 super(Post, self).delete(*args, **kwargs) |
241 if self.id == first_post_id: | 261 if self.id == first_post_id: |
242 self.topic.delete() | 262 self.topic.delete() |
243 | 263 |
264 def has_been_edited(self): | |
265 return (self.update_date - self.creation_date) > POST_EDIT_DELTA | |
266 | |
244 | 267 |
245 class FlaggedPost(models.Model): | 268 class FlaggedPost(models.Model): |
246 """This model represents a user flagging a post as inappropriate.""" | 269 """This model represents a user flagging a post as inappropriate.""" |
247 user = models.ForeignKey(User) | 270 user = models.ForeignKey(User) |
248 post = models.ForeignKey(Post) | 271 post = models.ForeignKey(Post) |
256 | 279 |
257 def get_post_url(self): | 280 def get_post_url(self): |
258 return '<a href="%s">Post</a>' % self.post.get_absolute_url() | 281 return '<a href="%s">Post</a>' % self.post.get_absolute_url() |
259 get_post_url.allow_tags = True | 282 get_post_url.allow_tags = True |
260 | 283 |
261 # TODO: A "read" table | 284 |
285 class ForumLastVisit(models.Model): | |
286 """ | |
287 This model records the last time a user visited a forum. | |
288 It is used to compute if a user has unread topics in a forum. | |
289 We keep track of a window of time, delimited by begin_date and end_date. | |
290 Topics updated within this window are tracked, and may have TopicLastVisit | |
291 objects. | |
292 Marking a forum as all read sets the begin_date equal to the end_date. | |
293 """ | |
294 user = models.ForeignKey(User) | |
295 forum = models.ForeignKey(Forum) | |
296 begin_date = models.DateTimeField() | |
297 end_date = models.DateTimeField() | |
298 | |
299 class Meta: | |
300 unique_together = ('user', 'forum') | |
301 ordering = ('-end_date', ) | |
302 | |
303 def __unicode__(self): | |
304 return u'Forum: %d User: %d Date: %s' % (self.forum.id, self.user.id, | |
305 self.end_date.strftime('%Y-%m-%d %H:%M:%S')) | |
306 | |
307 def is_caught_up(self): | |
308 return self.begin_date == self.end_date | |
309 | |
310 | |
311 class TopicLastVisit(models.Model): | |
312 """ | |
313 This model records the last time a user read a topic. | |
314 Objects of this class exist for the window specified in the | |
315 corresponding ForumLastVisit object. | |
316 """ | |
317 user = models.ForeignKey(User) | |
318 topic = models.ForeignKey(Topic) | |
319 last_visit = models.DateTimeField() | |
320 | |
321 class Meta: | |
322 unique_together = ('user', 'topic') | |
323 ordering = ('-last_visit', ) | |
324 | |
325 def __unicode__(self): | |
326 return u'Topic: %d User: %d Date: %s' % (self.topic.id, self.user.id, | |
327 self.last_visit.strftime('%Y-%m-%d %H:%M:%S')) | |
328 | |
329 def save(self, *args, **kwargs): | |
330 if self.id is None: | |
331 self.touch() | |
332 super(TopicLastVisit, self).save(*args, **kwargs) | |
333 | |
334 def touch(self): | |
335 self.last_visit = datetime.datetime.now() |