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