<commit_before>'''
The Inline classes used in the editor.
'''

from django.forms import ModelForm
from django.forms.models import BaseModelFormSet, modelformset_factory
from django.utils.functional import curry
from django.contrib.admin.util import flatten_fieldsets
from django.utils.encoding import force_unicode
from metashare.repository.editor.related_mixin import RelatedAdminMixin
from metashare.repository.editor.schemamodel_mixin import SchemaModelLookup
from django.contrib.admin.options import InlineModelAdmin



class SchemaModelInline(InlineModelAdmin, RelatedAdminMixin, SchemaModelLookup):
    extra = 1
    template = 'admin/edit_inline/stacked.html'
    collapse = False

    def __init__(self, parent_model, admin_site):
        super(SchemaModelInline, self).__init__(parent_model, admin_site)
        if self.collapse:
            self.verbose_name_plural = '_{}'.format(force_unicode(self.verbose_name_plural))
        # Show m2m fields as horizontal filter widget unless they have a custom widget:
        self.filter_horizontal = self.list_m2m_fields_without_custom_widget(self.model)

    def get_fieldsets(self, request, obj=None):
        return SchemaModelLookup.get_fieldsets(self, request, obj)


    def formfield_for_dbfield(self, db_field, **kwargs):
        # ForeignKey or ManyToManyFields
        if self.is_x_to_many_relation(db_field):
            return self.formfield_for_relation(db_field, **kwargs)
        self.use_hidden_widget_for_one2one(db_field, kwargs)
        formfield = super(SchemaModelInline, self).formfield_for_dbfield(db_field, **kwargs)
        self.use_related_widget_where_appropriate(db_field, kwargs, formfield)
        return formfield

    def response_change(self, request, obj):
        if '_popup' in request.REQUEST:
            return self.edit_response_close_popup_magic(obj)
        else:
            return super(SchemaModelInline, self).response_change(request, obj)




class ReverseInlineFormSet(BaseModelFormSet):
    '''
    A formset with either a single object or a single empty
    form. Since the formset is used to render a required OneToOne
    relation, the forms must not be empty.<commit_msg>Use the code for overriding the widget also for formset components<commit_after>'''
The Inline classes used in the editor.
'''

from django.forms import ModelForm
from django.forms.models import BaseModelFormSet, modelformset_factory
from django.utils.functional import curry
from django.contrib.admin.util import flatten_fieldsets
from django.utils.encoding import force_unicode
from metashare.repository.editor.related_mixin import RelatedAdminMixin
from metashare.repository.editor.schemamodel_mixin import SchemaModelLookup
from django.contrib.admin.options import InlineModelAdmin
from metashare.repository.editor.widgets import ComboWidget



class SchemaModelInline(InlineModelAdmin, RelatedAdminMixin, SchemaModelLookup):
    extra = 1
    template = 'admin/edit_inline/stacked.html'
    collapse = False

    def __init__(self, parent_model, admin_site):
        super(SchemaModelInline, self).__init__(parent_model, admin_site)
        if self.collapse:
            self.verbose_name_plural = '_{}'.format(force_unicode(self.verbose_name_plural))
        # Show m2m fields as horizontal filter widget unless they have a custom widget:
        self.filter_horizontal = self.list_m2m_fields_without_custom_widget(self.model)

    def get_fieldsets(self, request, obj=None):
        return SchemaModelLookup.get_fieldsets(self, request, obj)


    def formfield_for_dbfield(self, db_field, **kwargs):
        # ForeignKey or ManyToManyFields
        if self.is_x_to_many_relation(db_field):
            return self.formfield_for_relation(db_field, **kwargs)
        self.use_hidden_widget_for_one2one(db_field, kwargs)
        if db_field.name == 'languageId':
            kwargs.update({'widget': ComboWidget})
        if db_field.name == 'languageName':
            kwargs.update({'widget': ComboWidget})
        formfield = super(SchemaModelInline, self).formfield_for_dbfield(db_field, **kwargs)
        self.use_related_widget_where_appropriate(db_field, kwargs, formfield)
        return formfield

    def response_change(self, request, obj):
        if '_popup' in request.REQUEST:
            return self.edit_response_close_popup_magic(obj)
        else:
            return super(SchemaModelInline, self).response_change(request, obj)




class ReverseInlineFormSet(BaseModelFormSet):
    '''
    A formset with either a single object or a single empty
    form. Since the formset is used to render a required OneToOne
    relation, the forms must not be empty.
{'fraction_non_alphanumeric': 0.0728249194414608, 'fraction_numerical': 0.0017185821697099893, 'mean_word_length': 4.1618625277161865, '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': '2123652', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1307, 'n_words': 331}