Mercurial > public > sg101
comparison gpp/forums/models.py @ 167:cf9f9d4c4d54
Adding a query to the forums to get all the topics with unread posts. This is for ticket #54.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 24 Jan 2010 22:33:11 +0000 |
parents | f7a6b8fe4556 |
children | 6f14970b103a |
comparison
equal
deleted
inserted
replaced
166:8acf5be27f18 | 167:cf9f9d4c4d54 |
---|---|
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 | 9 |
10 from core.markup import site_markup | 10 from core.markup import site_markup |
11 | |
12 | |
13 POST_EDIT_DELTA = datetime.timedelta(seconds=3) | |
14 | 11 |
15 | 12 |
16 class Category(models.Model): | 13 class Category(models.Model): |
17 """ | 14 """ |
18 Forums belong to a category, whose access may be assigned to groups. | 15 Forums belong to a category, whose access may be assigned to groups. |
55 def forums_for_user(self, user): | 52 def forums_for_user(self, user): |
56 """ | 53 """ |
57 Returns a queryset containing the forums that the given user can | 54 Returns a queryset containing the forums that the given user can |
58 "see" due to authenticated status, superuser status and group membership. | 55 "see" due to authenticated status, superuser status and group membership. |
59 """ | 56 """ |
57 qs = self._for_user(user) | |
58 return qs.select_related('category', 'last_post', 'last_post__user') | |
59 | |
60 def forum_ids_for_user(self, user): | |
61 """Returns a list of forum IDs that the given user can "see".""" | |
62 qs = self._for_user(user) | |
63 return qs.values_list('id', flat=True) | |
64 | |
65 def _for_user(self, user): | |
66 """Common code for the xxx_for_user() methods.""" | |
60 if user.is_superuser: | 67 if user.is_superuser: |
61 qs = self.all() | 68 qs = self.all() |
62 else: | 69 else: |
63 user_groups = [] | 70 user_groups = user.groups.all() if user.is_authenticated() else [] |
64 if user.is_authenticated(): | 71 qs = self.filter(Q(category__groups__isnull=True) | |
65 user_groups = user.groups.all() | |
66 | |
67 qs = self.filter(Q(category__groups__isnull=True) | \ | |
68 Q(category__groups__in=user_groups)) | 72 Q(category__groups__in=user_groups)) |
69 | 73 return qs |
70 return qs.select_related('category', 'last_post', 'last_post__user') | |
71 | 74 |
72 | 75 |
73 class Forum(models.Model): | 76 class Forum(models.Model): |
74 """ | 77 """ |
75 A forum is a collection of topics. | 78 A forum is a collection of topics. |