def editComment(request, comment_id): # Extract the GET variable, this is a string revision = request.GET.get('diff') # Get a list of object references for the revisions for this post in descending order revisions = CommentRevision.objects.filter(comment=comment_id).order_by('-id') # Set the revision to show the diff for, depending on the GET variable if revision is None and len(revisions) > 0: rev = revisions[0] else: try: rev = CommentRevision.objects.get(id=revision) except CommentRevision.DoesNotExist: # If we don't have a value for rev, set it None so it can still be passed to template rev = None comment = Comment.objects.get(id=comment_id) # Check if we've hit the revert button, if so make revision and reload. if request.method == 'POST' and 'revert' in request.POST: # Copy the revision title and body to the current post comment.author = rev.author comment.homepage = rev.homepage comment.body = rev.body comment.save(False) # Delete the revision we've reverted rev.delete() return HttpResponseRedirect("") try: form = formhandlerExistingComment(request, comment) except ValidExistingComment: return HttpResponseRedirect(reverse('edit_post', args=[comment.post.id])) diff = [] # Calculate diff with Google's tool. If we didn't set these to None before, we'll set them now if rev is not None: d = Diff() diff_author_list = d.diff_main(comment.author, rev.author) diff.append(d.diff_prettyHtml(diff_author_list)) diff_homepage_list = d.diff_main(comment.homepage, rev.homepage) diff.append(d.diff_prettyHtml(diff_homepage_list)) diff_body_list = d.diff_main(comment.body, rev.body) diff.append(d.diff_prettyHtml(diff_body_list)) return render_to_response('manager/comment_manager.html', { 'comment': comment, 'form': form, 'revisions': revisions, 'rev': rev, 'diff': diff, }, context_instance=RequestContext(request))
def editPost(request, post_id): # Extract the GET variable, this is a string revision = request.GET.get('diff') # Get a list of object references for the revisions for this post in descending order revisions = PostRevision.objects.filter(post=post_id).order_by('-id') # Set the revision to show the diff for, depending on the GET variable if revision is None and len(revisions) > 0: rev = revisions[0] else: try: rev = PostRevision.objects.get(id=revision) except PostRevision.DoesNotExist: # If we don't have a value for rev, set it to None so can still be passed to template rev = None # Get the referenced post, assume it is valid since this is from the manager panel post = Post.objects.get(id=post_id) # Get the comments, can't check the related object for emptiness... comments = Comment.objects.filter(post=post) # Check if we've hit the revert button, if so make revision and reload. if request.method == 'POST' and 'revert' in request.POST: # Copy the revision title and body to the current post post.title = rev.title post.body = rev.body post.save(False) # Delete the revision we've reverted rev.delete() return HttpResponseRedirect("") # Do form processing try: form = formhandlerExistingPost(request, post) except PreviewPost as e: return render_to_response('manager/post_preview.html', { 'post': e.post, 'form': e.form, }, context_instance=RequestContext(request)) except CloseExistingPost as e: return HttpResponseRedirect(reverse('edit_post', args=[post.id])) except ValidExistingPost: return HttpResponseRedirect(reverse('manage_blog')) diff = [] # Calculate diff with Google's tool. If we didn't set these to None before, we'll set them now if rev is not None: d = Diff() diff_title_list = d.diff_main(post.title, rev.title) diff.append(d.diff_prettyHtml(diff_title_list)) diff_body_list = d.diff_main(post.body, rev.body) diff.append(d.diff_prettyHtml(diff_body_list)) # We are either showing an unedited form or one that is bound to data, with errors return render_to_response('manager/post_manager.html', { 'post': post, 'comments': comments, 'form': form, 'revisions': revisions, 'rev': rev, 'diff': diff, }, context_instance=RequestContext(request))