<commit_before>from django.views.generic import FormView as DjangoFormView
from django_cradmin import javascriptregistry

from . import formviewmixin


class WithinRoleFormView(javascriptregistry.viewmixin.WithinRoleViewMixin,
                         formviewmixin.FormViewMixin,
                         DjangoFormView):
    """
    Form view.

    Uses :class:`django.views.generic.edit.FormView` with
    :class:`django_cradmin.viewhelpers.formview.formviewmixin.FormViewMixin`
    and :class:`django_cradmin.javascriptregistry.viewmixin.WithinRoleViewMixin`.

    Examples:

        Minimalistic example::

            from django import forms
            from django.http import HttpResponseRedirect
            from django_cradmin.viewhelpers import formview

            class MyForm(forms.Form):
                first_name = forms.CharField(max_length=50)
                last_name = forms.CharField(max_length=50)

            class MyFormView(formview.WithinRoleFormView):
                template_name = 'myapp/myview.django.html'
                form_class = MyForm

                def get_form_renderable(self):
                    return uicontainer.layout.AdminuiPageSectionTight(
                        children=[
                            uicontainer.form.Form(
                                form=self.get_form(),
                                children=[
                                    uicontainer.fieldwrapper.FieldWrapper('first_name'),
                                    uicontainer.fieldwrapper.FieldWrapper('last_name'),
                                    uicontainer.button.SubmitPrimary(text='Save')
                                ]
                            )
                        ]
                    ).bootstrap()

                def form_valid(self, form):
                    # ... do something with the form ...
                    return HttpResponseRedirect('/some/view')
    """<commit_msg>formview: Update example with autofocus.<commit_after>from django.views.generic import FormView as DjangoFormView
from django_cradmin import javascriptregistry

from . import formviewmixin


class WithinRoleFormView(javascriptregistry.viewmixin.WithinRoleViewMixin,
                         formviewmixin.FormViewMixin,
                         DjangoFormView):
    """
    Form view.

    Uses :class:`django.views.generic.edit.FormView` with
    :class:`django_cradmin.viewhelpers.formview.formviewmixin.FormViewMixin`
    and :class:`django_cradmin.javascriptregistry.viewmixin.WithinRoleViewMixin`.

    Examples:

        Minimalistic example::

            from django import forms
            from django.http import HttpResponseRedirect
            from django_cradmin.viewhelpers import formview

            class MyForm(forms.Form):
                first_name = forms.CharField(max_length=50)
                last_name = forms.CharField(max_length=50)

            class MyFormView(formview.WithinRoleFormView):
                template_name = 'myapp/myview.django.html'
                form_class = MyForm

                def get_form_renderable(self):
                    return uicontainer.layout.AdminuiPageSectionTight(
                        children=[
                            uicontainer.form.Form(
                                form=self.get_form(),
                                children=[
                                    uicontainer.fieldwrapper.FieldWrapper(
                                        fieldname='first_name',
                                        # Override field renderable to set autofocus
                                        field_renderable=uicontainer.field.Field(autofocus=True)
                                    ),
                                    uicontainer.fieldwrapper.FieldWrapper('last_name'),
                                    uicontainer.button.SubmitPrimary(text='Save')
                                ]
                            )
                        ]
                    ).bootstrap()

                def form_valid(self, form):
                    # ... do something with the form ...
                    return HttpResponseRedirect('/some/view')
    """
{'fraction_non_alphanumeric': 0.06846374045801527, 'fraction_numerical': 0.0019083969465648854, 'mean_word_length': 1.4321345707656612, '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': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '126650', 'n_tokens_mistral': 1015, 'n_tokens_neox': 949, 'n_words': 160}