<commit_before>from django.forms.models import ModelFormMetaclass, BaseModelForm
from django import forms
from django.utils import six
from versions.models import Versionable
from django.utils.translation import ugettext_lazy as _


class VersionedBaseModelForm(BaseModelForm):
    """
    This is a helperclass to construct VersionedModelForm
    """
    def save(self, commit=True):
        if self.instance.pk is not None and isinstance(self.instance, Versionable):
            if self.has_changed():
                self.instance = self.instance.clone()
        return super().save(commit)


class VersionedModelForm(six.with_metaclass(ModelFormMetaclass, VersionedBaseModelForm)):
    """
    This is a modified version of Django's ModelForm which differs from ModelForm in
    only one way: It executes the .clone() method of an object before saving it back to
    the database, if the model is a sub-class of versions.models.Versionable. You can
    safely use this as a base class for all your model forms, it will work out correctly
    with both versioned and non-versioned models.
    """
    pass


class SettingsForm(forms.Form):
    """
    This form is meant to be used for modifying Event- or OrganizerSettings
    """
    BOOL_CHOICES = (
        ('True', _('enabled')),
        ('False', _('disabled')),
    )

    def __init__(self, *args, **kwargs):
        self.obj = kwargs.pop('obj')
        if 'initial' not in kwargs:
            kwargs['initial'] = self.obj.settings
        super().__init__(*args, **kwargs)

    def save(self):
        for name, field in self.fields.items():
            value = self.cleaned_data[name]
            if value is None:
                del self.obj.settings[name]
            elif self.obj.settings.get(value, as_type=type(value)) != value:
                self.obj.settings.set(name, value)<commit_msg>Fix a bug in SettingsForm<commit_after>from django.forms.models import ModelFormMetaclass, BaseModelForm
from django import forms
from django.utils import six
from versions.models import Versionable
from django.utils.translation import ugettext_lazy as _


class VersionedBaseModelForm(BaseModelForm):
    """
    This is a helperclass to construct VersionedModelForm
    """
    def save(self, commit=True):
        if self.instance.pk is not None and isinstance(self.instance, Versionable):
            if self.has_changed():
                self.instance = self.instance.clone()
        return super().save(commit)


class VersionedModelForm(six.with_metaclass(ModelFormMetaclass, VersionedBaseModelForm)):
    """
    This is a modified version of Django's ModelForm which differs from ModelForm in
    only one way: It executes the .clone() method of an object before saving it back to
    the database, if the model is a sub-class of versions.models.Versionable. You can
    safely use this as a base class for all your model forms, it will work out correctly
    with both versioned and non-versioned models.
    """
    pass


class SettingsForm(forms.Form):
    """
    This form is meant to be used for modifying Event- or OrganizerSettings
    """
    BOOL_CHOICES = (
        ('True', _('enabled')),
        ('False', _('disabled')),
    )

    def __init__(self, *args, **kwargs):
        self.obj = kwargs.pop('obj')
        if 'initial' not in kwargs:
            kwargs['initial'] = self.obj.settings
        super().__init__(*args, **kwargs)

    def save(self):
        for name, field in self.fields.items():
            value = self.cleaned_data[name]
            if value is None:
                del self.obj.settings[name]
            elif self.obj.settings.get(name, as_type=type(value)) != value:
                self.obj.settings.set(name, value)
{'fraction_non_alphanumeric': 0.09429413340476828, 'fraction_numerical': 0.0, 'mean_word_length': 2.8614270941054807, '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': '6579330', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1012, 'n_words': 345}