My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 47 attachment: groups.patch (6.5 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Index: admin.py
===================================================================
--- admin.py (revision 37)
+++ admin.py (working copy)
@@ -3,6 +3,7 @@

class ForumAdmin(admin.ModelAdmin):
list_display = ('title', '_parents_repr')
+ list_filter = ('groups',)
ordering = ['parent', 'title']
prepopulated_fields = {"slug": ("title",)}

Index: views.py
===================================================================
--- views.py (revision 37)
+++ views.py (working copy)
@@ -19,6 +19,11 @@
from forum.models import Forum,Thread,Post,Subscription
from forum.forms import CreateThreadForm, ReplyForm

+def forums_list(request):
+ queryset = Forum.objects.for_groups(request.user.groups.all()).filter(parent__isnull=True)
+ return object_list( request,
+ queryset=queryset)
+
def forum(request, slug):
"""
Displays a list of threads within a forum.
@@ -26,12 +31,12 @@
most recent post.
"""
try:
- f = Forum.objects.select_related().get(slug=slug)
+ f = Forum.objects.for_groups(request.user.groups.all()).select_related().get(slug=slug)
except Forum.DoesNotExist:
- return Http404
+ raise Http404

form = CreateThreadForm()
-
+ child_forums = f.child.for_groups(request.user.groups.all())
return object_list( request,
queryset=f.thread_set.select_related().all(),
paginate_by=10,
@@ -39,6 +44,7 @@
template_name='forum/thread_list.html',
extra_context = {
'forum': f,
+ 'child_forums': child_forums,
'form': form,
})

@@ -49,8 +55,10 @@
"""
try:
t = Thread.objects.select_related().get(pk=thread)
+ if not Forum.objects.has_access(t.forum, request.user.groups.all()):
+ raise Http404
except Thread.DoesNotExist:
- return Http404
+ raise Http404

p = t.post_set.select_related('author').all().order_by('time')
s = None
@@ -89,6 +97,8 @@
t = get_object_or_404(Thread, pk=thread)
if t.closed:
return HttpResponseServerError()
+ if not Forum.objects.has_access(t.forum, request.user.groups.all()):
+ raise HttpResponseServerError()

if request.method == "POST":
form = ReplyForm(request.POST)
@@ -167,6 +177,9 @@
return HttpResponseServerError()

f = get_object_or_404(Forum, slug=forum)
+
+ if not Forum.objects.has_access(f, request.user.groups.all()):
+ raise HttpResponseServerError()

if request.method == 'POST':
form = CreateThreadForm(request.POST)
Index: managers.py
===================================================================
--- managers.py (revision 0)
+++ managers.py (revision 0)
@@ -0,0 +1,11 @@
+from django.db import models
+from django.db.models import Q
+
+class ForumManager(models.Manager):
+ def for_groups(self, groups):
+ public = Q(groups__isnull=True)
+ user_groups = Q(groups__in=groups)
+ return self.filter(public|user_groups)
+
+ def has_access(self, forum, groups):
+ return forum in self.for_groups(groups)
\ No newline at end of file
Index: models.py
===================================================================
--- models.py (revision 37)
+++ models.py (working copy)
@@ -7,10 +7,12 @@

from django.db import models
import datetime
-from django.contrib.auth.models import User
+from django.contrib.auth.models import User, Group
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

+from forum.managers import ForumManager
+
class Forum(models.Model):
"""
Very basic outline for a Forum, or group of threads. The threads
@@ -20,6 +22,7 @@
All of the parent/child recursion code here is borrowed directly from
the Satchmo project: http://www.satchmoproject.com/
"""
+ groups = models.ManyToManyField(Group, blank=True)
title = models.CharField(_("Title"), max_length=100)
slug = models.SlugField(_("Slug"))
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
@@ -27,6 +30,8 @@
threads = models.IntegerField(_("Threads"), default=0)
posts = models.IntegerField(_("Posts"), default=0)

+ objects = ForumManager()
+
def _get_forum_latest_post(self):
"""This gets the latest post for the forum"""
if not hasattr(self, '__forum_latest_post'):
Index: urls.py
===================================================================
--- urls.py (revision 37)
+++ urls.py (working copy)
@@ -13,17 +13,13 @@
from forum.models import Forum
from forum.feeds import RssForumFeed, AtomForumFeed

-forum_dict = {
- 'queryset' : Forum.objects.filter(parent__isnull=True),
-}
-
feed_dict = {
'rss' : RssForumFeed,
'atom': AtomForumFeed
}

urlpatterns = patterns('',
- url(r'^$', 'django.views.generic.list_detail.object_list', forum_dict, name='forum_index'),
+ url(r'^$', 'forum.views.forums_list', name='forum_index'),

url(r'^(?P<url>(rss|atom).*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict}),

Index: templates/forum/thread_list.html
===================================================================
--- templates/forum/thread_list.html (revision 37)
+++ templates/forum/thread_list.html (working copy)
@@ -15,14 +15,14 @@

{% block content %}

-{% if forum.child.all %}
+{% if child_forums %}
<table id='djangoForumList'>
<tr>
<th>Sub-Forum</th>
<th>Last Post</th>
</tr>

-{% for subforum in forum.child.all %}
+{% for subforum in child_forums %}
<tr>
<td class='djangoForumListDetails'><p><strong><a href='{{ subforum.get_absolute_url }}'>{{ subforum.title }}</a></strong><br /><span class='djangoForumStats'>{{ subforum.threads }} thread{{ subforum.threads|pluralize }}, {{ subforum.posts }} post{{ subforum.posts|pluralize }}</span></p>
<p>{{ subforum.description }}</p></td>
Index: templates/forum/thread.html
===================================================================
--- templates/forum/thread.html (revision 37)
+++ templates/forum/thread.html (working copy)
@@ -1,4 +1,5 @@
-{% extends "forum_base.html" %}{% load i18n %}
+{% extends "forum_base.html" %}
+{% load i18n markup %}
{% block title %}{{ thread.title|escape }} ({{ forum.title }}){% endblock %}

{% block pagetitle %}{{ forum.title }} &raquo; {{ thread.title|escape }}{% endblock %}
@@ -9,7 +10,6 @@
{% block content %}

<table id='djangoForumThreadPosts'>
-{% load markup %}
{% for post in post_list %}

<tr>
Powered by Google Project Hosting