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 80 attachment: fix01.patch (7.1 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
diff --git a/forms.py b/forms.py
index a068cf0..044a155 100644
--- a/forms.py
+++ b/forms.py
@@ -1,11 +1,24 @@
from django import forms
+from models import Thread, Post, Subscription
from django.utils.translation import ugettext as _

-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}))
+class PartialThreadForm(forms.ModelForm):
+ class Meta:
+ model = Thread
+ fields = ('title',)
+
+class PartialPostForm(forms.ModelForm):
+ class Meta:
+ model = Post
+ fields = ('body',)
+
+class PartialSubscriptionForm(forms.ModelForm):
subscribe = forms.BooleanField(label=_("Subscribe via email"), required=False)

+ class Meta:
+ model = Subscription
+ fields = ('subscribe',)
+
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)
diff --git a/templates/forum/newthread.html b/templates/forum/newthread.html
index f6b3361..1478f65 100644
--- a/templates/forum/newthread.html
+++ b/templates/forum/newthread.html
@@ -4,8 +4,8 @@
{% block title %}{% blocktrans with forum.title as title %}New Thread in {{ title }}{% endblocktrans %}{% endblock %}

{% block extrahead %}
-<link rel="alternate" type="application/rss+xml" title="{% blocktrans %}{{ forum.title }} Posts via RSS{% endblocktrans %}" href="{% url forum_index %}rss/{{ forum.slug }}/" />
-<link rel="alternate" type="application/atom+xml" title="{% blocktrans %}{{ forum.title }} Posts via ATOM{% endblocktrans %}" href="{% url forum_index %}atom/{{ forum.slug }}/" />
+<link rel="alternate" type="application/rss+xml" title="{% blocktrans with forum.title as ftitle %}{{ ftitle }} Posts via RSS{% endblocktrans %}" href="{% url forum_index %}rss/{{ forum.slug }}/" />
+<link rel="alternate" type="application/atom+xml" title="{% blocktrans with forum.title as ftitle %}{{ ftitle }} Posts via ATOM{% endblocktrans %}" href="{% url forum_index %}atom/{{ forum.slug }}/" />
{% endblock %}

{% block pagetitle %}{% blocktrans with forum.title as title %}New Thread in {{ title }}{% endblocktrans %}{% endblock %}
@@ -17,9 +17,13 @@

<h2>{% trans "Create a Thread" %}</h2>
<form method='post' action='./'>
-{% if form.errors %}<ul>{{ form.errors.as_ul }}</ul>{% endif %}
+{% if t_form.errors %}<ul>{{ t_form.errors.as_ul }}</ul>{% endif %}
+{% if p_form.errors %}<ul>{{ p_form.errors.as_ul }}</ul>{% endif %}
+{% if s_form.errors %}<ul>{{ s_form.errors.as_ul }}</ul>{% endif %}
<p><label>{% trans "Posting As" %}</label><span>{{ user.username }}</span></p>
-{{ form.as_p }}
+{{ t_form.as_p }}
+{{ p_form.as_p }}
+{{ s_form.as_p }}
<p><input type='submit' value='{% trans "Post" %}' /></p>
</form>

diff --git a/templates/forum/thread_list.html b/templates/forum/thread_list.html
index e6d79e8..f7a0f5a 100644
--- a/templates/forum/thread_list.html
+++ b/templates/forum/thread_list.html
@@ -70,7 +70,9 @@
{% if user.is_authenticated %}
<form method='post' action='new/'>
<p><label>{% trans "Posting As" %}</label><span>{{ user.username }}</span></p>
-{{ form.as_p }}
+{{ t_form.as_p }}
+{{ p_form.as_p }}
+{{ s_form.as_p }}
<p><input type='submit' value='{% trans "Post" %}' /></p>
</form>
{% else %}
diff --git a/views.py b/views.py
index 9f8c91a..8612fc7 100644
--- a/views.py
+++ b/views.py
@@ -17,7 +17,7 @@ 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.forms import CreateThreadForm, ReplyForm
+from forum.forms import PartialThreadForm, PartialPostForm, PartialSubscriptionForm, ReplyForm

FORUM_PAGINATION = getattr(settings, 'FORUM_PAGINATION', 10)
LOGIN_URL = getattr(settings, 'LOGIN_URL', '/accounts/login/')
@@ -38,7 +38,9 @@ def forum(request, slug):
except Forum.DoesNotExist:
raise Http404

- form = CreateThreadForm()
+ t_form = PartialThreadForm(prefix="t")
+ p_form = PartialPostForm(prefix="p")
+ s_form = PartialSubscriptionForm(prefix="s")
child_forums = f.child.for_groups(request.user.groups.all())
return object_list( request,
queryset=f.thread_set.select_related().all(),
@@ -48,7 +50,9 @@ def forum(request, slug):
extra_context = {
'forum': f,
'child_forums': child_forums,
- 'form': form,
+ 't_form': t_form,
+ 'p_form': p_form,
+ 's_form': s_form,
})

def thread(request, thread):
@@ -167,13 +171,9 @@ def reply(request, thread):
'thread': t,
}))

-
def newthread(request, forum):
"""
- Rudimentary post function - this should probably use
- newforms, although not sure how that goes when we're updating
- two models.
-
+ Post function
Only allows a user to post if they're logged in.
"""
if not request.user.is_authenticated():
@@ -184,36 +184,39 @@ def newthread(request, forum):
if not Forum.objects.has_access(f, request.user.groups.all()):
return HttpResponseForbidden()

- if request.method == 'POST':
- form = CreateThreadForm(request.POST)
- if form.is_valid():
- t = Thread(
- forum=f,
- title=form.cleaned_data['title'],
- )
+ if request.method == "POST":
+ t_form = PartialThreadForm(request.POST, prefix="t")
+ p_form = PartialPostForm(request.POST, prefix="p")
+ s_form = PartialSubscriptionForm(request.POST, prefix="s")
+
+ if t_form.is_valid() and p_form.is_valid() and s_form.is_valid():
+ t = t_form.save(commit=False)
+ t.forum = f
t.save()

- p = Post(
- thread=t,
- author=request.user,
- body=form.cleaned_data['body'],
- time=datetime.now(),
- )
+ p = p_form.save(commit=False)
+ p.thread = t
+ p.author = request.user
+ p.time = datetime.now()
p.save()
-
- if form.cleaned_data.get('subscribe', False):
- s = Subscription(
- author=request.user,
- thread=t
- )
+
+ if s_form.cleaned_data.get('subscribe', False):
+ s = s_form.save(commit=False)
+ s.author = request.user
+ s.thread = t
s.save()
+
return HttpResponseRedirect(t.get_absolute_url())
else:
- form = CreateThreadForm()
+ t_form = PartialThreadForm(prefix="t")
+ p_form = PartialPostForm(prefix="p")
+ s_form = PartialSubscriptionForm(prefix="s")

return render_to_response('forum/newthread.html',
RequestContext(request, {
- 'form': form,
+ 't_form': t_form,
+ 'p_form': p_form,
+ 's_form': s_form,
'forum': f,
}))

Powered by Google Project Hosting