<commit_before>#-*- coding: utf-8 -*-
#
# django_pam/accounts/forms.py
#

import inspect

from django import forms
from django.contrib.auth import get_user_model, get_backends
from django.contrib.auth.forms import AuthenticationForm


class GeneralAuthenticationForm(AuthenticationForm):
    """
    Authentication form
    """
    email = forms.EmailField(required=False)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        email = self.cleaned_data.get('email')

        if username and password:
            self.user_cache = self.authenticate(username=username,
                                                password=password)

            if self.user_cache:
                if email:
                    self.user_cache = get_user_model().objects.update(
                        username, email=email)

                self.confirm_login_allowed(self.user_cache)

        if not (username and password and self.user_cache):
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name},
                )

        return self.cleaned_data

    def authenticate(self, **credentials):
        """
        If the given credentials are valid, return a User object.
        """
        user = None

        for backend in get_backends():
            try:
                inspect.getcallargs(backend.authenticate, **credentials)
            except TypeError:
                # This backend doesn't accept these credentials as arguments.
                # Try the next one.
                continue

            try:
                user = backend.authenticate(**credentials)
            except PermissionDenied:
                # This backend says to stop in our tracks - this user should
                # not be allowed in at all. (user is None)
                break
            else:
                if user:
                    # Annotate the user object with the path of the backend.
                    # (user is a valid object)
                    user.backend = "{}.{}".format(backend.__module__,
                                                  backend.__class__.__name__)
                    break

        return user

    class Media:
        css = {
            'all': ('django_pam/css/auth.css',)
            }
        js = ('django_pam/js/auth.js',)
<commit_msg>Fxed a bug where the user's email was not being saved.<commit_after>#-*- coding: utf-8 -*-
#
# django_pam/accounts/forms.py
#

import logging
import inspect

from django import forms
from django.contrib.auth import get_user_model, get_backends
from django.contrib.auth.forms import AuthenticationForm

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


class GeneralAuthenticationForm(AuthenticationForm):
    """
    Authentication form
    """
    email = forms.EmailField(required=False)

    def __init__(self, request=None, *args, **kwargs):
        #log.debug("request: %s, args: %s, kwargs: %s", request, args, kwargs)
        super(GeneralAuthenticationForm, self).__init__(
            request=request, *args, **kwargs)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = <PASSWORD>.cleaned_data.get('password')
        email = self.cleaned_data.get('email')

        if username and password:
            self.user_cache = self.authenticate(username=username,
                                                password=password)

            if self.user_cache:
                if email:
                    self.user_cache.email = email
                    self.user_cache.save()

                self.confirm_login_allowed(self.user_cache)

        if not (username and password and self.user_cache):
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name},
                )

        return self.cleaned_data

    def authenticate(self, **credentials):
        """
        If the given credentials are valid, return a User object.
        """
        user = None

        for backend in get_backends():
            try:
                inspect.getcallargs(backend.authenticate, **credentials)
            except TypeError:
                # This backend doesn't accept these credentials as arguments.
                # Try the next one.
                continue

            try:
                user = backend.authenticate(**credentials)
            except PermissionDenied:
                # This backend says to stop in our tracks - this user should
                # not be allowed in at all. (user is None)
                break
            else:
                if user:
                    # Annotate the user object with the path of the backend.
                    # (user is a valid object)
                    user.backend = "{}.{}".format(backend.__module__,
                                                  backend.__class__.__name__)
                    break

        return user

    class Media:
        css = {
            'all': ('django_pam/css/auth.css',)
            }
        js = ('django_pam/js/auth.js',)

{'fraction_non_alphanumeric': 0.0868425977915029, 'fraction_numerical': 0.0003743215422047539, 'mean_word_length': 1.633809758501725, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6859593', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1279, 'n_words': 348}