<commit_before>
from django import forms

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Submit, Field, HTML
from crispy_forms.bootstrap import FormActions


class LoginForm(forms.Form):
    """
    Super simple login form
    """
    email = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    # Form Layout
    helper = FormHelper()
    helper.form_class = 'form-horizontal'
    helper.label_class = 'col-md-4'
    helper.field_class = 'col-md-8'
    helper.layout = Layout(
        Fieldset(
            'Please Login',
            Field('email'),
            Field('password'),
        ),
        FormActions(
            Submit('login', 'Login', css_class="button"),
            #HTML('<br/><a href="{% url \'password_reset\' %}">Recover Password</a>'),
        )
    )<commit_msg>Added user update and create forms. Still needs testing.<commit_after>import string
import random

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group

from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Submit, Field, HTML
from crispy_forms.bootstrap import FormActions


class LoginForm(forms.Form):
    """
    Super simple login form
    """
    email = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    # Form Layout
    helper = FormHelper()
    helper.form_class = 'form-horizontal'
    helper.label_class = 'col-md-4'
    helper.field_class = 'col-md-8'
    helper.layout = Layout(
        Fieldset(
            'Please Login',
            Field('email'),
            Field('password'),
        ),
        FormActions(
            Submit('login', 'Login', css_class="button"),
            #HTML('<br/><a href="{% url \'password_reset\' %}">Recover Password</a>'),
        )
    )


class UserChangeForm(forms.ModelForm):
    """
    A form for updating users.
    """

    user_level = forms.ChoiceField(choices=Group.objects.all().values_list(), label='User Level')
    is_active = forms.ChoiceField(choices=((True, 'Active'), (False, 'Disabled')), label='Status')

    # Form Layout
    helper = FormHelper()
    helper.form_class = 'form-horizontal'
    helper.label_class = 'col-md-4'
    helper.field_class = 'col-md-8'
    helper.layout = Layout(
        Field('email'),
        Field('first_name'),
        Field('last_name'),
        Field('user_level'),
        Field('is_active'),
        FormActions(
            Submit('btnSubmit', 'Submit', css_class="button btn-primary"),
        ),
    )

    class Meta:
        model = get_user_model()

    def __init__(self, *args, **kwargs):
        # form instance and initial values
        initial = kwargs.get('initial', {})
        instance = kwargs.get('instance', {})

        # Set initial values for the non-model questions
        if instance:
            # Get user's group
            groups = instance.groups.all()
            initial['user_level'] = groups[0].id if groups.exists() else None

            # Map is_active question to model property
            initial['is_active'] = instance.is_active

        kwargs['initial'] = initial

        super(UserChangeForm, self).__init__(*args, **kwargs)

        self.fields['password'].required = False
        self.fields['last_login'].required = False
        self.fields['date_joined'].required = False

        f = self.fields.get('user_permissions', None)
        if f is not None:
            f.queryset = f.queryset.select_related('content_type')

    ## Set the hidden inputs to the initial value since we don't want them hoodwinked

    def clean_password(self):
        return self.initial["password"]

    def clean_date_joined(self):
        return self.initial["date_joined"]

    def save(self, commit=True):
        """
        Save the model instance with the correct Auth Group based on the user_level question
        """
        instance = super(UserChangeForm, self).save(commit=commit)

        if commit:
            instance.save()

            # Assign user to selected group
            instance.groups.clear()
            instance.groups.add(Group.objects.get(id=self.cleaned_data['user_level']))

        return instance


class UserCreationForm(UserChangeForm):
    """
    A form for creating new users. Includes all the required fields, plus a
    repeated password.
    """

    error_messages = {'duplicate_email': _("A user with that email already exists."), }

    class Meta:
        model = get_user_model()

    def __init__(self, *args, **kwargs):
        initial = kwargs.get('initial', {})

        # Set some initial values
        initial['date_joined'] = now()
        initial['password'] = '<PASSWORD>'

        kwargs['initial'] = initial

        super(UserCreationForm, self).__init__(*args, **kwargs)

    def clean_email(self):
        """
        Set a nicer error message than the ORM.
        """
        email = self.cleaned_data["email"]
        try:
            get_user_model()._default_manager.get(email=email)
        except get_user_model().DoesNotExist:
            return email
        raise forms.ValidationError(self.error_messages['duplicate_email'])

    def clean_password(self):
        """
        Generate a random 32 char password for this user
        """
        return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(32))

    def save(self, commit=True):
        """
        Save the model instance with the correct Auth Group based on the user_level question
        """
        instance = super(UserCreationForm, self).save(commit=commit)
        instance.set_password(self.cleaned_data['password'])
        instance.save()

        return instance
{'fraction_non_alphanumeric': 0.11646517963562064, 'fraction_numerical': 0.0018729780350757704, 'mean_word_length': 2.6103257529194837, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '3576564', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1600, 'n_words': 414}