Created
March 5, 2010 20:13
-
-
Save nathanborror/323092 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @login_required | |
| def comment_edit(request, comment_id, template_name='comments/edit.html'): | |
| comment = get_object_or_404(Comment, pk=comment_id, user=request.user) | |
| if request.POST: | |
| data = request.POST.copy() | |
| if not data.get('name', ''): | |
| data["name"] = request.user.get_full_name() or request.user.username | |
| if not data.get('email', ''): | |
| data["email"] = request.user.email | |
| form = CommentForm(comment.content_object, data) | |
| if form.is_valid(): | |
| comment.comment = form.cleaned_data['comment'] | |
| comment.save() | |
| request.user.message_set.create(message="Your comment has been edited.") | |
| return HttpResponseRedirect(comment.content_object.get_absolute_url()) | |
| else: | |
| form = CommentForm(comment.content_object, initial={'comment': comment.comment}) | |
| return render(request, template_name, { | |
| 'form': form, | |
| 'comment': comment.comment | |
| }) | |
| @login_required | |
| def comment_delete(request, comment_id, template_confirm_name='comments/confirm_delete.html'): | |
| comment = get_object_or_404(Comment, pk=comment_id, user=request.user) | |
| if request.POST: | |
| content_object = comment.content_object | |
| comment.delete() | |
| request.user.message_set.create(message="Your comment has been deleted.") | |
| return HttpResponseRedirect(content_object.get_absolute_url()) | |
| else: | |
| return render(request, template_confirm_name, {'comment': comment}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment