Updated commenting to use ajax

This commit is contained in:
Correl Roush 2010-12-15 22:00:13 -05:00
parent 33a27c17be
commit f89a815b47
2 changed files with 32 additions and 7 deletions

View file

@ -1,4 +1,4 @@
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -55,4 +55,7 @@ def add_comment(request):
comment.author = request.user comment.author = request.user
comment.path = diff.b.path if diff.b else diff.a.path comment.path = diff.b.path if diff.b else diff.a.path
comment.save() comment.save()
return HttpResponseRedirect(reverse(edit, args=[review.pk])) data = RequestContext(request, {
'comment': comment,
})
return render_to_response('components/comment.html', data)

View file

@ -15,8 +15,7 @@
var line_b = info[3]; var line_b = info[3];
if ($('#' + form_id).length == 0) { if ($('#' + form_id).length == 0) {
// Show the comment form // Show the comment form
$(this).after( var form = $(
'<tr class="comment-form" id="' + form_id + '" ><td colspan="3">' +
'<form method="post" action="{% url codereview.review.views.add_comment %}">' + '<form method="post" action="{% url codereview.review.views.add_comment %}">' +
"{% csrf_token %}" + "{% csrf_token %}" +
'<input type="hidden" name="review" value="{{ review.pk }}" />' + '<input type="hidden" name="review" value="{{ review.pk }}" />' +
@ -26,8 +25,27 @@
'<textarea name="text" />' + '<textarea name="text" />' +
'<input type="submit" value="Add Comment" />' + '<input type="submit" value="Add Comment" />' +
'<input type="button" value="Cancel" onclick="$(this.form).parent().parent().remove();"/>' + '<input type="button" value="Cancel" onclick="$(this.form).parent().parent().remove();"/>' +
'</form>' + '</form>');
'</td></tr>'); form.submit(function() {
var data = $(this).serialize();
//$.post('{% url codereview.review.views.add_comment %}', data, function(comment) {
$.ajax({
'url': '{% url codereview.review.views.add_comment %}',
'type': 'POST',
'data': data,
'context': $(this),
'success': function(html) {
html = $('<tr class="annotation"></tr>').append($('<td colspan="3"></td>').append(html));
$(this).parent().parent().after(html);
$(this).parent().parent().remove();
},
});
return false;
});
$(this).after(
$('<tr class="comment-form" id="' + form_id + '" ></tr>')
.append($('<td colspan="3"></td>').append(form))
);
} }
}) })
}); });
@ -37,5 +55,9 @@
{% for diff in diffs %} {% for diff in diffs %}
{% include "components/diff.html" %} {% include "components/diff.html" %}
{% endfor %} {% endfor %}
<div id="comment-template" class="ui-helper-hidden-accessible">
{% include "components/comment.html" %}
</div>
{% endblock %} {% endblock %}