changeset 106:cb72577785df

Forums: implemented the edit post function.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Sep 2009 21:49:56 +0000
parents 08ddfd835305
children e94398f5e027
files gpp/forums/forms.py gpp/forums/templatetags/forum_tags.py gpp/forums/urls.py gpp/forums/views.py gpp/templates/forums/display_post.html gpp/templates/forums/edit_post.html gpp/templates/forums/post_edit_button.html
diffstat 7 files changed, 88 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/forms.py	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/forums/forms.py	Sat Sep 19 21:49:56 2009 +0000
@@ -17,7 +17,7 @@
     profile.save()
 
 
-class PostForm(forms.Form):
+class NewPostForm(forms.Form):
     """Form for creating a new post."""
     body = forms.CharField(label='', widget=forms.Textarea)
     topic_id = forms.IntegerField(widget=forms.HiddenInput)
@@ -99,3 +99,21 @@
 
         bump_post_count(self.user)
         return topic
+
+
+class PostForm(forms.ModelForm):
+    """
+    Form for editing an existing post.
+    """
+    body = forms.CharField(label='', widget=forms.Textarea)
+
+    class Meta:
+        model = Post
+        fields = ('body', )
+
+    class Media:
+        css = {
+            'all': settings.GPP_THIRD_PARTY_CSS['markitup'],
+        }
+        js = settings.GPP_THIRD_PARTY_JS['markitup'] + \
+            ('js/forums.js', )
--- a/gpp/forums/templatetags/forum_tags.py	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/forums/templatetags/forum_tags.py	Sat Sep 19 21:49:56 2009 +0000
@@ -23,6 +23,7 @@
 def post_edit_button(post, user, can_moderate, media_url):
     show_button = post.user.id == user.id or can_moderate
     return {
+        'post': post,
         'show_button': show_button,
         'MEDIA_URL': media_url,
     }
--- a/gpp/forums/urls.py	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/forums/urls.py	Sat Sep 19 21:49:56 2009 +0000
@@ -7,6 +7,7 @@
     url(r'^$', 'index', name='forums-index'),
     url(r'^new-topic-success/(?P<tid>\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'),
     url(r'^topic/(?P<id>\d+)/$', 'topic_index', name='forums-topic_index'),
+    url(r'^edit/(?P<id>\d+)/$', 'edit_post', name='forums-edit_post'),
     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'),
--- a/gpp/forums/views.py	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/forums/views.py	Sat Sep 19 21:49:56 2009 +0000
@@ -21,8 +21,7 @@
 from forums.models import Topic
 from forums.models import Post
 from forums.models import FlaggedPost
-from forums.forms import NewTopicForm
-from forums.forms import PostForm
+from forums.forms import NewTopicForm, NewPostForm, PostForm
 
 #######################################################################
 
@@ -127,7 +126,7 @@
         'last_page': last_page,
         'can_moderate': can_moderate,
         'can_reply': can_reply,
-        'form': PostForm(initial={'topic_id': topic.id}),
+        'form': NewPostForm(initial={'topic_id': topic.id}),
         },
         context_instance=RequestContext(request))
 
@@ -182,7 +181,7 @@
     if not request.user.is_authenticated():
         return HttpResponseForbidden()
 
-    form = PostForm(request.POST)
+    form = NewPostForm(request.POST)
     if form.is_valid():
         if form.topic.locked or not form.topic.forum.category.can_access(request.user):
             return HttpResponseForbidden()
@@ -236,3 +235,35 @@
 """)
     return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
             'Thanks for helping to improve the discussions on this site.')
+
+
+@login_required
+def edit_post(request, id):
+    """
+    This view function allows authorized users to edit posts.
+    The superuser, forum moderators, and original author can edit posts.
+    """
+    post = get_object_or_404(Post.objects.select_related(), pk=id)
+    can_edit = request.user == post.user or \
+            request.user.is_superuser or \
+            request.user in post.topic.forum.moderators.all()
+
+    if not can_edit:
+        return HttpResponseForbidden("You don't have permission to edit that post.")
+
+    if request.method == "POST":
+        form = PostForm(request.POST, instance=post)
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(post.get_absolute_url())
+    else:
+        form = PostForm(instance=post)
+
+    return render_to_response('forums/edit_post.html', {
+        'forum': post.topic.forum,
+        'topic': post.topic,
+        'post': post,
+        'form': form,
+        'can_moderate': True,
+        },
+        context_instance=RequestContext(request))
--- a/gpp/templates/forums/display_post.html	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/templates/forums/display_post.html	Sat Sep 19 21:49:56 2009 +0000
@@ -17,14 +17,17 @@
       </div>
       <div class="forum-post-body">
          {{ post.html|safe }}
+         {% ifnotequal post.creation_date post.update_date %}
+         <p class="small quiet">Last edited: {{ post.update_date|date:"M d, Y H:i:s" }}</p>
+         {% endifnotequal %}
       </div>
       <div class="forum-post-info-tools">
       <a href=""><img src="{{ MEDIA_URL }}icons/comment.png" alt="Reply with quote" title="Reply with quote" /></a>
       {% post_edit_button post user can_moderate MEDIA_URL %}
-      {% if can_moderate %}
       <a href="#" class="post-flag" id="fp-{{ post.id }}" 
          title="Flag this post as spam, abuse, or a violation of site rules.">
          <img src="{{ MEDIA_URL }}icons/flag_red.png" alt="Flag" /></a>
+      {% if can_moderate %}
       <a href=""><img src="{{ MEDIA_URL }}icons/cross.png" alt="Delete post" title="Delete post" /></a>
       {% endif %}
       </div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/templates/forums/edit_post.html	Sat Sep 19 21:49:56 2009 +0000
@@ -0,0 +1,27 @@
+{% extends 'base.html' %}
+{% block title %}Forums: Edit Post{% endblock %}
+{% block custom_js %}{{ form.media }}{% endblock %}
+{% block content %}
+<h2>Forums: Edit Post</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">
+<table class="forum-topic" id="forum-topic">
+{% include 'forums/display_post.html' %}
+</table>
+
+<a name="forum-reply-form"></a>
+<form action="." method="post" id="forums-quick-reply">
+<fieldset>
+<legend>Edit Post</legend>
+{{ form.as_p }}
+<input type="submit" value="Update Post" id="forums-edit-post" />
+</fieldset>
+</form>
+</div>
+{% endblock %}
--- a/gpp/templates/forums/post_edit_button.html	Thu Sep 17 02:20:27 2009 +0000
+++ b/gpp/templates/forums/post_edit_button.html	Sat Sep 19 21:49:56 2009 +0000
@@ -1,3 +1,3 @@
 {% if show_button %}
-<a href=""><img src="{{ MEDIA_URL }}icons/page_edit.png" alt="Edit post" title="Edit post" /></a>
+<a href="{% url forums-edit_post id=post.id %}"><img src="{{ MEDIA_URL }}icons/page_edit.png" alt="Edit post" title="Edit post" /></a>
 {% endif %}