codereview/review/views.py

40 lines
1.6 KiB
Python
Executable file

from django.http import Http404, HttpResponseRedirect
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
from codereview.review.models import Review, Comment
from codereview.review.forms import NewCommitReviewForm
@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()
review = Review.objects.create(
author=request.user,
repo=repository,
ref=commit.id,
parent=commit.parents[0] if commit.parents else None,
description=description)
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
repo = vcs.create(review.repo.type, review.repo.path)
diffs = repo.diff(review.ref, review.parent if review.parent else None)
data = RequestContext(request, {
"review": review,
"diffs": diffs,
})
return render_to_response('review/edit.html', data)