2010-12-16 03:00:13 +00:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseRedirect
|
2010-12-09 05:06:17 +00:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
|
from codereview.browser import vcs
|
|
|
|
from codereview.dashboard.models import Repository
|
2010-12-13 05:33:04 +00:00
|
|
|
from codereview.review.models import Review, Comment
|
2010-12-14 04:29:13 +00:00
|
|
|
from codereview.review.forms import NewCommitReviewForm, CommentForm
|
2010-12-09 05:06:17 +00:00
|
|
|
|
|
|
|
@permission_required('review.add_review')
|
|
|
|
def new(request):
|
|
|
|
if not request.POST:
|
|
|
|
raise Http404
|
|
|
|
form = NewCommitReviewForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
repository = Repository.objects.get(pk=form.cleaned_data['repo'])
|
|
|
|
repo = vcs.create(repository.type, repository.path)
|
|
|
|
commit = repo.commit(form.cleaned_data['ref'])
|
|
|
|
description = commit.message.split('\n')[0].strip()
|
2010-12-13 05:33:04 +00:00
|
|
|
review = Review.objects.create(
|
|
|
|
author=request.user,
|
2010-12-09 05:06:17 +00:00
|
|
|
repo=repository,
|
2010-12-13 05:33:04 +00:00
|
|
|
ref=commit.id,
|
|
|
|
parent=commit.parents[0] if commit.parents else None,
|
|
|
|
description=description)
|
2010-12-09 05:06:17 +00:00
|
|
|
return HttpResponseRedirect(reverse(edit, args=[review.pk]))
|
|
|
|
@permission_required('review.change_review')
|
|
|
|
def edit(request, review_id):
|
|
|
|
try:
|
|
|
|
review = Review.objects.get(pk=review_id)
|
|
|
|
except:
|
|
|
|
raise Http404
|
2010-12-13 05:33:04 +00:00
|
|
|
repo = vcs.create(review.repo.type, review.repo.path)
|
|
|
|
diffs = repo.diff(review.ref, review.parent if review.parent else None)
|
2010-12-09 05:06:17 +00:00
|
|
|
data = RequestContext(request, {
|
|
|
|
"review": review,
|
|
|
|
"diffs": diffs,
|
|
|
|
})
|
|
|
|
return render_to_response('review/edit.html', data)
|
2010-12-14 04:29:13 +00:00
|
|
|
@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()
|
2010-12-16 03:00:13 +00:00
|
|
|
data = RequestContext(request, {
|
|
|
|
'comment': comment,
|
|
|
|
})
|
|
|
|
return render_to_response('components/comment.html', data)
|