<commit_before>from datetime import datetime
import re

from django import newforms as forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.serializers import serialize
from django.db.models import Q, ManyToManyField
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.template.context import RequestContext
from django.template.loader import render_to_string
from django.utils import simplejson
from django.views.generic.list_detail import object_list
from django.views.decorators.http import require_GET, require_POST
from djblets.auth.util import login_required

from reviewboard.diffviewer.models import DiffSet, DiffSetHistory, FileDiff
from reviewboard.diffviewer.views import view_diff, view_diff_fragment
from reviewboard.diffviewer.views import UserVisibleError, get_diff_files
from reviewboard.reviews.db import \
    get_all_review_requests, get_review_requests_to_group, \
    get_review_requests_to_user_directly, get_review_requests_to_user, \
    get_review_requests_from_user
from reviewboard.reviews.models import ReviewRequest, ReviewRequestDraft, Quip
from reviewboard.reviews.models import Review, Comment, Group
from reviewboard.reviews.forms import NewReviewRequestForm
from reviewboard.reviews.email import mail_review_request, mail_review
from reviewboard import scmtools


@login_required
def new_review_request(request, template_name='reviews/review_detail.html'):
    if request.POST:
        form = NewReviewRequestForm(request.POST.copy())

        if form.is_valid():
            form.clean_data['submitter'] = request.user
            form.clean_data['status'] = 'P'
            form.clean_data['public'] = True
            new_reviewreq = form.create()

            return HttpResponseRedirect(new_reviewreq.get_absolute_url())
    else:
        form = NewReviewRequestForm(initial={'submitter': request.user})

    return render_to_response(template_name, RequestContext(request, {
        'form': form,
    }))


@login_required
def new_from_changenum(request):
    if not request.POST or 'changenum' not in request.POST:
        # XXX Display an error page
        return HttpResponseRedirect('/r/new/')

    changenum = request.POST['changenum']

    diffset_history = DiffSetHistory()
    diffset_history.save()

    review_request = ReviewRequest()
    changeset = scmtools.get_tool().get_changeset(changenum)

    if changeset:
        review_request.summary = changeset.summary
        review_request.description = changeset.description
        review_request.testing_done = changeset.testing_done
        review_request.branch = changeset.branch
        review_request.bugs_closed = ','.join(changeset.bugs_closed)
        review_request.diffset_history = diffset_history
        review_request.submitter = request.user
        review_request.status = 'P'
        review_request.public = False
        review_request.save()
<commit_msg>When creating a review request from a changenum, set the changenum in the review request.<commit_after>from datetime import datetime
import re

from django import newforms as forms
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.serializers import serialize
from django.db.models import Q, ManyToManyField
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.template.context import RequestContext
from django.template.loader import render_to_string
from django.utils import simplejson
from django.views.generic.list_detail import object_list
from django.views.decorators.http import require_GET, require_POST
from djblets.auth.util import login_required

from reviewboard.diffviewer.models import DiffSet, DiffSetHistory, FileDiff
from reviewboard.diffviewer.views import view_diff, view_diff_fragment
from reviewboard.diffviewer.views import UserVisibleError, get_diff_files
from reviewboard.reviews.db import \
    get_all_review_requests, get_review_requests_to_group, \
    get_review_requests_to_user_directly, get_review_requests_to_user, \
    get_review_requests_from_user
from reviewboard.reviews.models import ReviewRequest, ReviewRequestDraft, Quip
from reviewboard.reviews.models import Review, Comment, Group
from reviewboard.reviews.forms import NewReviewRequestForm
from reviewboard.reviews.email import mail_review_request, mail_review
from reviewboard import scmtools


@login_required
def new_review_request(request, template_name='reviews/review_detail.html'):
    if request.POST:
        form = NewReviewRequestForm(request.POST.copy())

        if form.is_valid():
            form.clean_data['submitter'] = request.user
            form.clean_data['status'] = 'P'
            form.clean_data['public'] = True
            new_reviewreq = form.create()

            return HttpResponseRedirect(new_reviewreq.get_absolute_url())
    else:
        form = NewReviewRequestForm(initial={'submitter': request.user})

    return render_to_response(template_name, RequestContext(request, {
        'form': form,
    }))


@login_required
def new_from_changenum(request):
    if not request.POST or 'changenum' not in request.POST:
        # XXX Display an error page
        return HttpResponseRedirect('/r/new/')

    changenum = request.POST['changenum']

    diffset_history = DiffSetHistory()
    diffset_history.save()

    review_request = ReviewRequest()
    changeset = scmtools.get_tool().get_changeset(changenum)

    if changeset:
        review_request.changenum = changenum
        review_request.summary = changeset.summary
        review_request.description = changeset.description
        review_request.testing_done = changeset.testing_done
        review_request.branch = changeset.branch
        review_request.bugs_closed = ','.join(changeset.bugs_closed)
        review_request.diffset_history = diffset_history
        review_request.submitter = request.user
        review_request.status = 'P'
        review_request.public = False
        review_request.save()

{'fraction_non_alphanumeric': 0.0700912145943351, 'fraction_numerical': 0.0019203072491598655, 'mean_word_length': 4.434782608695652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7439468', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1703, 'n_words': 406}