Mercurial > public > sg101
comparison gpp/forums/views.py @ 110:c329bfaed4a7
Forums: implemented the move topic feature.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 Sep 2009 20:19:45 +0000 |
parents | 07be3e39e639 |
children | e5faf9f0c11a |
comparison
equal
deleted
inserted
replaced
109:07be3e39e639 | 110:c329bfaed4a7 |
---|---|
20 from core.functions import email_admins | 20 from core.functions import email_admins |
21 from forums.models import Forum | 21 from forums.models import Forum |
22 from forums.models import Topic | 22 from forums.models import Topic |
23 from forums.models import Post | 23 from forums.models import Post |
24 from forums.models import FlaggedPost | 24 from forums.models import FlaggedPost |
25 from forums.forms import NewTopicForm, NewPostForm, PostForm | 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm |
26 | 26 |
27 ####################################################################### | 27 ####################################################################### |
28 | 28 |
29 TOPICS_PER_PAGE = 50 | 29 TOPICS_PER_PAGE = 50 |
30 POSTS_PER_PAGE = 2 | 30 POSTS_PER_PAGE = 2 |
396 if _can_moderate(topic.forum, request.user): | 396 if _can_moderate(topic.forum, request.user): |
397 topic.sticky = not topic.sticky | 397 topic.sticky = not topic.sticky |
398 topic.save() | 398 topic.save() |
399 return HttpResponseRedirect(topic.get_absolute_url()) | 399 return HttpResponseRedirect(topic.get_absolute_url()) |
400 | 400 |
401 return HttpResponseForbidden() | 401 return HttpResponseForbidden() |
402 | 402 |
403 | 403 |
404 @login_required | 404 @login_required |
405 def mod_topic_lock(request, id): | 405 def mod_topic_lock(request, id): |
406 """ | 406 """ |
410 if _can_moderate(topic.forum, request.user): | 410 if _can_moderate(topic.forum, request.user): |
411 topic.locked = not topic.locked | 411 topic.locked = not topic.locked |
412 topic.save() | 412 topic.save() |
413 return HttpResponseRedirect(topic.get_absolute_url()) | 413 return HttpResponseRedirect(topic.get_absolute_url()) |
414 | 414 |
415 return HttpResponseForbidden() | 415 return HttpResponseForbidden() |
416 | 416 |
417 | 417 |
418 @login_required | 418 @login_required |
419 def mod_topic_delete(request, id): | 419 def mod_topic_delete(request, id): |
420 """ | 420 """ |
424 if _can_moderate(topic.forum, request.user): | 424 if _can_moderate(topic.forum, request.user): |
425 forum_url = topic.forum.get_absolute_url() | 425 forum_url = topic.forum.get_absolute_url() |
426 _delete_topic(topic) | 426 _delete_topic(topic) |
427 return HttpResponseRedirect(forum_url) | 427 return HttpResponseRedirect(forum_url) |
428 | 428 |
429 return HttpResponseForbidden() | 429 return HttpResponseForbidden() |
430 | |
431 | |
432 @login_required | |
433 def mod_topic_move(request, id): | |
434 """ | |
435 This view function is for moderators to move a topic to a different forum. | |
436 """ | |
437 topic = get_object_or_404(Topic.objects.select_related(), pk=id) | |
438 if not _can_moderate(topic.forum, request.user): | |
439 return HttpResponseForbidden() | |
440 | |
441 if request.method == 'POST': | |
442 form = MoveTopicForm(request.user, request.POST) | |
443 if form.is_valid(): | |
444 new_forum = form.cleaned_data['forums'] | |
445 old_forum = topic.forum | |
446 if new_forum != old_forum: | |
447 topic.forum = new_forum | |
448 topic.save() | |
449 # Have to adjust foreign keys to last_post, denormalized counts, etc.: | |
450 old_forum.topic_count_update() | |
451 old_forum.post_count_update() | |
452 old_forum.save() | |
453 new_forum.topic_count_update() | |
454 new_forum.post_count_update() | |
455 new_forum.save() | |
456 | |
457 return HttpResponseRedirect(topic.get_absolute_url()) | |
458 else: | |
459 form = MoveTopicForm(request.user) | |
460 | |
461 return render_to_response('forums/move_topic.html', { | |
462 'forum': topic.forum, | |
463 'topic': topic, | |
464 'form': form, | |
465 }, | |
466 context_instance=RequestContext(request)) | |
430 | 467 |
431 | 468 |
432 def _can_moderate(forum, user): | 469 def _can_moderate(forum, user): |
433 """ | 470 """ |
434 Determines if a user has permission to moderate a given forum. | 471 Determines if a user has permission to moderate a given forum. |