<commit_before>from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.forms.models import ModelFormMetaclass
from django.contrib.auth.forms import AuthenticationForm as DjangoAuthenticationForm
from django.contrib.auth import authenticate

from enroll.models import VerificationToken
from enroll.validators import UniqueUsernameValidator, UniqueEmailValidator
from enroll import import_class
from enroll.signals import post_registration
from django.forms.forms import DeclarativeFieldsMetaclass

DEFAULT_FORM_VALIDATORS = {
    'username': [ UniqueUsernameValidator ],
    'email': [ UniqueEmailValidator ],
}


def add_validators_to_class_fields(new_class):
    for field, validators in new_class.field_validators.iteritems():
        if field not in new_class.base_fields:
            #field is not present on form - TODO logger.warning
            continue
        for validator in validators:
            if isinstance(validator, basestring):
                validator = import_class(validator)()
            elif isinstance(validator, type):
                validator = validator()
            new_class.base_fields[field].validators.append(validator)


class ExplicitValidationModelFormMetaclass(ModelFormMetaclass):
    """Adds explicit declared field validators to class fields"""<commit_msg>prevent multiple validator assign<commit_after>from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.forms.models import ModelFormMetaclass
from django.contrib.auth.forms import AuthenticationForm as DjangoAuthenticationForm
from django.contrib.auth import authenticate

from enroll.models import VerificationToken
from enroll.validators import UniqueUsernameValidator, UniqueEmailValidator
from enroll import import_class
from enroll.signals import post_registration
from django.forms.forms import DeclarativeFieldsMetaclass

DEFAULT_FORM_VALIDATORS = {
    'username': [ UniqueUsernameValidator ],
    'email': [ UniqueEmailValidator ],
}


def add_validators_to_class_fields(new_class):
    for field, validators in new_class.field_validators.iteritems():
        if field not in new_class.base_fields:
            continue
        for validator in validators:
            if isinstance(validator, basestring):
                validator = import_class(validator)()
            elif isinstance(validator, type):
                validator = validator()
            field_instance = new_class.base_fields[field]
            if not hasattr(field_instance, '_enroll_validators_initialized'):
                field_instance.validators.append(validator)
                field_instance._enroll_validators_initialized = True


class ExplicitValidationModelFormMetaclass(ModelFormMetaclass):
    """Adds explicit declared field validators to class fields"""
{'fraction_non_alphanumeric': 0.05836967460583697, 'fraction_numerical': 0.0, 'mean_word_length': 4.195121951219512, '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': '5167417', 'n_tokens_mistral': 756, 'n_tokens_neox': 733, 'n_words': 208}