Skip to content

Instantly share code, notes, and snippets.

@danjac
Created January 16, 2021 10:52
Show Gist options
  • Save danjac/ab5ce15a6da9f85e66746452874fce53 to your computer and use it in GitHub Desktop.
Save danjac/ab5ce15a6da9f85e66746452874fce53 to your computer and use it in GitHub Desktop.
@login_required
@community_required
@add_messages_to_response_header
def comment_update_view(request, pk):
comment = get_object_or_404(
Comment.objects.for_community(request.community).select_related(
"owner", "community"
),
pk=pk,
)
has_perm_or_403(request.user, "comments.change_comment", comment)
form = CommentForm(
request.POST if request.method == "POST" else None, instance=comment,
)
frame = TurboFrame(f"comment-{comment.id}-content")
if request.method == "POST" and form.is_valid():
comment = form.save(commit=False)
comment.editor = request.user
comment.edited = timezone.now()
comment.save()
comment.notify_on_update()
messages.success(request, _("Your comment has been updated"))
return frame.template(
"comments/includes/content.html", {"comment": comment}
).response(request)
return frame.template(
"comments/includes/comment_form.html", {"form": form, "comment": comment}
).response(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment