Browse files

Add ability to edit a comment

  • Loading branch information...
1 parent a424f41 commit 34e839b8c8610d127b90c942fdfb6c54a8983527 @paltman paltman committed Apr 26, 2012
Showing with 31 additions and 3 deletions.
  1. +2 −1 dialogos/signals.py
  2. +3 −1 dialogos/urls.py
  3. +26 −1 dialogos/views.py
View
3 dialogos/signals.py
@@ -1,4 +1,5 @@
import django.dispatch
-commented = django.dispatch.Signal(providing_args=["comment", "request"])
+commented = django.dispatch.Signal(providing_args=["comment", "request"])
+comment_updated = django.dispatch.Signal(providing_args=["comment", "request"])
View
4 dialogos/urls.py
@@ -5,5 +5,7 @@
url(r"^comment/(?P<content_type_id>\d+)/(?P<object_id>\d+)/$", "post_comment",
name="post_comment"),
url(r"^comment/(?P<comment_id>\d+)/delete/$", "delete_comment",
- name="delete_comment")
+ name="delete_comment"),
+ url(r"^comment/(?P<comment_id>\d+)/edit/$", "edit_comment",
+ name="edit_comment")
)
View
27 dialogos/views.py
@@ -11,7 +11,7 @@
from dialogos.authorization import load_can_delete, load_can_edit
from dialogos.forms import CommentForm
from dialogos.models import Comment
-from dialogos.signals import commented
+from dialogos.signals import commented, comment_updated
can_delete = load_can_delete()
@@ -54,6 +54,31 @@ def post_comment(request, content_type_id, object_id, form_class=CommentForm):
@login_required
@require_POST
+def edit_comment(request, comment_id, form_class=CommentForm):
+ comment = get_object_or_404(Comment, pk=comment_id)
+ form = form_class(request.POST, initial=comment, request=request, obj=comment.content_object, user=request.user)
+ if form.is_valid():
+ comment = form.save()
+ comment_updated.send(sender=edit_comment, comment=comment, request=request)
+ if request.is_ajax():
+ return HttpResponse(json.dumps({
+ "status": "OK"
+ }), mimetype="application/json")
+ else:
+ if request.is_ajax():
+ return HttpResponse(json.dumps({
+ "status": "ERROR",
+ "errors": form.errors
+ }), mimetype="application/json")
+ redirect_to = request.POST.get("next")
+ # light security check -- make sure redirect_to isn't garbage.
+ if not redirect_to or " " in redirect_to or redirect_to.startswith("http"):
+ redirect_to = comment.content_object
+ return redirect(redirect_to)
+
+
+@login_required
+@require_POST
def delete_comment(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id)
obj = comment.content_object

0 comments on commit 34e839b

Please sign in to comment.