comparison gpp/forums/views.py @ 111:e5faf9f0c11a

Forums: implemented the bulk moderator functions that operate on a forum: bulk sticky, lock, delete, and move. These haven't been tested that well yet.
author Brian Neal <bgneal@gmail.com>
date Mon, 28 Sep 2009 03:57:09 +0000
parents c329bfaed4a7
children d1b0b86441c0
comparison
equal deleted inserted replaced
110:c329bfaed4a7 111:e5faf9f0c11a
76 except InvalidPage: 76 except InvalidPage:
77 raise Http404 77 raise Http404
78 78
79 # we do this for the template since it is rendered twice 79 # we do this for the template since it is rendered twice
80 page_nav = render_to_string('forums/pagination.html', {'page': page}) 80 page_nav = render_to_string('forums/pagination.html', {'page': page})
81
82 can_moderate = _can_moderate(forum, request.user)
81 83
82 return render_to_response('forums/forum_index.html', { 84 return render_to_response('forums/forum_index.html', {
83 'forum': forum, 85 'forum': forum,
84 'page': page, 86 'page': page,
85 'page_nav': page_nav, 87 'page_nav': page_nav,
88 'can_moderate': can_moderate,
86 }, 89 },
87 context_instance=RequestContext(request)) 90 context_instance=RequestContext(request))
88 91
89 92
90 def topic_index(request, id): 93 def topic_index(request, id):
441 if request.method == 'POST': 444 if request.method == 'POST':
442 form = MoveTopicForm(request.user, request.POST) 445 form = MoveTopicForm(request.user, request.POST)
443 if form.is_valid(): 446 if form.is_valid():
444 new_forum = form.cleaned_data['forums'] 447 new_forum = form.cleaned_data['forums']
445 old_forum = topic.forum 448 old_forum = topic.forum
446 if new_forum != old_forum: 449 _move_topic(topic, old_forum, new_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()) 450 return HttpResponseRedirect(topic.get_absolute_url())
458 else: 451 else:
459 form = MoveTopicForm(request.user) 452 form = MoveTopicForm(request.user)
460 453
461 return render_to_response('forums/move_topic.html', { 454 return render_to_response('forums/move_topic.html', {
464 'form': form, 457 'form': form,
465 }, 458 },
466 context_instance=RequestContext(request)) 459 context_instance=RequestContext(request))
467 460
468 461
462 @login_required
463 def mod_forum(request, slug):
464 """
465 Displays a view to allow moderators to perform various operations
466 on topics in a forum in bulk. We currently support mass locking/unlocking,
467 stickying and unstickying, moving, and deleting topics.
468 """
469 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
470 if not _can_moderate(forum, request.user):
471 return HttpResponseForbidden()
472
473 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
474 paginator = create_topic_paginator(topics)
475 page_num = int(request.REQUEST.get('page', 1))
476 try:
477 page = paginator.page(page_num)
478 except InvalidPage:
479 raise Http404
480
481 # we do this for the template since it is rendered twice
482 page_nav = render_to_string('forums/pagination.html', {'page': page})
483 form = None
484
485 if request.method == 'POST':
486 topic_ids = request.POST.getlist('topic_ids')
487 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
488 url += '?page=%s' % page_num
489
490 if len(topic_ids):
491 if request.POST.get('sticky'):
492 _bulk_sticky(forum, topic_ids)
493 return HttpResponseRedirect(url)
494 elif request.POST.get('lock'):
495 _bulk_lock(forum, topic_ids)
496 return HttpResponseRedirect(url)
497 elif request.POST.get('delete'):
498 _bulk_delete(forum, topic_ids)
499 return HttpResponseRedirect(url)
500 elif request.POST.get('move'):
501 form = MoveTopicForm(request.user, request.POST, hide_label=True)
502 if form.is_valid():
503 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
504 return HttpResponseRedirect(url)
505
506 if form is None:
507 form = MoveTopicForm(request.user, hide_label=True)
508
509 return render_to_response('forums/mod_forum.html', {
510 'forum': forum,
511 'page': page,
512 'page_nav': page_nav,
513 'form': form,
514 },
515 context_instance=RequestContext(request))
516
517
469 def _can_moderate(forum, user): 518 def _can_moderate(forum, user):
470 """ 519 """
471 Determines if a user has permission to moderate a given forum. 520 Determines if a user has permission to moderate a given forum.
472 """ 521 """
473 return user.is_authenticated() and ( 522 return user.is_authenticated() and (
491 profile.forum_post_count += 1 540 profile.forum_post_count += 1
492 profile.save() 541 profile.save()
493 542
494 543
495 def _quote_message(who, message): 544 def _quote_message(who, message):
496 """ 545 """
497 Builds a message reply by quoting the existing message in a 546 Builds a message reply by quoting the existing message in a
498 typical email-like fashion. The quoting is compatible with Markdown. 547 typical email-like fashion. The quoting is compatible with Markdown.
499 """ 548 """
500 header = '*%s wrote:*\n\n' % (who, ) 549 header = '*%s wrote:*\n\n' % (who, )
501 lines = wrap(message, 55).split('\n') 550 lines = wrap(message, 55).split('\n')
502 for i, line in enumerate(lines): 551 for i, line in enumerate(lines):
503 lines[i] = '> ' + line 552 lines[i] = '> ' + line
504 return header + '\n'.join(lines) 553 return header + '\n'.join(lines)
554
555
556 def _move_topic(topic, old_forum, new_forum):
557 if new_forum != old_forum:
558 topic.forum = new_forum
559 topic.save()
560 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
561 old_forum.topic_count_update()
562 old_forum.post_count_update()
563 old_forum.save()
564 new_forum.topic_count_update()
565 new_forum.post_count_update()
566 new_forum.save()
567
568
569 def _bulk_sticky(forum, topic_ids):
570 """
571 Performs a toggle on the sticky status for a given list of topic ids.
572 """
573 topics = Topic.objects.filter(pk__in=topic_ids)
574 for topic in topics:
575 if topic.forum == forum:
576 topic.sticky = not topic.sticky
577 topic.save()
578
579
580 def _bulk_lock(forum, topic_ids):
581 """
582 Performs a toggle on the locked status for a given list of topic ids.
583 """
584 topics = Topic.objects.filter(pk__in=topic_ids)
585 for topic in topics:
586 if topic.forum == forum:
587 topic.locked = not topic.locked
588 topic.save()
589
590
591 def _bulk_delete(forum, topic_ids):
592 """
593 Deletes the list of topics.
594 """
595 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
596 for topic in topics:
597 if topic.forum == forum:
598 _delete_topic(topic)
599
600
601 def _bulk_move(topic_ids, old_forum, new_forum):
602 """
603 Moves the list of topics to a new forum.
604 """
605 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
606 for topic in topics:
607 if topic.forum == old_forum:
608 _move_topic(topic, old_forum, new_forum)
609