<commit_before>import os

from django.shortcuts import render, get_object_or_404, render_to_response
from django.http import StreamingHttpResponse, HttpResponseNotFound
from django.contrib.auth.decorators import permission_required
from django.core.servers.basehttp import FileWrapper
from django.core.urlresolvers import reverse, reverse_lazy
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.views.generic import View
from django.utils.decorators import method_decorator

from excerptexport.models import ExtractionOrder, Excerpt, OutputFile, BoundingGeometry
from excerptexport.models.extraction_order import ExtractionOrderState
from excerptexport import settings
from excerptexport.services.data_conversion_service import trigger_data_conversion
from excerptexport.forms import ExportOptionsForm, NewExcerptForm


def has_excerptexport_all_permissions():
    excerpt_export_permissions = [
        'excerptexport.add_boundinggeometry',
        'excerptexport.change_boundinggeometry',
        'excerptexport.add_excerpt',
        'excerptexport.change_excerpt',
        'excerptexport.add_extractionorder',
        'excerptexport.change_extractionorder',
    ]
    # This is a simple hack, instead of displaying a login page, it shows a permission denied page if the user is
    # logged in
    login_url = reverse_lazy('excerptexport:access_denied')
    return permission_required(excerpt_export_permissions, login_url=login_url, raise_exception=False)


class NewExtractionOrderView(View):
    @method_decorator(login_required)
    @method_decorator(has_excerptexport_all_permissions())
    def get(self, request, excerpt_form_initial_data=None):
        view_model = {
            'user': request.user,
            'export_options_form': ExportOptionsForm(auto_id='%s'),
            'new_excerpt_form': NewExcerptForm(auto_id='%s', initial=excerpt_form_initial_data),
            'personal_excerpts': Excerpt.objects.filter(is_active=True, is_public=False, owner=request.user),
            'public_excerpts': Excerpt.objects.filter(is_active=True, is_public=True),
            'administrative_areas': settings.ADMINISTRATIVE_AREAS
        }
        return render(request, 'excerptexport/templates/new_excerpt_export.html', view_model)


    @method_decorator(login_required)
    @method_decorator(has_excerptexport_all_permissions())
    def post(self, request):
        view_context = {}
        if request.POST['form-mode'] == 'existing_excerpt':
            existing_excerpt_id = request.POST['existing_excerpt.id']
            view_context = {'excerpt': existing_excerpt_id}
            extraction_order = ExtractionOrder.objects.create(
                excerpt_id=existing_excerpt_id,
                orderer=request.user
            )

        if request.POST['form-mode'] == 'create_new_excerpt':
            new_excerpt_form = NewExcerptForm(request.POST)
            if new_excerpt_form.is_valid():
                form_data = new_excerpt_form.cleaned_data
                bounding_geometry = BoundingGeometry.create_from_bounding_box_coordinates(
                    form_data['new_excerpt_bounding_box_north'],
                    form_data['new_excerpt_bounding_box_east'],
                    form_data['new_excerpt_bounding_box_south'],
                    form_data['new_excerpt_bounding_box_west']
                )
                bounding_geometry.save()

                excerpt = Excerpt.objects.create(
                    name=form_data['new_excerpt_name'],
                    is_active=True,
                    is_public=form_data['new_excerpt_is_public'] if ('new_excerpt_is_public' in form_data) else False,
                    bounding_geometry=bounding_geometry,
                    owner=request.user
                )

                extraction_order = ExtractionOrder.objects.create(
                    excerpt=excerpt,
                    orderer=request.user<commit_msg>rely on django forms to abstract common browser behavior<commit_after>import os

from django.shortcuts import render, get_object_or_404, render_to_response
from django.http import StreamingHttpResponse, HttpResponseNotFound
from django.contrib.auth.decorators import permission_required
from django.core.servers.basehttp import FileWrapper
from django.core.urlresolvers import reverse, reverse_lazy
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.views.generic import View
from django.utils.decorators import method_decorator

from excerptexport.models import ExtractionOrder, Excerpt, OutputFile, BoundingGeometry
from excerptexport.models.extraction_order import ExtractionOrderState
from excerptexport import settings
from excerptexport.services.data_conversion_service import trigger_data_conversion
from excerptexport.forms import ExportOptionsForm, NewExcerptForm


def has_excerptexport_all_permissions():
    excerpt_export_permissions = [
        'excerptexport.add_boundinggeometry',
        'excerptexport.change_boundinggeometry',
        'excerptexport.add_excerpt',
        'excerptexport.change_excerpt',
        'excerptexport.add_extractionorder',
        'excerptexport.change_extractionorder',
    ]
    # This is a simple hack, instead of displaying a login page, it shows a permission denied page if the user is
    # logged in
    login_url = reverse_lazy('excerptexport:access_denied')
    return permission_required(excerpt_export_permissions, login_url=login_url, raise_exception=False)


class NewExtractionOrderView(View):
    @method_decorator(login_required)
    @method_decorator(has_excerptexport_all_permissions())
    def get(self, request, excerpt_form_initial_data=None):
        view_model = {
            'user': request.user,
            'export_options_form': ExportOptionsForm(auto_id='%s'),
            'new_excerpt_form': NewExcerptForm(auto_id='%s', initial=excerpt_form_initial_data),
            'personal_excerpts': Excerpt.objects.filter(is_active=True, is_public=False, owner=request.user),
            'public_excerpts': Excerpt.objects.filter(is_active=True, is_public=True),
            'administrative_areas': settings.ADMINISTRATIVE_AREAS
        }
        return render(request, 'excerptexport/templates/new_excerpt_export.html', view_model)


    @method_decorator(login_required)
    @method_decorator(has_excerptexport_all_permissions())
    def post(self, request):
        view_context = {}
        if request.POST['form-mode'] == 'existing_excerpt':
            existing_excerpt_id = request.POST['existing_excerpt.id']
            view_context = {'excerpt': existing_excerpt_id}
            extraction_order = ExtractionOrder.objects.create(
                excerpt_id=existing_excerpt_id,
                orderer=request.user
            )

        if request.POST['form-mode'] == 'create_new_excerpt':
            new_excerpt_form = NewExcerptForm(request.POST)
            if new_excerpt_form.is_valid():
                form_data = new_excerpt_form.cleaned_data
                bounding_geometry = BoundingGeometry.create_from_bounding_box_coordinates(
                    form_data['new_excerpt_bounding_box_north'],
                    form_data['new_excerpt_bounding_box_east'],
                    form_data['new_excerpt_bounding_box_south'],
                    form_data['new_excerpt_bounding_box_west']
                )
                bounding_geometry.save()

                excerpt = Excerpt.objects.create(
                    name=form_data['new_excerpt_name'],
                    is_active=True,
                    is_public=form_data['new_excerpt_is_public'],
                    bounding_geometry=bounding_geometry,
                    owner=request.user
                )

                extraction_order = ExtractionOrder.objects.create(
                    excerpt=excerpt,
                    orderer=request.user
{'fraction_non_alphanumeric': 0.07401812688821752, 'fraction_numerical': 0.0007552870090634441, 'mean_word_length': 3.264626945786366, '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': '7195087', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2153, 'n_words': 369}