<commit_before>"""
Interface to interacting with OCR presets.
"""

import os
import glob
import json

from django import forms
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ValidationError

from ocradmin.core import generic_views as gv
from ocradmin.core.decorators import saves_files
from ocradmin.ocrtasks.models import OcrTask
from ocradmin.plugins import graph, cache
from ocradmin.plugins import utils as pluginutils
from ocradmin.presets.models import Preset, Profile

from nodetree import script, node, registry
from nodetree import utils as nodeutils

from ocradmin.plugins import nodes



class PresetForm(forms.ModelForm):
    """
        Base preset form
    """
    def __init__(self, *args, **kwargs):
        super(PresetForm, self).__init__(*args, **kwargs)
        # change a widget attribute:
        self.fields['description'].widget.attrs["rows"] = 2
        self.fields['description'].widget.attrs["cols"] = 40

    def clean_data(self):
        try:
            data = json.loads(self.cleaned_data["data"])
        except ValueError:
            raise ValidationError("Preset data must be valid JSON")
        profile = self.cleaned_data["profile"]
        if profile is not None:
            errors = profile.validate_preset(data)
            if errors:
                raise ValidationError("Data does not fit preset profile: %s" % errors)
        return self.cleaned_data["data"]

    class Meta:
        model = Preset
        fields = ["name", "tags", "description", "public", "profile", "user", "data"]
        exclude = ["created_on", "updated_on"]
        widgets = dict(
                user=forms.HiddenInput()
        )


presetlist = gv.GenericListView.as_view(
        model=Preset,
        page_name="OCR Presets",
        fields=["name", "description", "user", "created_on"],)


presetcreate = gv.GenericCreateView.as_view(
        model=Preset,
        form_class=PresetForm,
        page_name="New OCR Preset",)


presetdetail = gv.GenericDetailView.as_view(
        model=Preset,
        page_name="OCR Preset",
        fields=["name", "description", "user", "public", "profile", "tags", "created_on",
            "updated_on",])


presetedit = gv.GenericEditView.as_view(
        model=Preset,
        form_class=PresetForm,
        page_name="Edit OCR Preset",)


presetdelete = gv.GenericDeleteView.as_view(
        model=Preset,
        page_name="Delete OCR Preset",
        success_url="/presets/list/",)


def createjson(request):
    """Create a preset and return JSON data"""
    form = PresetForm(request.POST)
    if not form.is_valid():
        return HttpResponse(json.dumps(form.errors),
                mimetype="application/json")
    form.save()
    return HttpResponse(json.dumps(form.instance.slug),
            status=201, mimetype="application/json")


def data(request, slug):
    """Return the data for a given preset in JSON format"""
    preset = get_object_or_404(Preset, slug=slug)
    return HttpResponse(preset.data, mimetype="application/json")


def update_data(request, slug):
    """Update script data for a given script."""
    preset = get_object_or_404(Preset, slug=slug)
    scriptdata = request.POST.get("data", "")
    # TODO: Validate script data
    preset.data = scriptdata
    preset.save()
    return HttpResponse(preset.data, mimetype="application/json")


def download(request, slug):
    """Return the data for a preset as an attachment"""
    preset = get_object_or_404(Preset, slug=slug)
    response = HttpResponse(preset.data, mimetype="application/json")
    response['Content-Disposition'] = "attachment; filename=%s.json" % slug
    return response


def fetch(request):
    """Hacky method of forcing downloading of an in-progress script via JS"""
    slug = request.POST.get("name", "untitled")
    script = request.POST.get("script")
    response = HttpResponse(script, mimetype="application/json")
    response['Content-Disposition'] = "attachment; filename=%s.json" % slug
    return response


@saves_files
def builder(request):
    """
    Show the preset builder.
    """
    print request.session
    context = dict(
            form=PresetForm(initial=dict(user=request.user)),
            presets=Preset.objects.order_by("name"),
            profiles=Preset.objects.order_by("name"),<commit_msg>Remove debug<commit_after>"""
Interface to interacting with OCR presets.
"""

import os
import glob
import json

from django import forms
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ValidationError

from ocradmin.core import generic_views as gv
from ocradmin.core.decorators import saves_files
from ocradmin.ocrtasks.models import OcrTask
from ocradmin.plugins import graph, cache
from ocradmin.plugins import utils as pluginutils
from ocradmin.presets.models import Preset, Profile

from nodetree import script, node, registry
from nodetree import utils as nodeutils

from ocradmin.plugins import nodes



class PresetForm(forms.ModelForm):
    """
        Base preset form
    """
    def __init__(self, *args, **kwargs):
        super(PresetForm, self).__init__(*args, **kwargs)
        # change a widget attribute:
        self.fields['description'].widget.attrs["rows"] = 2
        self.fields['description'].widget.attrs["cols"] = 40

    def clean_data(self):
        try:
            data = json.loads(self.cleaned_data["data"])
        except ValueError:
            raise ValidationError("Preset data must be valid JSON")
        profile = self.cleaned_data["profile"]
        if profile is not None:
            errors = profile.validate_preset(data)
            if errors:
                raise ValidationError("Data does not fit preset profile: %s" % errors)
        return self.cleaned_data["data"]

    class Meta:
        model = Preset
        fields = ["name", "tags", "description", "public", "profile", "user", "data"]
        exclude = ["created_on", "updated_on"]
        widgets = dict(
                user=forms.HiddenInput()
        )


presetlist = gv.GenericListView.as_view(
        model=Preset,
        page_name="OCR Presets",
        fields=["name", "description", "user", "created_on"],)


presetcreate = gv.GenericCreateView.as_view(
        model=Preset,
        form_class=PresetForm,
        page_name="New OCR Preset",)


presetdetail = gv.GenericDetailView.as_view(
        model=Preset,
        page_name="OCR Preset",
        fields=["name", "description", "user", "public", "profile", "tags", "created_on",
            "updated_on",])


presetedit = gv.GenericEditView.as_view(
        model=Preset,
        form_class=PresetForm,
        page_name="Edit OCR Preset",)


presetdelete = gv.GenericDeleteView.as_view(
        model=Preset,
        page_name="Delete OCR Preset",
        success_url="/presets/list/",)


def createjson(request):
    """Create a preset and return JSON data"""
    form = PresetForm(request.POST)
    if not form.is_valid():
        return HttpResponse(json.dumps(form.errors),
                mimetype="application/json")
    form.save()
    return HttpResponse(json.dumps(form.instance.slug),
            status=201, mimetype="application/json")


def data(request, slug):
    """Return the data for a given preset in JSON format"""
    preset = get_object_or_404(Preset, slug=slug)
    return HttpResponse(preset.data, mimetype="application/json")


def update_data(request, slug):
    """Update script data for a given script."""
    preset = get_object_or_404(Preset, slug=slug)
    scriptdata = request.POST.get("data", "")
    # TODO: Validate script data
    preset.data = scriptdata
    preset.save()
    return HttpResponse(preset.data, mimetype="application/json")


def download(request, slug):
    """Return the data for a preset as an attachment"""
    preset = get_object_or_404(Preset, slug=slug)
    response = HttpResponse(preset.data, mimetype="application/json")
    response['Content-Disposition'] = "attachment; filename=%s.json" % slug
    return response


def fetch(request):
    """Hacky method of forcing downloading of an in-progress script via JS"""
    slug = request.POST.get("name", "untitled")
    script = request.POST.get("script")
    response = HttpResponse(script, mimetype="application/json")
    response['Content-Disposition'] = "attachment; filename=%s.json" % slug
    return response


@saves_files
def builder(request):
    """
    Show the preset builder.
    """
    context = dict(
            form=PresetForm(initial=dict(user=request.user)),
            presets=Preset.objects.order_by("name"),
            profiles=Preset.objects.order_by("name"),
{'fraction_non_alphanumeric': 0.1151267189453347, 'fraction_numerical': 0.004091374019774974, 'mean_word_length': 3.2947779404587605, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2623505', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2571, 'n_words': 654}