bgneal@81
|
1 """
|
bgneal@81
|
2 Views for the forums application.
|
bgneal@81
|
3 """
|
bgneal@113
|
4 import datetime
|
bgneal@113
|
5
|
bgneal@83
|
6 from django.contrib.auth.decorators import login_required
|
bgneal@82
|
7 from django.http import Http404
|
bgneal@98
|
8 from django.http import HttpResponse
|
bgneal@89
|
9 from django.http import HttpResponseBadRequest
|
bgneal@90
|
10 from django.http import HttpResponseForbidden
|
bgneal@83
|
11 from django.http import HttpResponseRedirect
|
bgneal@83
|
12 from django.core.urlresolvers import reverse
|
bgneal@91
|
13 from django.core.paginator import InvalidPage
|
bgneal@82
|
14 from django.shortcuts import get_object_or_404
|
bgneal@81
|
15 from django.shortcuts import render_to_response
|
bgneal@97
|
16 from django.template.loader import render_to_string
|
bgneal@81
|
17 from django.template import RequestContext
|
bgneal@89
|
18 from django.views.decorators.http import require_POST
|
bgneal@108
|
19 from django.utils.text import wrap
|
bgneal@81
|
20
|
bgneal@90
|
21 from core.paginator import DiggPaginator
|
bgneal@98
|
22 from core.functions import email_admins
|
bgneal@113
|
23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
|
bgneal@113
|
24 ForumLastVisit
|
bgneal@115
|
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
|
bgneal@115
|
26 SplitTopicForm
|
bgneal@114
|
27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \
|
bgneal@167
|
28 get_post_unread_status, get_unread_topics
|
bgneal@81
|
29
|
bgneal@117
|
30 from bio.models import UserProfile
|
bgneal@90
|
31 #######################################################################
|
bgneal@90
|
32
|
bgneal@93
|
33 TOPICS_PER_PAGE = 50
|
bgneal@113
|
34 POSTS_PER_PAGE = 20
|
bgneal@90
|
35
|
bgneal@167
|
36
|
bgneal@167
|
37 def get_page_num(request):
|
bgneal@167
|
38 """Returns the value of the 'page' variable in GET if it exists, or 1
|
bgneal@167
|
39 if it does not."""
|
bgneal@167
|
40
|
bgneal@167
|
41 try:
|
bgneal@167
|
42 page_num = int(request.GET.get('page', 1))
|
bgneal@167
|
43 except ValueError:
|
bgneal@167
|
44 page_num = 1
|
bgneal@167
|
45
|
bgneal@167
|
46 return page_num
|
bgneal@167
|
47
|
bgneal@167
|
48
|
bgneal@93
|
49 def create_topic_paginator(topics):
|
bgneal@93
|
50 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@93
|
51
|
bgneal@93
|
52 def create_post_paginator(posts):
|
bgneal@93
|
53 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
54
|
bgneal@167
|
55
|
bgneal@167
|
56 def attach_topic_page_ranges(topics):
|
bgneal@167
|
57 """Attaches a page_range attribute to each topic in the supplied list.
|
bgneal@167
|
58 This attribute will be None if it is a single page topic. This is used
|
bgneal@167
|
59 by the templates to generate "goto page x" links.
|
bgneal@167
|
60 """
|
bgneal@167
|
61 for topic in topics:
|
bgneal@167
|
62 if topic.post_count > POSTS_PER_PAGE:
|
bgneal@167
|
63 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE,
|
bgneal@167
|
64 body=2, tail=3, margin=1)
|
bgneal@167
|
65 topic.page_range = pp.page(1).page_range
|
bgneal@167
|
66 else:
|
bgneal@167
|
67 topic.page_range = None
|
bgneal@167
|
68
|
bgneal@90
|
69 #######################################################################
|
bgneal@81
|
70
|
bgneal@81
|
71 def index(request):
|
bgneal@82
|
72 """
|
bgneal@82
|
73 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
74 """
|
bgneal@167
|
75 # check for special forum queries
|
bgneal@167
|
76 query = request.GET.get("query")
|
bgneal@167
|
77 if query == "unread":
|
bgneal@167
|
78 return HttpResponseRedirect(reverse('forums-unread_topics'))
|
bgneal@168
|
79 elif query == "unanswered":
|
bgneal@168
|
80 return HttpResponseRedirect(reverse('forums-unanswered_topics'))
|
bgneal@167
|
81
|
bgneal@100
|
82 forums = Forum.objects.forums_for_user(request.user)
|
bgneal@113
|
83 get_forum_unread_status(forums, request.user)
|
bgneal@81
|
84 cats = {}
|
bgneal@81
|
85 for forum in forums:
|
bgneal@81
|
86 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
87 'cat': forum.category,
|
bgneal@81
|
88 'forums': [],
|
bgneal@81
|
89 })
|
bgneal@81
|
90 cat['forums'].append(forum)
|
bgneal@81
|
91
|
bgneal@81
|
92 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
93 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
94
|
bgneal@81
|
95 return render_to_response('forums/index.html', {
|
bgneal@81
|
96 'cats': cats,
|
bgneal@81
|
97 },
|
bgneal@81
|
98 context_instance=RequestContext(request))
|
bgneal@81
|
99
|
bgneal@82
|
100
|
bgneal@81
|
101 def forum_index(request, slug):
|
bgneal@82
|
102 """
|
bgneal@82
|
103 Displays all the topics in a forum.
|
bgneal@82
|
104 """
|
bgneal@101
|
105 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
106
|
bgneal@100
|
107 if not forum.category.can_access(request.user):
|
bgneal@100
|
108 return HttpResponseForbidden()
|
bgneal@100
|
109
|
bgneal@107
|
110 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@114
|
111 get_topic_unread_status(forum, topics, request.user)
|
bgneal@114
|
112
|
bgneal@93
|
113 paginator = create_topic_paginator(topics)
|
bgneal@167
|
114 page_num = get_page_num(request)
|
bgneal@93
|
115 try:
|
bgneal@93
|
116 page = paginator.page(page_num)
|
bgneal@93
|
117 except InvalidPage:
|
bgneal@93
|
118 raise Http404
|
bgneal@97
|
119
|
bgneal@167
|
120 attach_topic_page_ranges(page.object_list)
|
bgneal@161
|
121
|
bgneal@97
|
122 # we do this for the template since it is rendered twice
|
bgneal@97
|
123 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
124
|
bgneal@111
|
125 can_moderate = _can_moderate(forum, request.user)
|
bgneal@82
|
126
|
bgneal@82
|
127 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
128 'forum': forum,
|
bgneal@93
|
129 'page': page,
|
bgneal@97
|
130 'page_nav': page_nav,
|
bgneal@111
|
131 'can_moderate': can_moderate,
|
bgneal@82
|
132 },
|
bgneal@82
|
133 context_instance=RequestContext(request))
|
bgneal@82
|
134
|
bgneal@82
|
135
|
bgneal@82
|
136 def topic_index(request, id):
|
bgneal@82
|
137 """
|
bgneal@82
|
138 Displays all the posts in a topic.
|
bgneal@82
|
139 """
|
bgneal@101
|
140 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@100
|
141
|
bgneal@100
|
142 if not topic.forum.category.can_access(request.user):
|
bgneal@100
|
143 return HttpResponseForbidden()
|
bgneal@100
|
144
|
bgneal@86
|
145 topic.view_count += 1
|
bgneal@86
|
146 topic.save()
|
bgneal@86
|
147
|
bgneal@86
|
148 posts = topic.posts.select_related()
|
bgneal@114
|
149
|
bgneal@93
|
150 paginator = create_post_paginator(posts)
|
bgneal@167
|
151 page_num = get_page_num(request)
|
bgneal@90
|
152 try:
|
bgneal@90
|
153 page = paginator.page(page_num)
|
bgneal@90
|
154 except InvalidPage:
|
bgneal@90
|
155 raise Http404
|
bgneal@117
|
156 get_post_unread_status(topic, page.object_list, request.user)
|
bgneal@117
|
157
|
bgneal@117
|
158 # Attach user profiles to each post to avoid using get_user_profile() in
|
bgneal@117
|
159 # the template.
|
bgneal@117
|
160 users = set(post.user.id for post in page.object_list)
|
bgneal@117
|
161
|
bgneal@117
|
162 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
|
bgneal@117
|
163 user_profiles = dict((profile.user.id, profile) for profile in profiles)
|
bgneal@117
|
164
|
bgneal@117
|
165 for post in page.object_list:
|
bgneal@117
|
166 post.user_profile = user_profiles[post.user.id]
|
bgneal@90
|
167
|
bgneal@90
|
168 last_page = page_num == paginator.num_pages
|
bgneal@86
|
169
|
bgneal@113
|
170 if request.user.is_authenticated() and last_page:
|
bgneal@113
|
171 _update_last_visit(request.user, topic)
|
bgneal@113
|
172
|
bgneal@97
|
173 # we do this for the template since it is rendered twice
|
bgneal@97
|
174 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@97
|
175
|
bgneal@109
|
176 can_moderate = _can_moderate(topic.forum, request.user)
|
bgneal@104
|
177
|
bgneal@104
|
178 can_reply = request.user.is_authenticated() and (
|
bgneal@104
|
179 not topic.locked or can_moderate)
|
bgneal@104
|
180
|
bgneal@86
|
181 return render_to_response('forums/topic.html', {
|
bgneal@86
|
182 'forum': topic.forum,
|
bgneal@86
|
183 'topic': topic,
|
bgneal@90
|
184 'page': page,
|
bgneal@97
|
185 'page_nav': page_nav,
|
bgneal@87
|
186 'last_page': last_page,
|
bgneal@104
|
187 'can_moderate': can_moderate,
|
bgneal@104
|
188 'can_reply': can_reply,
|
bgneal@106
|
189 'form': NewPostForm(initial={'topic_id': topic.id}),
|
bgneal@86
|
190 },
|
bgneal@86
|
191 context_instance=RequestContext(request))
|
bgneal@83
|
192
|
bgneal@83
|
193
|
bgneal@83
|
194 @login_required
|
bgneal@83
|
195 def new_topic(request, slug):
|
bgneal@83
|
196 """
|
bgneal@83
|
197 This view handles the creation of new topics.
|
bgneal@83
|
198 """
|
bgneal@101
|
199 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
200
|
bgneal@100
|
201 if not forum.category.can_access(request.user):
|
bgneal@100
|
202 return HttpResponseForbidden()
|
bgneal@100
|
203
|
bgneal@83
|
204 if request.method == 'POST':
|
bgneal@102
|
205 form = NewTopicForm(request.user, forum, request.POST)
|
bgneal@83
|
206 if form.is_valid():
|
bgneal@102
|
207 topic = form.save(request.META.get("REMOTE_ADDR"))
|
bgneal@108
|
208 _bump_post_count(request.user)
|
bgneal@83
|
209 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
210 kwargs={'tid': topic.pk}))
|
bgneal@83
|
211 else:
|
bgneal@102
|
212 form = NewTopicForm(request.user, forum)
|
bgneal@83
|
213
|
bgneal@83
|
214 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
215 'forum': forum,
|
bgneal@83
|
216 'form': form,
|
bgneal@83
|
217 },
|
bgneal@83
|
218 context_instance=RequestContext(request))
|
bgneal@83
|
219
|
bgneal@83
|
220
|
bgneal@83
|
221 @login_required
|
bgneal@83
|
222 def new_topic_thanks(request, tid):
|
bgneal@83
|
223 """
|
bgneal@83
|
224 This view displays the success page for a newly created topic.
|
bgneal@83
|
225 """
|
bgneal@101
|
226 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
|
bgneal@83
|
227 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
228 'forum': topic.forum,
|
bgneal@83
|
229 'topic': topic,
|
bgneal@83
|
230 },
|
bgneal@83
|
231 context_instance=RequestContext(request))
|
bgneal@89
|
232
|
bgneal@89
|
233
|
bgneal@89
|
234 @require_POST
|
bgneal@89
|
235 def quick_reply_ajax(request):
|
bgneal@89
|
236 """
|
bgneal@89
|
237 This function handles the quick reply to a thread function. This
|
bgneal@89
|
238 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
239 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
240 to the document.
|
bgneal@89
|
241 """
|
bgneal@90
|
242 if not request.user.is_authenticated():
|
bgneal@108
|
243 return HttpResponseForbidden('Please login or register to post.')
|
bgneal@90
|
244
|
bgneal@106
|
245 form = NewPostForm(request.POST)
|
bgneal@89
|
246 if form.is_valid():
|
bgneal@108
|
247 if not _can_post_in_topic(form.topic, request.user):
|
bgneal@108
|
248 return HttpResponseForbidden("You don't have permission to post in this topic.")
|
bgneal@100
|
249
|
bgneal@108
|
250 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
|
bgneal@114
|
251 post.unread = True
|
bgneal@122
|
252 post.user_profile = request.user.get_profile()
|
bgneal@108
|
253 _bump_post_count(request.user)
|
bgneal@113
|
254 _update_last_visit(request.user, form.topic)
|
bgneal@89
|
255 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
256 'post': post,
|
bgneal@113
|
257 'can_moderate': _can_moderate(form.topic.forum, request.user),
|
bgneal@120
|
258 'can_reply': True,
|
bgneal@89
|
259 },
|
bgneal@89
|
260 context_instance=RequestContext(request))
|
bgneal@89
|
261
|
bgneal@108
|
262 return HttpResponseBadRequest("Invalid post.");
|
bgneal@89
|
263
|
bgneal@91
|
264
|
bgneal@91
|
265 def goto_post(request, post_id):
|
bgneal@91
|
266 """
|
bgneal@91
|
267 This function calculates what page a given post is on, then redirects
|
bgneal@91
|
268 to that URL. This function is the target of get_absolute_url() for
|
bgneal@91
|
269 Post objects.
|
bgneal@91
|
270 """
|
bgneal@101
|
271 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@91
|
272 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@91
|
273 page = count / POSTS_PER_PAGE + 1
|
bgneal@91
|
274 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
|
bgneal@91
|
275 '?page=%s#p%s' % (page, post.id)
|
bgneal@91
|
276 return HttpResponseRedirect(url)
|
bgneal@91
|
277
|
bgneal@98
|
278
|
bgneal@98
|
279 @require_POST
|
bgneal@98
|
280 def flag_post(request):
|
bgneal@98
|
281 """
|
bgneal@98
|
282 This function handles the flagging of posts by users. This function should
|
bgneal@98
|
283 be the target of an AJAX post.
|
bgneal@98
|
284 """
|
bgneal@98
|
285 if not request.user.is_authenticated():
|
bgneal@99
|
286 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@98
|
287
|
bgneal@98
|
288 id = request.POST.get('id')
|
bgneal@98
|
289 if id is None:
|
bgneal@98
|
290 return HttpResponseBadRequest('No post id')
|
bgneal@98
|
291
|
bgneal@98
|
292 try:
|
bgneal@98
|
293 post = Post.objects.get(pk=id)
|
bgneal@98
|
294 except Post.DoesNotExist:
|
bgneal@98
|
295 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@98
|
296
|
bgneal@98
|
297 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@98
|
298 flag.save()
|
bgneal@98
|
299 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@98
|
300
|
bgneal@98
|
301 A user has flagged a forum post for review.
|
bgneal@98
|
302 """)
|
bgneal@98
|
303 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@98
|
304 'Thanks for helping to improve the discussions on this site.')
|
bgneal@106
|
305
|
bgneal@106
|
306
|
bgneal@106
|
307 @login_required
|
bgneal@106
|
308 def edit_post(request, id):
|
bgneal@106
|
309 """
|
bgneal@106
|
310 This view function allows authorized users to edit posts.
|
bgneal@106
|
311 The superuser, forum moderators, and original author can edit posts.
|
bgneal@106
|
312 """
|
bgneal@106
|
313 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@108
|
314
|
bgneal@109
|
315 can_moderate = _can_moderate(post.topic.forum, request.user)
|
bgneal@108
|
316 can_edit = can_moderate or request.user == post.user
|
bgneal@106
|
317
|
bgneal@106
|
318 if not can_edit:
|
bgneal@106
|
319 return HttpResponseForbidden("You don't have permission to edit that post.")
|
bgneal@106
|
320
|
bgneal@106
|
321 if request.method == "POST":
|
bgneal@106
|
322 form = PostForm(request.POST, instance=post)
|
bgneal@106
|
323 if form.is_valid():
|
bgneal@115
|
324 post = form.save(commit=False)
|
bgneal@115
|
325 post.touch()
|
bgneal@115
|
326 post.save()
|
bgneal@106
|
327 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@106
|
328 else:
|
bgneal@106
|
329 form = PostForm(instance=post)
|
bgneal@106
|
330
|
bgneal@123
|
331 post.user_profile = request.user.get_profile()
|
bgneal@123
|
332
|
bgneal@106
|
333 return render_to_response('forums/edit_post.html', {
|
bgneal@106
|
334 'forum': post.topic.forum,
|
bgneal@106
|
335 'topic': post.topic,
|
bgneal@106
|
336 'post': post,
|
bgneal@106
|
337 'form': form,
|
bgneal@108
|
338 'can_moderate': can_moderate,
|
bgneal@106
|
339 },
|
bgneal@106
|
340 context_instance=RequestContext(request))
|
bgneal@107
|
341
|
bgneal@107
|
342
|
bgneal@107
|
343 @require_POST
|
bgneal@107
|
344 def delete_post(request):
|
bgneal@107
|
345 """
|
bgneal@107
|
346 This view function allows superusers and forum moderators to delete posts.
|
bgneal@107
|
347 This function is the target of AJAX calls from the client.
|
bgneal@107
|
348 """
|
bgneal@107
|
349 if not request.user.is_authenticated():
|
bgneal@107
|
350 return HttpResponseForbidden('Please login to delete a post.')
|
bgneal@107
|
351
|
bgneal@107
|
352 id = request.POST.get('id')
|
bgneal@107
|
353 if id is None:
|
bgneal@107
|
354 return HttpResponseBadRequest('No post id')
|
bgneal@107
|
355
|
bgneal@107
|
356 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@107
|
357
|
bgneal@107
|
358 can_delete = request.user.is_superuser or \
|
bgneal@107
|
359 request.user in post.topic.forum.moderators.all()
|
bgneal@107
|
360
|
bgneal@107
|
361 if not can_delete:
|
bgneal@107
|
362 return HttpResponseForbidden("You don't have permission to delete that post.")
|
bgneal@107
|
363
|
bgneal@147
|
364 delete_single_post(post)
|
bgneal@147
|
365 return HttpResponse("The post has been deleted.")
|
bgneal@147
|
366
|
bgneal@147
|
367
|
bgneal@147
|
368 def delete_single_post(post):
|
bgneal@147
|
369 """
|
bgneal@147
|
370 This function deletes a single post. It handles the case of where
|
bgneal@147
|
371 a post is the sole post in a topic by deleting the topic also. It
|
bgneal@147
|
372 adjusts any foreign keys in Topic or Forum objects that might be pointing
|
bgneal@147
|
373 to this post before deleting the post to avoid a cascading delete.
|
bgneal@147
|
374 """
|
bgneal@107
|
375 if post.topic.post_count == 1 and post == post.topic.last_post:
|
bgneal@107
|
376 _delete_topic(post.topic)
|
bgneal@107
|
377 else:
|
bgneal@107
|
378 _delete_post(post)
|
bgneal@107
|
379
|
bgneal@107
|
380
|
bgneal@107
|
381 def _delete_post(post):
|
bgneal@107
|
382 """
|
bgneal@107
|
383 Internal function to delete a single post object.
|
bgneal@107
|
384 Decrements the post author's post count.
|
bgneal@107
|
385 Adjusts the parent topic and forum's last_post as needed.
|
bgneal@107
|
386 """
|
bgneal@107
|
387 # Adjust post creator's post count
|
bgneal@107
|
388 profile = post.user.get_profile()
|
bgneal@107
|
389 if profile.forum_post_count > 0:
|
bgneal@107
|
390 profile.forum_post_count -= 1
|
bgneal@107
|
391 profile.save()
|
bgneal@107
|
392
|
bgneal@107
|
393 # If this post is the last_post in a topic, we need to update
|
bgneal@107
|
394 # both the topic and parent forum's last post fields. If we don't
|
bgneal@107
|
395 # the cascading delete will delete them also!
|
bgneal@107
|
396
|
bgneal@107
|
397 topic = post.topic
|
bgneal@107
|
398 if topic.last_post == post:
|
bgneal@107
|
399 topic.last_post_pre_delete()
|
bgneal@107
|
400 topic.save()
|
bgneal@107
|
401
|
bgneal@107
|
402 forum = topic.forum
|
bgneal@107
|
403 if forum.last_post == post:
|
bgneal@107
|
404 forum.last_post_pre_delete()
|
bgneal@107
|
405 forum.save()
|
bgneal@107
|
406
|
bgneal@107
|
407 # Should be safe to delete the post now:
|
bgneal@107
|
408 post.delete()
|
bgneal@107
|
409
|
bgneal@107
|
410
|
bgneal@107
|
411 def _delete_topic(topic):
|
bgneal@107
|
412 """
|
bgneal@107
|
413 Internal function to delete an entire topic.
|
bgneal@107
|
414 Deletes the topic and all posts contained within.
|
bgneal@107
|
415 Adjusts the parent forum's last_post as needed.
|
bgneal@107
|
416 Note that we don't bother adjusting all the users'
|
bgneal@107
|
417 post counts as that doesn't seem to be worth the effort.
|
bgneal@107
|
418 """
|
bgneal@147
|
419 if topic.forum.last_post and topic.forum.last_post.topic == topic:
|
bgneal@107
|
420 topic.forum.last_post_pre_delete()
|
bgneal@107
|
421 topic.forum.save()
|
bgneal@107
|
422
|
bgneal@107
|
423 # It should be safe to just delete the topic now. This will
|
bgneal@107
|
424 # automatically delete all posts in the topic.
|
bgneal@107
|
425 topic.delete()
|
bgneal@108
|
426
|
bgneal@108
|
427
|
bgneal@108
|
428 @login_required
|
bgneal@108
|
429 def new_post(request, topic_id):
|
bgneal@108
|
430 """
|
bgneal@108
|
431 This function is the view for creating a normal, non-quick reply
|
bgneal@108
|
432 to a topic.
|
bgneal@108
|
433 """
|
bgneal@108
|
434 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
|
bgneal@108
|
435 can_post = _can_post_in_topic(topic, request.user)
|
bgneal@108
|
436
|
bgneal@108
|
437 if can_post:
|
bgneal@108
|
438 if request.method == 'POST':
|
bgneal@108
|
439 form = PostForm(request.POST)
|
bgneal@108
|
440 if form.is_valid():
|
bgneal@108
|
441 post = form.save(commit=False)
|
bgneal@108
|
442 post.topic = topic
|
bgneal@108
|
443 post.user = request.user
|
bgneal@108
|
444 post.user_ip = request.META.get("REMOTE_ADDR", "")
|
bgneal@108
|
445 post.save()
|
bgneal@108
|
446 _bump_post_count(request.user)
|
bgneal@113
|
447 _update_last_visit(request.user, topic)
|
bgneal@108
|
448 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@108
|
449 else:
|
bgneal@108
|
450 quote_id = request.GET.get('quote')
|
bgneal@108
|
451 if quote_id:
|
bgneal@108
|
452 quote_post = get_object_or_404(Post.objects.select_related(),
|
bgneal@108
|
453 pk=quote_id)
|
bgneal@108
|
454 form = PostForm(initial={'body': _quote_message(quote_post.user.username,
|
bgneal@108
|
455 quote_post.body)})
|
bgneal@108
|
456 else:
|
bgneal@108
|
457 form = PostForm()
|
bgneal@108
|
458 else:
|
bgneal@108
|
459 form = None
|
bgneal@108
|
460
|
bgneal@108
|
461 return render_to_response('forums/new_post.html', {
|
bgneal@108
|
462 'forum': topic.forum,
|
bgneal@108
|
463 'topic': topic,
|
bgneal@108
|
464 'form': form,
|
bgneal@108
|
465 'can_post': can_post,
|
bgneal@108
|
466 },
|
bgneal@108
|
467 context_instance=RequestContext(request))
|
bgneal@108
|
468
|
bgneal@108
|
469
|
bgneal@109
|
470 @login_required
|
bgneal@109
|
471 def mod_topic_stick(request, id):
|
bgneal@109
|
472 """
|
bgneal@109
|
473 This view function is for moderators to toggle the sticky status of a topic.
|
bgneal@109
|
474 """
|
bgneal@109
|
475 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
476 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
477 topic.sticky = not topic.sticky
|
bgneal@109
|
478 topic.save()
|
bgneal@109
|
479 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
480
|
bgneal@110
|
481 return HttpResponseForbidden()
|
bgneal@109
|
482
|
bgneal@109
|
483
|
bgneal@109
|
484 @login_required
|
bgneal@109
|
485 def mod_topic_lock(request, id):
|
bgneal@109
|
486 """
|
bgneal@109
|
487 This view function is for moderators to toggle the locked status of a topic.
|
bgneal@109
|
488 """
|
bgneal@109
|
489 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
490 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
491 topic.locked = not topic.locked
|
bgneal@109
|
492 topic.save()
|
bgneal@109
|
493 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
494
|
bgneal@110
|
495 return HttpResponseForbidden()
|
bgneal@109
|
496
|
bgneal@109
|
497
|
bgneal@109
|
498 @login_required
|
bgneal@109
|
499 def mod_topic_delete(request, id):
|
bgneal@109
|
500 """
|
bgneal@109
|
501 This view function is for moderators to delete an entire topic.
|
bgneal@109
|
502 """
|
bgneal@109
|
503 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
504 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
505 forum_url = topic.forum.get_absolute_url()
|
bgneal@109
|
506 _delete_topic(topic)
|
bgneal@109
|
507 return HttpResponseRedirect(forum_url)
|
bgneal@109
|
508
|
bgneal@110
|
509 return HttpResponseForbidden()
|
bgneal@110
|
510
|
bgneal@110
|
511
|
bgneal@110
|
512 @login_required
|
bgneal@110
|
513 def mod_topic_move(request, id):
|
bgneal@110
|
514 """
|
bgneal@110
|
515 This view function is for moderators to move a topic to a different forum.
|
bgneal@110
|
516 """
|
bgneal@110
|
517 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@110
|
518 if not _can_moderate(topic.forum, request.user):
|
bgneal@110
|
519 return HttpResponseForbidden()
|
bgneal@110
|
520
|
bgneal@110
|
521 if request.method == 'POST':
|
bgneal@110
|
522 form = MoveTopicForm(request.user, request.POST)
|
bgneal@110
|
523 if form.is_valid():
|
bgneal@110
|
524 new_forum = form.cleaned_data['forums']
|
bgneal@110
|
525 old_forum = topic.forum
|
bgneal@111
|
526 _move_topic(topic, old_forum, new_forum)
|
bgneal@110
|
527 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@110
|
528 else:
|
bgneal@110
|
529 form = MoveTopicForm(request.user)
|
bgneal@110
|
530
|
bgneal@110
|
531 return render_to_response('forums/move_topic.html', {
|
bgneal@110
|
532 'forum': topic.forum,
|
bgneal@110
|
533 'topic': topic,
|
bgneal@110
|
534 'form': form,
|
bgneal@110
|
535 },
|
bgneal@110
|
536 context_instance=RequestContext(request))
|
bgneal@109
|
537
|
bgneal@109
|
538
|
bgneal@111
|
539 @login_required
|
bgneal@111
|
540 def mod_forum(request, slug):
|
bgneal@111
|
541 """
|
bgneal@111
|
542 Displays a view to allow moderators to perform various operations
|
bgneal@111
|
543 on topics in a forum in bulk. We currently support mass locking/unlocking,
|
bgneal@111
|
544 stickying and unstickying, moving, and deleting topics.
|
bgneal@111
|
545 """
|
bgneal@111
|
546 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@111
|
547 if not _can_moderate(forum, request.user):
|
bgneal@111
|
548 return HttpResponseForbidden()
|
bgneal@111
|
549
|
bgneal@111
|
550 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@111
|
551 paginator = create_topic_paginator(topics)
|
bgneal@167
|
552 page_num = get_page_num(request)
|
bgneal@111
|
553 try:
|
bgneal@111
|
554 page = paginator.page(page_num)
|
bgneal@111
|
555 except InvalidPage:
|
bgneal@111
|
556 raise Http404
|
bgneal@111
|
557
|
bgneal@111
|
558 # we do this for the template since it is rendered twice
|
bgneal@111
|
559 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
560 form = None
|
bgneal@111
|
561
|
bgneal@111
|
562 if request.method == 'POST':
|
bgneal@111
|
563 topic_ids = request.POST.getlist('topic_ids')
|
bgneal@111
|
564 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
|
bgneal@111
|
565 url += '?page=%s' % page_num
|
bgneal@111
|
566
|
bgneal@111
|
567 if len(topic_ids):
|
bgneal@111
|
568 if request.POST.get('sticky'):
|
bgneal@111
|
569 _bulk_sticky(forum, topic_ids)
|
bgneal@111
|
570 return HttpResponseRedirect(url)
|
bgneal@111
|
571 elif request.POST.get('lock'):
|
bgneal@111
|
572 _bulk_lock(forum, topic_ids)
|
bgneal@111
|
573 return HttpResponseRedirect(url)
|
bgneal@111
|
574 elif request.POST.get('delete'):
|
bgneal@111
|
575 _bulk_delete(forum, topic_ids)
|
bgneal@111
|
576 return HttpResponseRedirect(url)
|
bgneal@111
|
577 elif request.POST.get('move'):
|
bgneal@111
|
578 form = MoveTopicForm(request.user, request.POST, hide_label=True)
|
bgneal@111
|
579 if form.is_valid():
|
bgneal@111
|
580 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
|
bgneal@111
|
581 return HttpResponseRedirect(url)
|
bgneal@111
|
582
|
bgneal@111
|
583 if form is None:
|
bgneal@111
|
584 form = MoveTopicForm(request.user, hide_label=True)
|
bgneal@111
|
585
|
bgneal@111
|
586 return render_to_response('forums/mod_forum.html', {
|
bgneal@111
|
587 'forum': forum,
|
bgneal@111
|
588 'page': page,
|
bgneal@111
|
589 'page_nav': page_nav,
|
bgneal@111
|
590 'form': form,
|
bgneal@111
|
591 },
|
bgneal@111
|
592 context_instance=RequestContext(request))
|
bgneal@111
|
593
|
bgneal@111
|
594
|
bgneal@113
|
595 @login_required
|
bgneal@113
|
596 @require_POST
|
bgneal@113
|
597 def forum_catchup(request, slug):
|
bgneal@113
|
598 """
|
bgneal@113
|
599 This view marks all the topics in the forum as being read.
|
bgneal@113
|
600 """
|
bgneal@113
|
601 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@113
|
602
|
bgneal@113
|
603 if not forum.category.can_access(request.user):
|
bgneal@113
|
604 return HttpResponseForbidden()
|
bgneal@113
|
605
|
bgneal@113
|
606 forum.catchup(request.user)
|
bgneal@113
|
607 return HttpResponseRedirect(forum.get_absolute_url())
|
bgneal@113
|
608
|
bgneal@113
|
609
|
bgneal@115
|
610 @login_required
|
bgneal@115
|
611 def mod_topic_split(request, id):
|
bgneal@115
|
612 """
|
bgneal@115
|
613 This view function allows moderators to split posts off to a new topic.
|
bgneal@115
|
614 """
|
bgneal@115
|
615 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@115
|
616 if not _can_moderate(topic.forum, request.user):
|
bgneal@115
|
617 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
618
|
bgneal@115
|
619 if request.method == "POST":
|
bgneal@115
|
620 form = SplitTopicForm(request.user, request.POST)
|
bgneal@115
|
621 if form.is_valid():
|
bgneal@115
|
622 if form.split_at:
|
bgneal@115
|
623 _split_topic_at(topic, form.post_ids[0],
|
bgneal@115
|
624 form.cleaned_data['forums'],
|
bgneal@115
|
625 form.cleaned_data['name'])
|
bgneal@115
|
626 else:
|
bgneal@115
|
627 _split_topic(topic, form.post_ids,
|
bgneal@115
|
628 form.cleaned_data['forums'],
|
bgneal@115
|
629 form.cleaned_data['name'])
|
bgneal@115
|
630
|
bgneal@115
|
631 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
632 else:
|
bgneal@115
|
633 form = SplitTopicForm(request.user)
|
bgneal@115
|
634
|
bgneal@115
|
635 posts = topic.posts.select_related()
|
bgneal@115
|
636
|
bgneal@115
|
637 return render_to_response('forums/mod_split_topic.html', {
|
bgneal@115
|
638 'forum': topic.forum,
|
bgneal@115
|
639 'topic': topic,
|
bgneal@115
|
640 'posts': posts,
|
bgneal@115
|
641 'form': form,
|
bgneal@115
|
642 },
|
bgneal@115
|
643 context_instance=RequestContext(request))
|
bgneal@115
|
644
|
bgneal@115
|
645
|
bgneal@167
|
646 @login_required
|
bgneal@167
|
647 def unread_topics(request):
|
bgneal@168
|
648 """Displays the topics with unread posts for a given user."""
|
bgneal@168
|
649
|
bgneal@167
|
650 topics = get_unread_topics(request.user)
|
bgneal@167
|
651
|
bgneal@167
|
652 paginator = create_topic_paginator(topics)
|
bgneal@167
|
653 page_num = get_page_num(request)
|
bgneal@167
|
654 try:
|
bgneal@167
|
655 page = paginator.page(page_num)
|
bgneal@167
|
656 except InvalidPage:
|
bgneal@167
|
657 raise Http404
|
bgneal@167
|
658
|
bgneal@167
|
659 attach_topic_page_ranges(page.object_list)
|
bgneal@167
|
660
|
bgneal@167
|
661 # we do this for the template since it is rendered twice
|
bgneal@167
|
662 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@167
|
663
|
bgneal@167
|
664 return render_to_response('forums/topic_list.html', {
|
bgneal@167
|
665 'title': 'Topics With Unread Posts',
|
bgneal@167
|
666 'page': page,
|
bgneal@167
|
667 'page_nav': page_nav,
|
bgneal@167
|
668 },
|
bgneal@167
|
669 context_instance=RequestContext(request))
|
bgneal@167
|
670
|
bgneal@167
|
671
|
bgneal@168
|
672 def unanswered_topics(request):
|
bgneal@168
|
673 """Displays the topics with no replies."""
|
bgneal@168
|
674
|
bgneal@168
|
675 forum_ids = Forum.objects.forum_ids_for_user(request.user)
|
bgneal@168
|
676 topics = Topic.objects.filter(forum__id__in=forum_ids,
|
bgneal@168
|
677 post_count=1).select_related(
|
bgneal@168
|
678 'forum', 'user', 'last_post', 'last_post__user')
|
bgneal@168
|
679
|
bgneal@168
|
680 paginator = create_topic_paginator(topics)
|
bgneal@168
|
681 page_num = get_page_num(request)
|
bgneal@168
|
682 try:
|
bgneal@168
|
683 page = paginator.page(page_num)
|
bgneal@168
|
684 except InvalidPage:
|
bgneal@168
|
685 raise Http404
|
bgneal@168
|
686
|
bgneal@168
|
687 attach_topic_page_ranges(page.object_list)
|
bgneal@168
|
688
|
bgneal@168
|
689 # we do this for the template since it is rendered twice
|
bgneal@168
|
690 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@168
|
691
|
bgneal@168
|
692 return render_to_response('forums/topic_list.html', {
|
bgneal@168
|
693 'title': 'Unanswered Topics',
|
bgneal@168
|
694 'page': page,
|
bgneal@168
|
695 'page_nav': page_nav,
|
bgneal@168
|
696 },
|
bgneal@168
|
697 context_instance=RequestContext(request))
|
bgneal@168
|
698
|
bgneal@168
|
699
|
bgneal@109
|
700 def _can_moderate(forum, user):
|
bgneal@109
|
701 """
|
bgneal@109
|
702 Determines if a user has permission to moderate a given forum.
|
bgneal@109
|
703 """
|
bgneal@109
|
704 return user.is_authenticated() and (
|
bgneal@109
|
705 user.is_superuser or user in forum.moderators.all())
|
bgneal@109
|
706
|
bgneal@109
|
707
|
bgneal@108
|
708 def _can_post_in_topic(topic, user):
|
bgneal@108
|
709 """
|
bgneal@108
|
710 This function returns true if the given user can post in the given topic
|
bgneal@108
|
711 and false otherwise.
|
bgneal@108
|
712 """
|
bgneal@108
|
713 return (not topic.locked and topic.forum.category.can_access(user)) or \
|
bgneal@108
|
714 (user.is_superuser or user in topic.forum.moderators.all())
|
bgneal@108
|
715
|
bgneal@108
|
716
|
bgneal@108
|
717 def _bump_post_count(user):
|
bgneal@108
|
718 """
|
bgneal@108
|
719 Increments the forum_post_count for the given user.
|
bgneal@108
|
720 """
|
bgneal@108
|
721 profile = user.get_profile()
|
bgneal@108
|
722 profile.forum_post_count += 1
|
bgneal@108
|
723 profile.save()
|
bgneal@108
|
724
|
bgneal@108
|
725
|
bgneal@108
|
726 def _quote_message(who, message):
|
bgneal@111
|
727 """
|
bgneal@111
|
728 Builds a message reply by quoting the existing message in a
|
bgneal@111
|
729 typical email-like fashion. The quoting is compatible with Markdown.
|
bgneal@111
|
730 """
|
bgneal@111
|
731 header = '*%s wrote:*\n\n' % (who, )
|
bgneal@111
|
732 lines = wrap(message, 55).split('\n')
|
bgneal@111
|
733 for i, line in enumerate(lines):
|
bgneal@111
|
734 lines[i] = '> ' + line
|
bgneal@111
|
735 return header + '\n'.join(lines)
|
bgneal@111
|
736
|
bgneal@111
|
737
|
bgneal@111
|
738 def _move_topic(topic, old_forum, new_forum):
|
bgneal@111
|
739 if new_forum != old_forum:
|
bgneal@111
|
740 topic.forum = new_forum
|
bgneal@111
|
741 topic.save()
|
bgneal@111
|
742 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
|
bgneal@112
|
743 old_forum.sync()
|
bgneal@111
|
744 old_forum.save()
|
bgneal@112
|
745 new_forum.sync()
|
bgneal@111
|
746 new_forum.save()
|
bgneal@111
|
747
|
bgneal@111
|
748
|
bgneal@111
|
749 def _bulk_sticky(forum, topic_ids):
|
bgneal@111
|
750 """
|
bgneal@111
|
751 Performs a toggle on the sticky status for a given list of topic ids.
|
bgneal@111
|
752 """
|
bgneal@111
|
753 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
754 for topic in topics:
|
bgneal@111
|
755 if topic.forum == forum:
|
bgneal@111
|
756 topic.sticky = not topic.sticky
|
bgneal@111
|
757 topic.save()
|
bgneal@111
|
758
|
bgneal@111
|
759
|
bgneal@111
|
760 def _bulk_lock(forum, topic_ids):
|
bgneal@111
|
761 """
|
bgneal@111
|
762 Performs a toggle on the locked status for a given list of topic ids.
|
bgneal@111
|
763 """
|
bgneal@111
|
764 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
765 for topic in topics:
|
bgneal@111
|
766 if topic.forum == forum:
|
bgneal@111
|
767 topic.locked = not topic.locked
|
bgneal@111
|
768 topic.save()
|
bgneal@111
|
769
|
bgneal@111
|
770
|
bgneal@111
|
771 def _bulk_delete(forum, topic_ids):
|
bgneal@111
|
772 """
|
bgneal@111
|
773 Deletes the list of topics.
|
bgneal@111
|
774 """
|
bgneal@111
|
775 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
776 for topic in topics:
|
bgneal@111
|
777 if topic.forum == forum:
|
bgneal@111
|
778 _delete_topic(topic)
|
bgneal@111
|
779
|
bgneal@111
|
780
|
bgneal@111
|
781 def _bulk_move(topic_ids, old_forum, new_forum):
|
bgneal@111
|
782 """
|
bgneal@111
|
783 Moves the list of topics to a new forum.
|
bgneal@111
|
784 """
|
bgneal@111
|
785 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
786 for topic in topics:
|
bgneal@111
|
787 if topic.forum == old_forum:
|
bgneal@111
|
788 _move_topic(topic, old_forum, new_forum)
|
bgneal@111
|
789
|
bgneal@113
|
790
|
bgneal@113
|
791 def _update_last_visit(user, topic):
|
bgneal@113
|
792 """
|
bgneal@113
|
793 Does the bookkeeping for the last visit status for the user to the
|
bgneal@113
|
794 topic/forum.
|
bgneal@113
|
795 """
|
bgneal@113
|
796 now = datetime.datetime.now()
|
bgneal@113
|
797 try:
|
bgneal@113
|
798 flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
|
bgneal@113
|
799 except ForumLastVisit.DoesNotExist:
|
bgneal@113
|
800 flv = ForumLastVisit(user=user, forum=topic.forum)
|
bgneal@113
|
801 flv.begin_date = now
|
bgneal@113
|
802
|
bgneal@113
|
803 flv.end_date = now
|
bgneal@113
|
804 flv.save()
|
bgneal@113
|
805
|
bgneal@113
|
806 if topic.update_date > flv.begin_date:
|
bgneal@113
|
807 try:
|
bgneal@113
|
808 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
|
bgneal@113
|
809 except TopicLastVisit.DoesNotExist:
|
bgneal@113
|
810 tlv = TopicLastVisit(user=user, topic=topic)
|
bgneal@113
|
811
|
bgneal@113
|
812 tlv.touch()
|
bgneal@113
|
813 tlv.save()
|
bgneal@113
|
814
|
bgneal@115
|
815
|
bgneal@115
|
816 def _split_topic_at(topic, post_id, new_forum, new_name):
|
bgneal@115
|
817 """
|
bgneal@115
|
818 This function splits the post given by post_id and all posts that come
|
bgneal@115
|
819 after it in the given topic to a new topic in a new forum.
|
bgneal@115
|
820 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
821 """
|
bgneal@115
|
822 post = get_object_or_404(Post, id=post_id)
|
bgneal@115
|
823 if post.topic == topic:
|
bgneal@115
|
824 post_ids = Post.objects.filter(topic=topic,
|
bgneal@115
|
825 creation_date__gte=post.creation_date).values_list('id', flat=True)
|
bgneal@115
|
826 _split_topic(topic, post_ids, new_forum, new_name)
|
bgneal@115
|
827
|
bgneal@115
|
828
|
bgneal@115
|
829 def _split_topic(topic, post_ids, new_forum, new_name):
|
bgneal@115
|
830 """
|
bgneal@115
|
831 This function splits the posts given by the post_ids list in the
|
bgneal@115
|
832 given topic to a new topic in a new forum.
|
bgneal@115
|
833 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
834 """
|
bgneal@115
|
835 posts = Post.objects.filter(topic=topic, id__in=post_ids)
|
bgneal@115
|
836 if len(posts) > 0:
|
bgneal@115
|
837 new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
|
bgneal@115
|
838 new_topic.save()
|
bgneal@115
|
839 for post in posts:
|
bgneal@115
|
840 post.topic = new_topic
|
bgneal@115
|
841 post.save()
|
bgneal@115
|
842
|
bgneal@115
|
843 topic.post_count_update()
|
bgneal@115
|
844 topic.save()
|
bgneal@115
|
845 new_topic.post_count_update()
|
bgneal@115
|
846 new_topic.save()
|
bgneal@115
|
847 topic.forum.sync()
|
bgneal@115
|
848 topic.forum.save()
|
bgneal@115
|
849 new_forum.sync()
|
bgneal@115
|
850 new_forum.save()
|