Add support for editing posts

This commit is contained in:
Trever Fischer
2012-08-29 22:18:41 -04:00
parent e962307adc
commit eed9a6df1b
5 changed files with 41 additions and 2 deletions

View File

@@ -10,6 +10,8 @@ urlpatterns = patterns('forums',
url(r'^sticky/(?P<topicID>.*)', 'views.stickyTopic'),
url(r'^reply/(?P<topicID>.*)', 'views.reply'),
url(r'^reply$', 'views.reply'),
url(r'^edit/(?P<postID>.*)', 'views.editPost'),
url(r'^edit$', 'views.editPost'),
url(r'^post/(?P<id>.*)$', 'views.post'),
url(r'^preview$', 'views.preview'),
)

View File

@@ -7,6 +7,7 @@ from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from notification import models as notification
def index(request):
@@ -44,7 +45,7 @@ def reply(request, topicID=None):
notification.send([reply.parent.user], "forum_reply", {"reply": reply, 'notice_url': reverse('forums.views.post', kwargs={'id':reply.id}), 'notice_description': reply.topic().title})
messages.info(request, "Reply successful")
return HttpResponseRedirect(reverse('forums.views.post', kwargs={"id":reply.id}))
return render_to_response('forums/reply.html', {"post":parentPost, "form":form}, context_instance = RequestContext(request))
return render_to_response('forums/reply.html', {"parent":parentPost, "form":form}, context_instance = RequestContext(request))
@login_required
def newTopic(request, forumID=None):
@@ -83,6 +84,25 @@ def newTopic(request, forumID=None):
return HttpResponseRedirect(reverse('forums.views.post', kwargs={'id': reply.id}))
return render_to_response('forums/newTopic.html', {"forum":parentForum, "replyForm":replyForm, "topicForm": topicForm}, context_instance = RequestContext(request))
@login_required
def editPost(request, postID=None):
post = models.Post.objects.get(id__exact=postID)
if post.user != request.user and not request.user.has_perm('edit_posts'):
raise PermissionDenied
if request.method == 'POST':
form = forms.ReplyForm(request.POST)
else:
form = forms.ReplyForm(instance=post)
if form.is_valid():
post.body = form.cleaned_data['body']
post.save()
messages.info(request, "Post updated.")
return HttpResponseRedirect(reverse('forums.views.post',
kwargs={"id":post.id}))
return render_to_response('forums/edit.html', {"post": post, "parent":
post.parent, "form": form}, context_instance = RequestContext(request))
@permission_required('forums.delete_topic')
def deleteTopic(request, topicID):
topic = models.Topic.objects.get(id__exact=topicID)

View File

@@ -15,5 +15,8 @@ Edited {{post.updated}}
<br style="clear:both"/>
<a href="{% url forums.views.post id=post.id%}">permalink</a>
<a href="{%url forums.views.reply post.id%}">Reply &raquo;</a>
{% if post.user == user or perms.forums.edit_posts %}
<a href="{%url forums.views.editPost post.id%}">Edit &raquo;</a>
{% endif %}
</div>
</div>

View File

@@ -0,0 +1,10 @@
{% extends "forums/reply.html" %}
{% block form %}
{{post}}
<form method="POST" action="{% url forums.views.editPost postID=post.pk %}">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit"/>
</form>
{% endblock %}

View File

@@ -19,16 +19,20 @@ $(function() {
{% endblock %}
{%block content%}
{% if parent %}
<div class="item">
<h2>Replying to:</h2>
{% include 'forums/_reply.html' with post=post %}
{% include 'forums/_reply.html' with post=parent%}
</div>
{% endif %}
<div class="item">
{% block form %}
<form method="POST" action="{%url forums.views.reply post.id %}">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit"/>
</form>
{% endblock %}
</div>
<div class="item" id="forum-preview">
</div>