<commit_before>from functools import partial

from django import forms
from django.utils.datastructures import SortedDict

from websettings import websettings


def get_settingstore_fields():
    fields = [(k, forms.CharField(max_length=255,
                                  label=k,
                                  initial=partial(getattr, websettings, k)))
              for k in websettings.settings.keys()]
    return SortedDict(fields)


class SettingStoreFieldsMetaclass(type):
    def __new__(cls, *args, **kwargs):
        super_new = super(SettingStoreFieldsMetaclass, cls).__new__
        new_class = super_new(cls, *args, **kwargs)

        new_class.base_fields = get_settingstore_fields()
        return new_class


class BaseSettingStoreForm(forms.BaseForm):
    def save(self):
        if hasattr(self, 'cleaned_data'):
            for k, v in self.cleaned_data.items():
                setattr(websettings, k, v)
        else:
            # TODO: need more humanization.
            raise ValueError('Form needs cleaned_data')


class SettingStoreForm(BaseSettingStoreForm):
    __metaclass__ = SettingStoreFieldsMetaclass<commit_msg>Fixed SettingStoreForm for more testable.<commit_after>from functools import partial

from django import forms
from django.utils.datastructures import SortedDict

from websettings import websettings


def get_settingstore_fields(setting_store):
    fields = [(k, forms.CharField(max_length=255,
                                  label=k,
                                  initial=partial(getattr, setting_store, k)))
              for k in setting_store.settings.keys()]
    return SortedDict(fields)


class SettingStoreFieldsMetaclass(type):
    def __new__(cls, name, bases, attrs):
        super_new = super(SettingStoreFieldsMetaclass, cls).__new__
        new_class = super_new(cls, name, bases, attrs)

        setting_store = attrs.get('setting_store', websettings)
        new_class.base_fields = get_settingstore_fields(setting_store)
        return new_class


class BaseSettingStoreForm(forms.BaseForm):
    def save(self):
        if hasattr(self, 'cleaned_data'):
            for k, v in self.cleaned_data.items():
                setattr(self.setting_store, k, v)
        else:
            # TODO: need more humanization.
            raise ValueError('Form needs cleaned_data')


class SettingStoreForm(BaseSettingStoreForm):
    __metaclass__ = SettingStoreFieldsMetaclass

    # For more testable
    setting_store = websettings
{'fraction_non_alphanumeric': 0.0788630904723779, 'fraction_numerical': 0.0024019215372297837, 'mean_word_length': 2.8564814814814814, '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': '1317211', 'n_tokens_mistral': 702, 'n_tokens_neox': 685, 'n_words': 165}