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 39 attachment: forms.patch (11.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
# HG changeset patch
# User jibaku
# Date 1225320128 -3600
# Branch trunk
# Node ID b136b8cfac77a51526bb90921a7d3a428814b669
# Parent f2429e37214b680efcffd0bd7ea7e1fe566b9bd0
Started switch to django.forms for the forms

diff -r f2429e37214b -r b136b8cfac77 forms.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/forms.py Wed Oct 29 23:42:08 2008 +0100
@@ -0,0 +1,6 @@
+from django import forms
+
+class CreateThreadForm(forms.Form):
+ title = forms.CharField(label="Title", max_length=100)
+ body = forms.CharField(label="Body", widget=forms.Textarea(attrs={'rows':8, 'cols':50}))
+ subscribe = forms.BooleanField(label="Subscribe via email")
\ No newline at end of file
diff -r f2429e37214b -r b136b8cfac77 templates/forum/thread_list.html
--- a/templates/forum/thread_list.html Wed Oct 29 22:54:36 2008 +0100
+++ b/templates/forum/thread_list.html Wed Oct 29 23:42:08 2008 +0100
@@ -69,11 +69,8 @@
<h2>{% trans "Create a Thread" %}</h2>
{% if user.is_authenticated %}
<form method='post' action='new/'>
-<p><label for='title'>{% trans "Title" %}</label> <input type='text' name='title' maxlength='100' /></p>
<p><label>{% trans "Posting As" %}</label><span>{{ user.username }}</span></p>
-<p><label for='body'>{% trans "Body" %}</label>
-<textarea name='body' rows='8' cols='50'></textarea></p>
-<p><label for='subscribe'>{% trans "Subscribe via email:" %}</label><input type='checkbox' name='subscribe' /></p>
+{{ form.as_p }}
<p><input type='submit' value='Post' /></p>
</form>
{% else %}
diff -r f2429e37214b -r b136b8cfac77 views.py
--- a/views.py Wed Oct 29 22:54:36 2008 +0100
+++ b/views.py Wed Oct 29 23:42:08 2008 +0100
@@ -6,7 +6,7 @@
from forum.models import Forum,Thread,Post,Subscription
from datetime import datetime
from django.shortcuts import get_object_or_404, render_to_response
-from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseServerError, HttpResponseForbidden
+from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseServerError, HttpResponseForbidden, HttpResponseNotAllowed
from django.template import RequestContext, Context, loader
from django import forms
from django.core.mail import EmailMessage
@@ -16,6 +16,9 @@
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list
+
+from forum.forms import CreateThreadForm
+
def forum(request, slug):
"""
Displays a list of threads within a forum.
@@ -24,6 +27,8 @@
"""
f = get_object_or_404(Forum, slug=slug)

+ form = CreateThreadForm()
+
return object_list( request,
queryset=f.thread_set.all(),
paginate_by=10,
@@ -31,6 +36,7 @@
template_name='forum/thread_list.html',
extra_context = {
'forum': f,
+ 'form':form,
})

def thread(request, thread):
@@ -131,26 +137,31 @@
"""
if not request.user.is_authenticated():
return HttpResponseServerError()
+
f = get_object_or_404(Forum, slug=forum)
- t = Thread(
- forum=f,
- title=request.POST.get('title'),
- )
- t.save()
- p = Post(
- thread=t,
- author=request.user,
- body=request.POST.get('body'),
- time=datetime.now(),
- )
- p.save()
- if request.POST.get('subscribe',False):
- s = Subscription(
- author=request.user,
- thread=t
+
+ if request.method == 'POST':
+ form = CreateThreadForm(request.POST)
+ if form.is_valid():
+ t = Thread(
+ forum=f,
+ title=form.cleaned_data['title']
)
- s.save()
- return HttpResponseRedirect(t.get_absolute_url())
+ t.save()
+ p = Post(
+ thread=t,
+ author=request.user,
+ body=form.cleaned_data['body'],
+ time=datetime.now(),
+ )
+ p.save()
+ if form.cleaned_data.get('subscribe',False):
+ s = Subscription(
+ author=request.user,
+ thread=t
+ )
+ s.save()
+ return HttpResponseRedirect(t.get_absolute_url())

def updatesubs(request):
"""

# HG changeset patch
# User jibaku
# Date 1225321520 -3600
# Branch trunk
# Node ID 3463f5c552d878b092b0a91deebe8f0c1b7f2b63
# Parent b2957141be57801ea9feb862546af44d25a1b469
Reply with django.forms

diff -r b2957141be57 -r 3463f5c552d8 forms.py
--- a/forms.py Wed Oct 29 23:43:51 2008 +0100
+++ b/forms.py Thu Oct 30 00:05:20 2008 +0100
@@ -3,4 +3,8 @@
class CreateThreadForm(forms.Form):
title = forms.CharField(label="Title", max_length=100)
body = forms.CharField(label="Body", widget=forms.Textarea(attrs={'rows':8, 'cols':50}))
- subscribe = forms.BooleanField(label="Subscribe via email")
\ No newline at end of file
+ subscribe = forms.BooleanField(label="Subscribe via email", required=False)
+
+class ReplyForm(forms.Form):
+ body = forms.CharField(label="Body", widget=forms.Textarea(attrs={'rows':8, 'cols':50}))
+ subscribe = forms.BooleanField(label="Subscribe via email", required=False)
\ No newline at end of file
diff -r b2957141be57 -r 3463f5c552d8 templates/forum/thread.html
--- a/templates/forum/thread.html Wed Oct 29 23:43:51 2008 +0100
+++ b/templates/forum/thread.html Thu Oct 30 00:05:20 2008 +0100
@@ -38,9 +38,7 @@
{% if user.is_authenticated %}
<form method='post' action='reply/'>
<p><label>{% trans "Posting As" %}</label><span>{{ user.username }}</span></p>
-<p><label for='id_body'>{% trans "Your Post" %}</label>
-<textarea name='body' rows='8' cols='50' id='id_body'></textarea></p>
-<p><label for='id_subscribe'>{% trans "Subscribe via email:" %}</label><input type='checkbox' id='id_subscribe' name='subscribe' {% if subscription %}checked{% endif %}/></p>
+{{ form.as_p }}
<input type='submit' value='Submit' />
</form>
{% else %}
diff -r b2957141be57 -r 3463f5c552d8 views.py
--- a/views.py Wed Oct 29 23:43:51 2008 +0100
+++ b/views.py Thu Oct 30 00:05:20 2008 +0100
@@ -3,7 +3,6 @@
and posts, adding new threads, and adding replies.
"""

-from forum.models import Forum,Thread,Post,Subscription
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
@@ -17,7 +16,8 @@
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list

-from forum.forms import CreateThreadForm
+from forum.models import Forum, Thread, Post, Subscription
+from forum.forms import CreateThreadForm, ReplyForm

def forum(request, slug):
"""
@@ -51,6 +51,13 @@
t.views += 1
t.save()

+ if s:
+ initial = {'subscribe':True}
+ else:
+ initial = {'subscribe':False}
+
+ form = ReplyForm(initial=initial)
+
return object_list( request,
queryset=p,
paginate_by=10,
@@ -60,6 +67,7 @@
'forum': t.forum,
'thread': t,
'subscription': s,
+ 'form': form,
})

def reply(request, thread):
@@ -72,60 +80,66 @@
t = get_object_or_404(Thread, pk=thread)
if t.closed:
return HttpResponseServerError()
- body = request.POST.get('body', False)
- p = Post(
- thread=t,
- author=request.user,
- body=body,
- time=datetime.now(),
- )
- p.save()

- sub = Subscription.objects.filter(thread=t, author=request.user)
- if request.POST.get('subscribe',False):
- if not sub:
- s = Subscription(
+ if request.method == "POST":
+ form = ReplyForm(request.POST)
+ if form.is_valid():
+ body = form.cleaned_data['body']
+ p = Post(
+ thread=t,
author=request.user,
- thread=t
+ body=body,
+ time=datetime.now(),
)
- s.save()
+ p.save()
+
+ sub = Subscription.objects.filter(thread=t, author=request.user)
+ if form.cleaned_data.get('subscribe',False):
+ if not sub:
+ s = Subscription(
+ author=request.user,
+ thread=t
+ )
+ s.save()
+ else:
+ if sub:
+ sub.delete()
+
+ # 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]'
+
+ 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('Hello', 'Body goes here', 'from@example.com',
+ # ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
+ # headers = {'Reply-To': 'another@example.com'})
+ 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:
- if sub:
- sub.delete()
-
- # 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]'
-
- 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('Hello', 'Body goes here', 'from@example.com',
- # ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
- # headers = {'Reply-To': 'another@example.com'})
- 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())
+ return HttpResponseNotAllowed(['POST'])

def newthread(request, forum):
"""
Powered by Google Project Hosting