My favorites
▼
|
Sign in
django-forum
Simple Django Forum Component
Project Home
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
39
attachment: forms.patch
(12.3 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
Index: views.py
===================================================================
--- views.py (revision 27)
+++ views.py (working copy)
@@ -5,9 +5,8 @@
from datetime import datetime
from django.shortcuts import get_object_or_404, render_to_response
-from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseServerError, HttpResponseForbidden, HttpResponseNotAllowed
+from django.http import HttpResponseRedirect, HttpResponseServerError, HttpResponseForbidden, HttpResponseNotAllowed
from django.template import RequestContext, Context, loader
-from django import forms
from django.core.mail import EmailMessage
from django.conf import settings
from django.template.defaultfilters import striptags, wordwrap
@@ -16,27 +15,50 @@
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list
-from forum.models import Forum,Thread,Post,Subscription
+from forum.models import Forum, Thread, Post, Subscription
from forum.forms import CreateThreadForm, ReplyForm
+from forum.utils import send_subscription_mail
+FORUM_PAGINATE_POST_BY = getattr(settings, 'FORUM_PAGINATE_POST_BY', 10)
+FORUM_PAGINATE_THREAD_BY = getattr(settings, 'FORUM_PAGINATE_THREAD_BY', 10)
+
def forum(request, slug):
"""
Displays a list of threads within a forum.
Threads are sorted by their sticky flag, followed by their
most recent post.
+
+ Only allows a user to post if they're logged in.
"""
- f = get_object_or_404(Forum, slug=slug)
+ forum = get_object_or_404(Forum, slug=slug)
- form = CreateThreadForm()
-
+ if request.method == 'POST' and request.user.is_authenticated():
+ form = CreateThreadForm(request.POST)
+ if form.is_valid():
+ thread = Thread.objects.start_thread(
+ forum=forum,
+ user=request.user,
+ title=form.cleaned_data['title'],
+ body=form.cleaned_data['body']
+ )
+ if form.cleaned_data.get('subscribe',False):
+ s = Subscription(
+ author=request.user,
+ thread=thread
+ )
+ s.save()
+ return HttpResponseRedirect(thread.get_absolute_url())
+ else:
+ form = CreateThreadForm()
+
return object_list( request,
- queryset=f.thread_set.all(),
- paginate_by=10,
+ queryset=forum.thread_set.all(),
+ paginate_by=FORUM_PAGINATE_THREAD_BY,
template_object_name='thread',
template_name='forum/thread_list.html',
extra_context = {
- 'forum': f,
- 'form': form,
+ 'forum': forum,
+ 'form':form,
})
def thread(request, thread):
@@ -46,42 +68,29 @@
"""
t = get_object_or_404(Thread, pk=thread)
p = t.post_set.all().order_by('time')
- s = t.subscription_set.filter(author=request.user)
+ if request.user.is_authenticated():
+ s = t.subscription_set.filter(author=request.user)
+ else:
+ s = None
t.views += 1
t.save()
-
+
+ # Managin replies
if s:
initial = {'subscribe': True}
else:
initial = {'subscribe': False}
-
- form = ReplyForm(initial=initial)
- return object_list( request,
- queryset=p,
- paginate_by=10,
- template_object_name='post',
- template_name='forum/thread.html',
- extra_context = {
- 'forum': t.forum,
- 'thread': t,
- 'subscription': s,
- 'form': form,
- })
-
-def reply(request, thread):
- """
- If a thread isn't closed, and the user is logged in, post a reply
- to a thread. Note we don't have "nested" replies at this stage.
- """
- if not request.user.is_authenticated():
- return HttpResponseServerError()
- t = get_object_or_404(Thread, pk=thread)
- if t.closed:
- return HttpResponseServerError()
-
if request.method == "POST":
+ if not request.user.is_authenticated():
+ request.user.message_set.create(message=u'You must be authenticated to reply to a thread')
+ return HttpResponseRedirect(t.get_absolute_url())
+
+ if t.closed:
+ request.user.message_set.create(message=u'You cannot reply to a closed thread')
+ return HttpResponseRedirect(t.get_absolute_url())
+
form = ReplyForm(request.POST)
if form.is_valid():
body = form.cleaned_data['body']
@@ -107,74 +116,23 @@
# Subscriptions are updated now send mail to all the authors subscribed in
# this thread.
- mail_subject = ''
- try:
- mail_subject = settings.FORUM_MAIL_PREFIX
- except AttributeError:
- mail_subject = '[Forum]'
+ send_subscription_mail(body, t)
- mail_from = ''
- try:
- mail_from = settings.FORUM_MAIL_FROM
- except AttributeError:
- mail_from = settings.DEFAULT_FROM_EMAIL
-
- mail_tpl = loader.get_template('forum/notify.txt')
- c = Context({
- 'body': wordwrap(striptags(body), 72),
- 'site' : Site.objects.get_current(),
- 'thread': t,
- })
-
- email = EmailMessage(
- subject=mail_subject+' '+striptags(t.title),
- body= mail_tpl.render(c),
- from_email=mail_from,
- to=[mail_from],
- bcc=[s.author.email for s in t.subscription_set.all()],)
- email.send(fail_silently=True)
-
return HttpResponseRedirect(p.get_absolute_url())
else:
- return HttpResponseNotAllowed(['POST'])
-
-def newthread(request, forum):
- """
- Rudimentary post function - this should probably use
- newforms, although not sure how that goes when we're updating
- two models.
-
- Only allows a user to post if they're logged in.
- """
- if not request.user.is_authenticated():
- return HttpResponseServerError()
-
- f = get_object_or_404(Forum, slug=forum)
-
- if request.method == 'POST':
- form = CreateThreadForm(request.POST)
- if form.is_valid():
- t = Thread(
- forum=f,
- title=form.cleaned_data['title'],
- )
- t.save()
-
- p = Post(
- thread=t,
- author=request.user,
- body=form.cleaned_data['body'],
- time=datetime.now(),
- )
- p.save()
+ form = ReplyForm(initial=initial)
- if form.cleaned_data.get('subscribe', False):
- s = Subscription(
- author=request.user,
- thread=t
- )
- s.save()
- return HttpResponseRedirect(t.get_absolute_url())
+ return object_list( request,
+ queryset=p,
+ paginate_by=FORUM_PAGINATE_POST_BY,
+ template_object_name='post',
+ template_name='forum/thread.html',
+ extra_context = {
+ 'forum': t.forum,
+ 'thread': t,
+ 'subscription': s,
+ 'form': form,
+ })
def updatesubs(request):
"""
Index: managers.py
===================================================================
--- managers.py (revision 0)
+++ managers.py (revision 0)
@@ -0,0 +1,22 @@
+from django.db import models
+
+import datetime
+
+class ThreadManager(models.Manager):
+ def start_thread(self, forum, user, title, body):
+ """
+ Create a new thread with a first post
+ """
+ thread = self.create(
+ forum=forum,
+ title=title,
+ )
+ from forum.models import Post
+ post = Post(
+ thread=thread,
+ author=user,
+ body=body,
+ time=datetime.datetime.now(),
+ )
+ post.save()
+ return thread
\ No newline at end of file
Index: utils.py
===================================================================
--- utils.py (revision 0)
+++ utils.py (revision 0)
@@ -0,0 +1,33 @@
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.template import RequestContext, Context, loader
+from django.template.defaultfilters import striptags, wordwrap
+from django.core.mail import EmailMessage
+
+def send_subscription_mail(body, thread):
+
+ mail_subject = ''
+ try:
+ mail_subject = settings.FORUM_MAIL_PREFIX
+ except AttributeError:
+ mail_subject = '[Forum]'
+
+ mail_from = ''
+ try:
+ mail_from = settings.FORUM_MAIL_FROM
+ except AttributeError:
+ mail_from = settings.DEFAULT_FROM_EMAIL
+
+ mail_tpl = loader.get_template('forum/notify.txt')
+ c = Context({
+ 'body': wordwrap(striptags(body), 72),
+ 'site' : Site.objects.get_current(),
+ 'thread': thread,
+ })
+ email = EmailMessage(
+ subject=mail_subject+' '+striptags(thread.title),
+ body= mail_tpl.render(c),
+ from_email=mail_from,
+ to=[mail_from],
+ bcc=[s.author.email for s in thread.subscription_set.all()],)
+ email.send(fail_silently=False)
\ No newline at end of file
Index: models.py
===================================================================
--- models.py (revision 27)
+++ models.py (working copy)
@@ -11,6 +11,8 @@
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
+from forum.managers import ThreadManager
+
class Forum(models.Model):
"""
Very basic outline for a Forum, or group of threads. The threads
@@ -153,6 +155,8 @@
views = models.IntegerField(_("Views"), default=0)
latest_post_time = models.DateTimeField(_("Latest Post Time"), blank=True, null=True)
+ objects = ThreadManager()
+
def _get_thread_latest_post(self):
"""This gets the latest post for the thread"""
if not hasattr(self, '__thread_latest_post'):
Index: urls.py
===================================================================
--- urls.py (revision 27)
+++ urls.py (working copy)
@@ -28,13 +28,10 @@
url(r'^(?P<url>(rss|atom).*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict}),
url(r'^thread/(?P<thread>[0-9]+)/$', 'forum.views.thread', name='forum_view_thread'),
- url(r'^thread/(?P<thread>[0-9]+)/reply/$', 'forum.views.reply', name='forum_reply_thread'),
+
url(r'^subscriptions/$', 'forum.views.updatesubs', name='forum_subscriptions'),
url(r'^(?P<slug>[-\w]+)/$', 'forum.views.forum', name='forum_thread_list'),
- url(r'^(?P<forum>[-\w]+)/new/$', 'forum.views.newthread', name='forum_new_thread'),
-
- url(r'^([-\w/]+/)(?P<forum>[-\w]+)/new/$', 'forum.views.newthread'),
url(r'^([-\w/]+/)(?P<slug>[-\w]+)/$', 'forum.views.forum', name='forum_subforum_thread_list'),
)
Index: templates/forum/thread_list.html
===================================================================
--- templates/forum/thread_list.html (revision 27)
+++ templates/forum/thread_list.html (working copy)
@@ -68,7 +68,7 @@
<h2>Create a Thread</h2>
{% if user.is_authenticated %}
-<form method='post' action='new/'>
+<form method="post" action="">
<p><label>Posting As</label><span>{{ user.username }}</span></p>
{{ form.as_p }}
<p><input type='submit' value='Post' /></p>
Index: templates/forum/thread.html
===================================================================
--- templates/forum/thread.html (revision 27)
+++ templates/forum/thread.html (working copy)
@@ -36,7 +36,7 @@
<p>Sorry, this thread is closed. No further replies are permitted.</p>
{% else %}
{% if user.is_authenticated %}
-<form method='post' action='reply/'>
+<form method="post" action="">
<p><label>Posting As</label><span>{{ user.username }}</span></p>
{{ form.as_p }}
<input type='submit' value='Submit' />
Powered by
Google Project Hosting