changeset 110:c329bfaed4a7

Forums: implemented the move topic feature.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Sep 2009 20:19:45 +0000 (2009-09-26)
parents 07be3e39e639
children e5faf9f0c11a
files gpp/forums/forms.py gpp/forums/urls.py gpp/forums/views.py gpp/templates/forums/move_topic.html gpp/templates/forums/topic.html media/icons/application_go.png
diffstat 6 files changed, 81 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/forms.py	Sat Sep 26 18:03:57 2009 +0000
+++ b/gpp/forums/forms.py	Sat Sep 26 20:19:45 2009 +0000
@@ -4,6 +4,7 @@
 from django import forms
 from django.conf import settings
 
+from forums.models import Forum
 from forums.models import Topic
 from forums.models import Post
 
@@ -105,3 +106,17 @@
         }
         js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
             ('js/forums.js', )
+
+
+class MoveTopicForm(forms.Form):
+   """
+   Form for a moderator to move a topic to a forum.
+   """
+   forums = forms.ModelChoiceField(label='Move to forum', 
+         queryset=Forum.objects.none())
+
+   def __init__(self, user, *args, **kwargs):
+      super(MoveTopicForm, self).__init__(*args, **kwargs)
+      self.fields['forums'].queryset = \
+              Forum.objects.forums_for_user(user).order_by('name')
+
--- a/gpp/forums/urls.py	Sat Sep 26 18:03:57 2009 +0000
+++ b/gpp/forums/urls.py	Sat Sep 26 20:19:45 2009 +0000
@@ -12,9 +12,10 @@
     url(r'^flag-post/$', 'flag_post', name='forums-flag_post'),
     url(r'^forum/(?P<slug>[\w\d-]+)/$', 'forum_index', name='forums-forum_index'),
     url(r'^forum/(?P<slug>[\w\d-]+)/new-topic/$', 'new_topic', name='forums-new_topic'),
+    url(r'^mod/topic/delete/(\d+)/$', 'mod_topic_delete', name='forums-mod_topic_delete'),
     url(r'^mod/topic/lock/(\d+)/$', 'mod_topic_lock', name='forums-mod_topic_lock'),
+    url(r'^mod/topic/move/(\d+)/$', 'mod_topic_move', name='forums-mod_topic_move'),
     url(r'^mod/topic/stick/(\d+)/$', 'mod_topic_stick', name='forums-mod_topic_stick'),
-    url(r'^mod/topic/delete/(\d+)/$', 'mod_topic_delete', name='forums-mod_topic_delete'),
     url(r'^post/(\d+)/$', 'goto_post', name='forums-goto_post'),
     url(r'^post/new/(?P<topic_id>\d+)/$', 'new_post', name='forums-new_post'),
     url(r'^quick-reply/$', 'quick_reply_ajax', name='forums-quick_reply'),
--- a/gpp/forums/views.py	Sat Sep 26 18:03:57 2009 +0000
+++ b/gpp/forums/views.py	Sat Sep 26 20:19:45 2009 +0000
@@ -22,7 +22,7 @@
 from forums.models import Topic
 from forums.models import Post
 from forums.models import FlaggedPost
-from forums.forms import NewTopicForm, NewPostForm, PostForm
+from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm
 
 #######################################################################
 
@@ -398,7 +398,7 @@
         topic.save()
         return HttpResponseRedirect(topic.get_absolute_url())
 
-    return HttpResponseForbidden()    
+    return HttpResponseForbidden()
 
 
 @login_required
@@ -412,7 +412,7 @@
         topic.save()
         return HttpResponseRedirect(topic.get_absolute_url())
 
-    return HttpResponseForbidden()    
+    return HttpResponseForbidden()
 
 
 @login_required
@@ -426,7 +426,44 @@
         _delete_topic(topic)
         return HttpResponseRedirect(forum_url)
 
-    return HttpResponseForbidden()    
+    return HttpResponseForbidden()
+
+
+@login_required
+def mod_topic_move(request, id):
+    """
+    This view function is for moderators to move a topic to a different forum.
+    """
+    topic = get_object_or_404(Topic.objects.select_related(), pk=id)
+    if not _can_moderate(topic.forum, request.user):
+        return HttpResponseForbidden()
+
+    if request.method == 'POST':
+        form = MoveTopicForm(request.user, request.POST)
+        if form.is_valid():
+            new_forum = form.cleaned_data['forums']
+            old_forum = topic.forum
+            if new_forum != old_forum:
+                topic.forum = new_forum
+                topic.save()
+                # Have to adjust foreign keys to last_post, denormalized counts, etc.:
+                old_forum.topic_count_update()
+                old_forum.post_count_update()
+                old_forum.save()
+                new_forum.topic_count_update()
+                new_forum.post_count_update()
+                new_forum.save()
+
+            return HttpResponseRedirect(topic.get_absolute_url())
+    else:
+        form = MoveTopicForm(request.user)
+
+    return render_to_response('forums/move_topic.html', {
+        'forum': topic.forum,
+        'topic': topic,
+        'form': form,
+        },
+        context_instance=RequestContext(request))
 
 
 def _can_moderate(forum, user):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/move_topic.html	Sat Sep 26 20:19:45 2009 +0000
@@ -0,0 +1,21 @@
+{% extends 'base.html' %}
+{% block title %}Forums: Move Topic{% endblock %}
+{% block content %}
+<h2>Forums: Move Topic</h2>
+
+<h3>
+   <a href="{% url forums-index %}">SurfGuitar101 Forum Index</a> &raquo;
+   <a href="{% url forums-forum_index slug=forum.slug %}">{{ forum.name }}</a> &raquo;
+   <a href="{% url forums-topic_index id=topic.id %}">{{ topic.name }}</a>
+</h3>
+
+<div class="forum-block">
+<form action="." method="post">
+<fieldset>
+<legend>Move Topic: {{ topic.name }}</legend>
+   {{ form.as_p }}
+   <input type="submit" value="Move Topic" />
+</fieldset>
+</form>
+</div>
+{% endblock %}
--- a/gpp/templates/forums/topic.html	Sat Sep 26 18:03:57 2009 +0000
+++ b/gpp/templates/forums/topic.html	Sat Sep 26 20:19:45 2009 +0000
@@ -45,6 +45,8 @@
    <form action="{% url forums-mod_topic_delete topic.id %}" method="post">
       <input type="submit" value="Delete Topic" id="forum-mod-del-topic" />
    </form>
+   <a href="{% url forums-mod_topic_move topic.id %}"><img src="{{ MEDIA_URL }}icons/application_go.png" alt="Move Topic" title="Move Topic" /></a>
+   <a href="{% url forums-mod_topic_move topic.id %}">Move this topic</a>
 </div>
 {% endif %}
 
Binary file media/icons/application_go.png has changed