<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import forms
from data_exports.models import Export, Column
from inspect_model import InspectModel


class ExportForm(forms.ModelForm):

    class Meta:
        model = Export

    def __init__(self, *args, **kwargs):
        super(ExportForm, self).__init__(*args, **kwargs)
        if self.instance:  # don't allow modification of the model once created
            del self.fields['model']


class ColumnForm(forms.ModelForm):
    # make sure we have a select widget for the column choice
    # Choice list is computed in data_exports.admin.ColumnFormSet
    column = forms.ChoiceField(choices=[])

    class Meta:
        model = Column


class ColumnFormSet(forms.models.BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        super(ColumnFormSet, self).__init__(*args, **kwargs)
        model = self.instance.model.model_class()
        self.choices = [(u'', '---------')] + get_choices(model)

    def add_fields(self, form, index):
        """Filter the form's column choices

        This is done at the formset level as there's no other way i could find
        to get the parent object (stored in self.instance), and the form at the
        same time.

        """
        super(ColumnFormSet, self).add_fields(form, index)
        form.fields['column'].choices = self.choices


def get_choices(model, prefixes=[]):
    choices = []
    prefix = '.'.join(prefixes)
    if prefix:
        prefix = '%s.' % prefix<commit_msg>get performance fix for dj1.6 to work on dj<1.6<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import forms
from data_exports.models import Export, Column
from inspect_model import InspectModel


class ExportForm(forms.ModelForm):

    class Meta:
        model = Export

    def __init__(self, *args, **kwargs):
        super(ExportForm, self).__init__(*args, **kwargs)
        if self.instance:  # don't allow modification of the model once created
            del self.fields['model']


class ColumnForm(forms.ModelForm):
    # make sure we have a select widget for the column choice
    # Choice list is computed in data_exports.admin.ColumnFormSet
    column = forms.ChoiceField(choices=[])

    class Meta:
        model = Column


class ColumnFormSet(forms.models.BaseInlineFormSet):
    def get_choices(self):
        # avoid multiple choices generation for django 1.6+
        if not hasattr(self, "_choices"):
            model = self.instance.model.model_class()
            self._choices = [(u'', '---------')] + get_choices(model)
        return self._choices

    def add_fields(self, form, index):
        """Filter the form's column choices

        This is done at the formset level as there's no other way i could find
        to get the parent object (stored in self.instance), and the form at the
        same time.

        """
        super(ColumnFormSet, self).add_fields(form, index)
        form.fields['column'].choices = self.get_choices()


def get_choices(model, prefixes=[]):
    choices = []
    prefix = '.'.join(prefixes)
    if prefix:
        prefix = '%s.' % prefix
{'fraction_non_alphanumeric': 0.11312360978709883, 'fraction_numerical': 0.002542103590721322, 'mean_word_length': 2.8250303766707168, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3702941', 'n_tokens_mistral': 928, 'n_tokens_neox': 895, 'n_words': 287}