<commit_before>from collections import ChainMap

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from django.forms import CharField, ModelForm
from django.utils.translation import gettext_lazy as _

from pythonpro.core.models import User


class UserEmailForm(ModelForm):
    current_password = CharField(label=_("Password"), strip=False, required=True)

    class Meta:
        model = User
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super().clean()
        if not self.user.check_password(cleaned_data.get('current_password', '')):
            self.add_error('current_password', ValidationError('Senha Inválida', 'invalid_password'))
        return cleaned_data


class UserSignupForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('first_name', 'email', 'source')

    def __init__(self, *args, **kwargs):
        self.plain_password = User.objects.make_random_password(30)
        data = kwargs.get('data', None)
        if data is not None:
            self._set_passwords(data)
            kwargs['data'] = data
        elif args:
            query_dict = args[0]
            dct = {}
            self._set_passwords(dct)
            args = (ChainMap(query_dict, dct), *args[1:])
        super().__init__(*args, **kwargs)

    def _set_passwords(self, data):
        if 'password1' not in data and 'password2' not in data:
            data['password1'] = data['password2'] = self.plain_password

    password1 = forms.CharField(
        label=_("Password"),
        strip=False,
        required=False,
        widget=forms.PasswordInput,

    )
    password2 = forms.CharField(
        label=_("Password confirmation"),
        widget=forms.PasswordInput,
        strip=False,
        required=False,
        help_text=_("Enter the same password as before, for verification."),
    )


class LeadForm(UserSignupForm):
    class Meta:
        model = User
        fields = ('first_name', 'email')

    source = forms.CharField(widget=forms.HiddenInput())
    password1 = forms.CharField(widget=forms.HiddenInput())
    password2 = forms.CharField(widget=forms.HiddenInput())
<commit_msg>removing autofocus to avoid page to slow down when it was loaded. Closes #1922<commit_after>from collections import ChainMap

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from django.forms import CharField, ModelForm
from django.utils.translation import gettext_lazy as _

from pythonpro.core.models import User


class UserEmailForm(ModelForm):
    current_password = CharField(label=_("Password"), strip=False, required=True)

    class Meta:
        model = User
        fields = ('email',)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super().clean()
        if not self.user.check_password(cleaned_data.get('current_password', '')):
            self.add_error('current_password', ValidationError('Senha Inválida', 'invalid_password'))
        return cleaned_data


class UserSignupForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('first_name', 'email', 'source')

    def __init__(self, *args, **kwargs):
        self.plain_password = User.objects.make_random_password(30)
        data = kwargs.get('data', None)
        if data is not None:
            self._set_passwords(data)
            kwargs['data'] = data
        elif args:
            query_dict = args[0]
            dct = {}
            self._set_passwords(dct)
            args = (ChainMap(query_dict, dct), *args[1:])
        super().__init__(*args, **kwargs)

    def _set_passwords(self, data):
        if 'password1' not in data and 'password2' not in data:
            data['password1'] = data['password2'] = self.plain_password

    password1 = forms.CharField(
        label=_("Password"),
        strip=False,
        required=False,
        widget=forms.PasswordInput,

    )
    password2 = forms.CharField(
        label=_("Password confirmation"),
        widget=forms.PasswordInput,
        strip=False,
        required=False,
        help_text=_("Enter the same password as before, for verification."),
    )


class LeadForm(UserSignupForm):
    class Meta:
        model = User
        fields = ('first_name', 'email')

    source = forms.CharField(widget=forms.HiddenInput())
    password1 = forms.CharField(widget=forms.HiddenInput())
    password2 = forms.CharField(widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['email'].widget.attrs.pop("autofocus", None)

{'fraction_non_alphanumeric': 0.11652456354039789, 'fraction_numerical': 0.005684125050751117, 'mean_word_length': 2.957429718875502, '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': '3359388', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1393, 'n_words': 329}