Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import warnings
- from django import forms
- from django.contrib.auth import authenticate, get_user_model
- from django.template.defaultfilters import capfirst
- from django.utils.translation import ugettext_lazy as _
- from django.contrib.auth.models import User
- from djangular.forms import NgModelFormMixin
- class RegistrationForm(NgModelFormMixin,forms.Form):
- required_css_class = 'required'
- username = forms.RegexField(regex=r'^[\w.@+-]+$',
- max_length=30,
- label=_("Username"),
- error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
- email = forms.EmailField(label=_("E-mail"))
- password1 = forms.CharField(widget=forms.PasswordInput,
- label=_("Password"))
- password2 = forms.CharField(widget=forms.PasswordInput,
- label=_("Password (again)"))
- def __init__(self, *args, **kwargs):
- kwargs.update(scope_prefix='$parent')
- super(RegistrationForm, self).__init__(*args, **kwargs)
- def clean_username(self):
- """
- Validate that the username is alphanumeric and is not already
- in use.
- """
- existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
- print User.objects.all()
- print existing
- if existing.exists():
- raise forms.ValidationError(_("A user with that username already exists."))
- else:
- return self.cleaned_data['username']
- def clean(self):
- """
- Verifiy that the values entered into the two password fields
- match. Note that an error here will end up in
- ``non_field_errors()`` because it doesn't apply to a single
- field.
- """
- if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
- if self.cleaned_data['password1'] != self.cleaned_data['password2']:
- raise forms.ValidationError(_("The two password fields didn't match."))
- return self.cleaned_data
- class AuthenticationForm(NgModelFormMixin, forms.Form):
- username = forms.CharField(max_length=254)
- password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
- error_messages = {
- 'invalid_login': _("Please enter a correct %(username)s and password. "
- "Note that both fields may be case-sensitive."),
- 'inactive': _("This account is inactive."),
- }
- def __init__(self, request=None, *args, **kwargs):
- """
- The 'request' parameter is set for custom auth use by subclasses.
- The form data comes in via the standard 'data' kwarg.
- """
- kwargs.update(scope_prefix='$parent')
- self.request = request
- self.user_cache = None
- super(AuthenticationForm, self).__init__(*args, **kwargs)
- # Set the label for the "username" field.
- UserModel = get_user_model()
- self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
- if self.fields['username'].label is None:
- self.fields['username'].label = capfirst(self.username_field.verbose_name)
- def clean(self):
- username = self.cleaned_data.get('username')
- password = self.cleaned_data.get('password')
- if username and password:
- self.user_cache = authenticate(username=username,
- password=password)
- if self.user_cache is None:
- raise forms.ValidationError(
- self.error_messages['invalid_login'],
- code='invalid_login',
- params={'username': self.username_field.verbose_name},
- )
- elif not self.user_cache.is_active:
- raise forms.ValidationError(
- self.error_messages['inactive'],
- code='inactive',
- )
- return self.cleaned_data
- def check_for_test_cookie(self):
- warnings.warn("check_for_test_cookie is deprecated; ensure your login "
- "view is CSRF-protected.", DeprecationWarning)
- def get_user_id(self):
- if self.user_cache:
- return self.user_cache.id
- return None
- def get_user(self):
- return self.user_cache
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement