bgneal@81
|
1 """
|
bgneal@81
|
2 Views for the forums application.
|
bgneal@81
|
3 """
|
bgneal@83
|
4 from django.contrib.auth.decorators import login_required
|
bgneal@82
|
5 from django.http import Http404
|
bgneal@98
|
6 from django.http import HttpResponse
|
bgneal@89
|
7 from django.http import HttpResponseBadRequest
|
bgneal@90
|
8 from django.http import HttpResponseForbidden
|
bgneal@83
|
9 from django.http import HttpResponseRedirect
|
bgneal@83
|
10 from django.core.urlresolvers import reverse
|
bgneal@91
|
11 from django.core.paginator import InvalidPage
|
bgneal@82
|
12 from django.shortcuts import get_object_or_404
|
bgneal@81
|
13 from django.shortcuts import render_to_response
|
bgneal@97
|
14 from django.template.loader import render_to_string
|
bgneal@81
|
15 from django.template import RequestContext
|
bgneal@89
|
16 from django.views.decorators.http import require_POST
|
bgneal@81
|
17
|
bgneal@90
|
18 from core.paginator import DiggPaginator
|
bgneal@98
|
19 from core.functions import email_admins
|
bgneal@81
|
20 from forums.models import Forum
|
bgneal@83
|
21 from forums.models import Topic
|
bgneal@91
|
22 from forums.models import Post
|
bgneal@98
|
23 from forums.models import FlaggedPost
|
bgneal@106
|
24 from forums.forms import NewTopicForm, NewPostForm, PostForm
|
bgneal@81
|
25
|
bgneal@90
|
26 #######################################################################
|
bgneal@90
|
27
|
bgneal@93
|
28 TOPICS_PER_PAGE = 50
|
bgneal@90
|
29 POSTS_PER_PAGE = 2
|
bgneal@90
|
30
|
bgneal@93
|
31 def create_topic_paginator(topics):
|
bgneal@93
|
32 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@93
|
33
|
bgneal@93
|
34 def create_post_paginator(posts):
|
bgneal@93
|
35 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
36
|
bgneal@90
|
37 #######################################################################
|
bgneal@81
|
38
|
bgneal@81
|
39 def index(request):
|
bgneal@82
|
40 """
|
bgneal@82
|
41 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
42 """
|
bgneal@100
|
43 forums = Forum.objects.forums_for_user(request.user)
|
bgneal@81
|
44 cats = {}
|
bgneal@81
|
45 for forum in forums:
|
bgneal@81
|
46 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
47 'cat': forum.category,
|
bgneal@81
|
48 'forums': [],
|
bgneal@81
|
49 })
|
bgneal@81
|
50 cat['forums'].append(forum)
|
bgneal@81
|
51
|
bgneal@81
|
52 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
53 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
54
|
bgneal@81
|
55 return render_to_response('forums/index.html', {
|
bgneal@81
|
56 'cats': cats,
|
bgneal@81
|
57 },
|
bgneal@81
|
58 context_instance=RequestContext(request))
|
bgneal@81
|
59
|
bgneal@82
|
60
|
bgneal@81
|
61 def forum_index(request, slug):
|
bgneal@82
|
62 """
|
bgneal@82
|
63 Displays all the topics in a forum.
|
bgneal@82
|
64 """
|
bgneal@101
|
65 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
66
|
bgneal@100
|
67 if not forum.category.can_access(request.user):
|
bgneal@100
|
68 return HttpResponseForbidden()
|
bgneal@100
|
69
|
bgneal@100
|
70 topics = forum.topics.select_related('last_post', 'last_post__user')
|
bgneal@93
|
71 paginator = create_topic_paginator(topics)
|
bgneal@93
|
72 page_num = int(request.GET.get('page', 1))
|
bgneal@93
|
73 try:
|
bgneal@93
|
74 page = paginator.page(page_num)
|
bgneal@93
|
75 except InvalidPage:
|
bgneal@93
|
76 raise Http404
|
bgneal@97
|
77
|
bgneal@97
|
78 # we do this for the template since it is rendered twice
|
bgneal@97
|
79 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@82
|
80
|
bgneal@82
|
81 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
82 'forum': forum,
|
bgneal@93
|
83 'page': page,
|
bgneal@97
|
84 'page_nav': page_nav,
|
bgneal@82
|
85 },
|
bgneal@82
|
86 context_instance=RequestContext(request))
|
bgneal@82
|
87
|
bgneal@82
|
88
|
bgneal@82
|
89 def topic_index(request, id):
|
bgneal@82
|
90 """
|
bgneal@82
|
91 Displays all the posts in a topic.
|
bgneal@82
|
92 """
|
bgneal@101
|
93 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@100
|
94
|
bgneal@100
|
95 if not topic.forum.category.can_access(request.user):
|
bgneal@100
|
96 return HttpResponseForbidden()
|
bgneal@100
|
97
|
bgneal@86
|
98 topic.view_count += 1
|
bgneal@86
|
99 topic.save()
|
bgneal@86
|
100
|
bgneal@86
|
101 posts = topic.posts.select_related()
|
bgneal@93
|
102 paginator = create_post_paginator(posts)
|
bgneal@93
|
103 page_num = int(request.GET.get('page', 1))
|
bgneal@90
|
104 try:
|
bgneal@90
|
105 page = paginator.page(page_num)
|
bgneal@90
|
106 except InvalidPage:
|
bgneal@90
|
107 raise Http404
|
bgneal@90
|
108
|
bgneal@90
|
109 last_page = page_num == paginator.num_pages
|
bgneal@86
|
110
|
bgneal@97
|
111 # we do this for the template since it is rendered twice
|
bgneal@97
|
112 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@97
|
113
|
bgneal@104
|
114 can_moderate = request.user.is_authenticated() and (
|
bgneal@104
|
115 request.user.is_superuser or \
|
bgneal@104
|
116 request.user in topic.forum.moderators.all())
|
bgneal@104
|
117
|
bgneal@104
|
118 can_reply = request.user.is_authenticated() and (
|
bgneal@104
|
119 not topic.locked or can_moderate)
|
bgneal@104
|
120
|
bgneal@86
|
121 return render_to_response('forums/topic.html', {
|
bgneal@86
|
122 'forum': topic.forum,
|
bgneal@86
|
123 'topic': topic,
|
bgneal@90
|
124 'page': page,
|
bgneal@97
|
125 'page_nav': page_nav,
|
bgneal@87
|
126 'last_page': last_page,
|
bgneal@104
|
127 'can_moderate': can_moderate,
|
bgneal@104
|
128 'can_reply': can_reply,
|
bgneal@106
|
129 'form': NewPostForm(initial={'topic_id': topic.id}),
|
bgneal@86
|
130 },
|
bgneal@86
|
131 context_instance=RequestContext(request))
|
bgneal@83
|
132
|
bgneal@83
|
133
|
bgneal@83
|
134 @login_required
|
bgneal@83
|
135 def new_topic(request, slug):
|
bgneal@83
|
136 """
|
bgneal@83
|
137 This view handles the creation of new topics.
|
bgneal@83
|
138 """
|
bgneal@101
|
139 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
140
|
bgneal@100
|
141 if not forum.category.can_access(request.user):
|
bgneal@100
|
142 return HttpResponseForbidden()
|
bgneal@100
|
143
|
bgneal@83
|
144 if request.method == 'POST':
|
bgneal@102
|
145 form = NewTopicForm(request.user, forum, request.POST)
|
bgneal@83
|
146 if form.is_valid():
|
bgneal@102
|
147 topic = form.save(request.META.get("REMOTE_ADDR"))
|
bgneal@83
|
148 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
149 kwargs={'tid': topic.pk}))
|
bgneal@83
|
150 else:
|
bgneal@102
|
151 form = NewTopicForm(request.user, forum)
|
bgneal@83
|
152
|
bgneal@83
|
153 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
154 'forum': forum,
|
bgneal@83
|
155 'form': form,
|
bgneal@83
|
156 },
|
bgneal@83
|
157 context_instance=RequestContext(request))
|
bgneal@83
|
158
|
bgneal@83
|
159
|
bgneal@83
|
160 @login_required
|
bgneal@83
|
161 def new_topic_thanks(request, tid):
|
bgneal@83
|
162 """
|
bgneal@83
|
163 This view displays the success page for a newly created topic.
|
bgneal@83
|
164 """
|
bgneal@101
|
165 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
|
bgneal@83
|
166 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
167 'forum': topic.forum,
|
bgneal@83
|
168 'topic': topic,
|
bgneal@83
|
169 },
|
bgneal@83
|
170 context_instance=RequestContext(request))
|
bgneal@89
|
171
|
bgneal@89
|
172
|
bgneal@89
|
173 @require_POST
|
bgneal@89
|
174 def quick_reply_ajax(request):
|
bgneal@89
|
175 """
|
bgneal@89
|
176 This function handles the quick reply to a thread function. This
|
bgneal@89
|
177 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
178 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
179 to the document.
|
bgneal@89
|
180 """
|
bgneal@90
|
181 if not request.user.is_authenticated():
|
bgneal@90
|
182 return HttpResponseForbidden()
|
bgneal@90
|
183
|
bgneal@106
|
184 form = NewPostForm(request.POST)
|
bgneal@89
|
185 if form.is_valid():
|
bgneal@102
|
186 if form.topic.locked or not form.topic.forum.category.can_access(request.user):
|
bgneal@100
|
187 return HttpResponseForbidden()
|
bgneal@100
|
188
|
bgneal@89
|
189 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
|
bgneal@89
|
190 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
191 'post': post,
|
bgneal@89
|
192 },
|
bgneal@89
|
193 context_instance=RequestContext(request))
|
bgneal@89
|
194
|
bgneal@89
|
195 return HttpResponseBadRequest();
|
bgneal@89
|
196
|
bgneal@91
|
197
|
bgneal@91
|
198 def goto_post(request, post_id):
|
bgneal@91
|
199 """
|
bgneal@91
|
200 This function calculates what page a given post is on, then redirects
|
bgneal@91
|
201 to that URL. This function is the target of get_absolute_url() for
|
bgneal@91
|
202 Post objects.
|
bgneal@91
|
203 """
|
bgneal@101
|
204 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@91
|
205 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@91
|
206 page = count / POSTS_PER_PAGE + 1
|
bgneal@91
|
207 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
|
bgneal@91
|
208 '?page=%s#p%s' % (page, post.id)
|
bgneal@91
|
209 return HttpResponseRedirect(url)
|
bgneal@91
|
210
|
bgneal@98
|
211
|
bgneal@98
|
212 @require_POST
|
bgneal@98
|
213 def flag_post(request):
|
bgneal@98
|
214 """
|
bgneal@98
|
215 This function handles the flagging of posts by users. This function should
|
bgneal@98
|
216 be the target of an AJAX post.
|
bgneal@98
|
217 """
|
bgneal@98
|
218 if not request.user.is_authenticated():
|
bgneal@99
|
219 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@98
|
220
|
bgneal@98
|
221 id = request.POST.get('id')
|
bgneal@98
|
222 if id is None:
|
bgneal@98
|
223 return HttpResponseBadRequest('No post id')
|
bgneal@98
|
224
|
bgneal@98
|
225 try:
|
bgneal@98
|
226 post = Post.objects.get(pk=id)
|
bgneal@98
|
227 except Post.DoesNotExist:
|
bgneal@98
|
228 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@98
|
229
|
bgneal@98
|
230 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@98
|
231 flag.save()
|
bgneal@98
|
232 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@98
|
233
|
bgneal@98
|
234 A user has flagged a forum post for review.
|
bgneal@98
|
235 """)
|
bgneal@98
|
236 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@98
|
237 'Thanks for helping to improve the discussions on this site.')
|
bgneal@106
|
238
|
bgneal@106
|
239
|
bgneal@106
|
240 @login_required
|
bgneal@106
|
241 def edit_post(request, id):
|
bgneal@106
|
242 """
|
bgneal@106
|
243 This view function allows authorized users to edit posts.
|
bgneal@106
|
244 The superuser, forum moderators, and original author can edit posts.
|
bgneal@106
|
245 """
|
bgneal@106
|
246 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@106
|
247 can_edit = request.user == post.user or \
|
bgneal@106
|
248 request.user.is_superuser or \
|
bgneal@106
|
249 request.user in post.topic.forum.moderators.all()
|
bgneal@106
|
250
|
bgneal@106
|
251 if not can_edit:
|
bgneal@106
|
252 return HttpResponseForbidden("You don't have permission to edit that post.")
|
bgneal@106
|
253
|
bgneal@106
|
254 if request.method == "POST":
|
bgneal@106
|
255 form = PostForm(request.POST, instance=post)
|
bgneal@106
|
256 if form.is_valid():
|
bgneal@106
|
257 form.save()
|
bgneal@106
|
258 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@106
|
259 else:
|
bgneal@106
|
260 form = PostForm(instance=post)
|
bgneal@106
|
261
|
bgneal@106
|
262 return render_to_response('forums/edit_post.html', {
|
bgneal@106
|
263 'forum': post.topic.forum,
|
bgneal@106
|
264 'topic': post.topic,
|
bgneal@106
|
265 'post': post,
|
bgneal@106
|
266 'form': form,
|
bgneal@106
|
267 'can_moderate': True,
|
bgneal@106
|
268 },
|
bgneal@106
|
269 context_instance=RequestContext(request))
|