Added line commenting in reviews (not yet ajax)

This commit is contained in:
Correl Roush 2010-12-13 23:29:13 -05:00
parent fe601d1f22
commit 95ff757050
7 changed files with 61 additions and 3 deletions

View file

@ -103,3 +103,9 @@ table.diff td {
table.diff tr.annotation td { table.diff tr.annotation td {
white-space: normal; white-space: normal;
} }
.comment-form textarea {
display: block;
width: 90%;
height: 3em;
}

View file

@ -5,3 +5,7 @@ class NewCommitReviewForm(forms.Form):
author = forms.IntegerField(widget=forms.HiddenInput) author = forms.IntegerField(widget=forms.HiddenInput)
repo = forms.IntegerField(widget=forms.HiddenInput) repo = forms.IntegerField(widget=forms.HiddenInput)
ref = forms.CharField(widget=forms.HiddenInput) ref = forms.CharField(widget=forms.HiddenInput)
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('review', 'line_a', 'line_b', 'text')

View file

@ -40,6 +40,6 @@ class Comment(models.Model):
author = models.ForeignKey(User) author = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
path = models.TextField() path = models.TextField()
line_a = models.IntegerField() line_a = models.IntegerField(null=True)
line_b = models.IntegerField(null=True) line_b = models.IntegerField(null=True)
text = models.TextField() text = models.TextField()

View file

@ -3,4 +3,5 @@ from django.conf.urls.defaults import *
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^(?P<review_id>\d+)/$', 'codereview.review.views.edit'), (r'^(?P<review_id>\d+)/$', 'codereview.review.views.edit'),
(r'^new/$', 'codereview.review.views.new'), (r'^new/$', 'codereview.review.views.new'),
(r'^comment/new/$', 'codereview.review.views.add_comment'),
) )

View file

@ -6,7 +6,7 @@ from django.contrib.auth.decorators import permission_required
from codereview.browser import vcs from codereview.browser import vcs
from codereview.dashboard.models import Repository from codereview.dashboard.models import Repository
from codereview.review.models import Review, Comment from codereview.review.models import Review, Comment
from codereview.review.forms import NewCommitReviewForm from codereview.review.forms import NewCommitReviewForm, CommentForm
@permission_required('review.add_review') @permission_required('review.add_review')
def new(request): def new(request):
@ -38,3 +38,21 @@ def edit(request, review_id):
"diffs": diffs, "diffs": diffs,
}) })
return render_to_response('review/edit.html', data) return render_to_response('review/edit.html', data)
@permission_required('review.add_comment')
def add_comment(request):
review_id = request.POST.get('review')
try:
review = Review.objects.get(pk=review_id)
path = int(request.POST.get('path'))
except:
raise Http404
repo = vcs.create(review.repo.type, review.repo.path)
diffs = repo.diff(review.ref, review.parent if review.parent else None)
form = CommentForm(request.POST)
comment = form.save(commit=False)
diff = diffs[path]
comment.author = request.user
comment.path = diff.b.path if diff.b else diff.a.path
comment.save()
return HttpResponseRedirect(reverse(edit, args=[review.pk]))

View file

@ -12,7 +12,8 @@
<td colspan="3">{{ change.text }}</td> <td colspan="3">{{ change.text }}</td>
</tr> </tr>
{% else %} {% else %}
<tr class="{% if change.type == '+' %}add{% endif %}{% if change.type == '-' %}del{% endif %}"> <tr id="{{ review.pk }}_{{ forloop.parentloop.counter0 }}_{% if change.type != '+' %}{{ change.line_a }}{% else %}0{% endif %}_{% if change.type != '-' %}{{ change.line_b }}{% else %}0{% endif %}"
class="{% if change.type == '+' %}add{% endif %}{% if change.type == '-' %}del{% endif %}">
<td class="number">{% if change.type != '+' %}{{ change.line_a }}{% endif %}</td> <td class="number">{% if change.type != '+' %}{{ change.line_a }}{% endif %}</td>
<td class="number">{% if change.type != '-' %}{{ change.line_b }}{% endif %}</td> <td class="number">{% if change.type != '-' %}{{ change.line_b }}{% endif %}</td>
<td class="text">{{ change.text }}</td> <td class="text">{{ change.text }}</td>

View file

@ -4,6 +4,34 @@
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<script type="text/javascript">
$(document).ready(function() {
$('.diff tr:has(.number)').click(function(e) {
// Do stuff
var form_id = this.id + '_form';
var info = this.id.split('_');
var path = info[1];
var line_a = info[2];
var line_b = info[3];
if ($('#' + form_id).length == 0) {
// Show the comment form
$(this).after(
'<tr class="comment-form" id="' + form_id + '" ><td colspan="3">' +
'<form method="post" action="{% url codereview.review.views.add_comment %}">' +
"{% csrf_token %}" +
'<input type="hidden" name="review" value="{{ review.pk }}" />' +
'<input type="hidden" name="path" value="' + path + '" />' +
'<input type="hidden" name="line_a" value="' + line_a + '" />' +
'<input type="hidden" name="line_b" value="' + line_b + '" />' +
'<textarea name="text" />' +
'<input type="submit" value="Add Comment" />' +
'<input type="button" value="Cancel" onclick="$(this.form).parent().parent().remove();"/>' +
'</form>' +
'</td></tr>');
}
})
});
</script>
<h2>#{{ review.pk }} - <span id="review-description">{{ review.description }}</span></h2> <h2>#{{ review.pk }} - <span id="review-description">{{ review.description }}</span></h2>
{% for diff in diffs %} {% for diff in diffs %}