<commit_before>from django import forms
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _, ugettext
from satchmo_store.accounts.mail import send_welcome_email
from livesettings import config_value
from satchmo_store.contact.forms import ContactInfoForm
from satchmo_store.contact.models import AddressBook, PhoneNumber, Contact
from l10n.models import Country
from satchmo_utils.unique_id import generate_id

import logging
import signals

log = logging.getLogger('accounts.forms')

class EmailAuthenticationForm(AuthenticationForm):
    """Authentication form with a longer username field."""
    
    def __init__(self, *args, **kwargs):
        
        super(EmailAuthenticationForm, self).__init__(*args, **kwargs)
        username = self.fields['username']
        username.max_length = 75
        username.widget.attrs['maxlength'] = 75

class RegistrationForm(forms.Form):
    """The basic account registration form."""
    title = forms.CharField(max_length=30, label=_('Title'), required=False)
    email = forms.EmailField(label=_('Email address'),
        max_length=75, required=True)
    password2 = forms.CharField(label=_('Password (again)'),
        max_length=30, widget=forms.PasswordInput(), required=True)
    password1 = forms.CharField(label=_('Password'),
        max_length=30, widget=forms.PasswordInput(), required=True)
    first_name = forms.CharField(label=_('First name'),
        max_length=30, required=True)
    last_name = forms.CharField(label=_('Last name'),
        max_length=30, required=True)

    def __init__(self, *args, **kwargs):
        self.contact = None
        super(RegistrationForm, self).__init__(*args, **kwargs)

    newsletter = forms.BooleanField(label=_('Newsletter'),
        widget=forms.CheckboxInput(), required=False)

    def clean_password1(self):
        """Enforce that password and password2 are the same."""
        p1 = self.cleaned_data.get('password1')
        p2 = self.cleaned_data.get('password2')
        if not (p1 and p2 and p1 == p2):
            raise forms.ValidationError(
                ugettext("The two passwords do not match."))

        # note, here is where we'd put some kind of custom
        # validator to enforce "hard" passwords.
        return p1

    def clean_email(self):
        """Prevent account hijacking by disallowing duplicate emails."""
        email = self.cleaned_data.get('email', None)
        if email and User.objects.filter(email=email).count() > 0:
            raise forms.ValidationError(
                ugettext("That email address is already in use."))

        return email

    def save(self, request=None, **kwargs):
        """Create the contact and user described on the form.  Returns the
        `contact`.
        """
        if self.contact:
            log.debug('skipping save, already done')
        else:
            self.save_contact(request)
        return self.contact

    def save_contact(self, request):
        log.debug("Saving contact")
        data = self.cleaned_data
        password = data['<PASSWORD>']
        email = data['email']
        first_name = data['first_name']
        last_name = data['last_name']
        username = generate_id(first_name, last_name, email)
<commit_msg>* fix: do case-insensitive email check to avoid duplicate account mess with people using CapsLock<commit_after>from django import forms
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _, ugettext
from satchmo_store.accounts.mail import send_welcome_email
from livesettings import config_value
from satchmo_store.contact.forms import ContactInfoForm
from satchmo_store.contact.models import AddressBook, PhoneNumber, Contact
from l10n.models import Country
from satchmo_utils.unique_id import generate_id

import logging
import signals

log = logging.getLogger('accounts.forms')

class EmailAuthenticationForm(AuthenticationForm):
    """Authentication form with a longer username field."""
    
    def __init__(self, *args, **kwargs):
        
        super(EmailAuthenticationForm, self).__init__(*args, **kwargs)
        username = self.fields['username']
        username.max_length = 75
        username.widget.attrs['maxlength'] = 75

class RegistrationForm(forms.Form):
    """The basic account registration form."""
    title = forms.CharField(max_length=30, label=_('Title'), required=False)
    email = forms.EmailField(label=_('Email address'),
        max_length=75, required=True)
    password2 = forms.CharField(label=_('Password (again)'),
        max_length=30, widget=forms.PasswordInput(), required=True)
    password1 = forms.CharField(label=_('Password'),
        max_length=30, widget=forms.PasswordInput(), required=True)
    first_name = forms.CharField(label=_('First name'),
        max_length=30, required=True)
    last_name = forms.CharField(label=_('Last name'),
        max_length=30, required=True)

    def __init__(self, *args, **kwargs):
        self.contact = None
        super(RegistrationForm, self).__init__(*args, **kwargs)

    newsletter = forms.BooleanField(label=_('Newsletter'),
        widget=forms.CheckboxInput(), required=False)

    def clean_password1(self):
        """Enforce that password and password2 are the same."""
        p1 = self.cleaned_data.get('password1')
        p2 = self.cleaned_data.get('password2')
        if not (p1 and p2 and p1 == p2):
            raise forms.ValidationError(
                ugettext("The two passwords do not match."))

        # note, here is where we'd put some kind of custom
        # validator to enforce "hard" passwords.
        return p1

    def clean_email(self):
        """Prevent account hijacking by disallowing duplicate emails."""
        email = self.cleaned_data.get('email', None)
        if email and User.objects.filter(email__iexact=email).count() > 0:
            raise forms.ValidationError(
                ugettext("That email address is already in use."))

        return email

    def save(self, request=None, **kwargs):
        """Create the contact and user described on the form.  Returns the
        `contact`.
        """
        if self.contact:
            log.debug('skipping save, already done')
        else:
            self.save_contact(request)
        return self.contact

    def save_contact(self, request):
        log.debug("Saving contact")
        data = self.cleaned_data
        password = data['<PASSWORD>']
        email = data['email']
        first_name = data['first_name']
        last_name = data['last_name']
        username = generate_id(first_name, last_name, email)

{'fraction_non_alphanumeric': 0.10550058207217695, 'fraction_numerical': 0.009313154831199068, 'mean_word_length': 3.290262172284644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3944570', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1876, 'n_words': 508}