# HG changeset patch # User Brian Neal # Date 1253396996 0 # Node ID cb72577785dfe4e2e4aace8fc57bc6f39a765912 # Parent 08ddfd835305ddb700a4280e1fc9d0f7e74a67a0 Forums: implemented the edit post function. diff -r 08ddfd835305 -r cb72577785df gpp/forums/forms.py --- 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', ) diff -r 08ddfd835305 -r cb72577785df gpp/forums/templatetags/forum_tags.py --- 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, } diff -r 08ddfd835305 -r cb72577785df gpp/forums/urls.py --- 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\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'), url(r'^topic/(?P\d+)/$', 'topic_index', name='forums-topic_index'), + url(r'^edit/(?P\d+)/$', 'edit_post', name='forums-edit_post'), url(r'^flag-post/$', 'flag_post', name='forums-flag_post'), url(r'^forum/(?P[\w\d-]+)/$', 'forum_index', name='forums-forum_index'), url(r'^forum/(?P[\w\d-]+)/new-topic/$', 'new_topic', name='forums-new_topic'), diff -r 08ddfd835305 -r cb72577785df gpp/forums/views.py --- 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)) diff -r 08ddfd835305 -r cb72577785df gpp/templates/forums/display_post.html --- 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 @@
{{ post.html|safe }} + {% ifnotequal post.creation_date post.update_date %} +

Last edited: {{ post.update_date|date:"M d, Y H:i:s" }}

+ {% endifnotequal %}
diff -r 08ddfd835305 -r cb72577785df gpp/templates/forums/edit_post.html --- /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 %} +

Forums: Edit Post

+ +

+ SurfGuitar101 Forum Index » + {{ forum.name }} » + {{ topic.name }} +

+ +
+ +{% include 'forums/display_post.html' %} +
+ + +
+
+Edit Post +{{ form.as_p }} + +
+
+
+{% endblock %} diff -r 08ddfd835305 -r cb72577785df gpp/templates/forums/post_edit_button.html --- 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 %} -Edit post +Edit post {% endif %}