def check_username(request): """Validate a username and check for uniqueness.""" username = request.GET.get('username', None) f = UsernameField() try: f.clean(username) except ValidationError: return http.HttpResponse() try: UserProfile.objects.get(username=username) return http.HttpResponse() except UserProfile.DoesNotExist: pass return http.HttpResponse(status=404)
class RegisterForm(forms.ModelForm): username = UsernameField() full_name = forms.CharField(max_length=255, required=False) password = forms.CharField(max_length=128, widget=forms.PasswordInput(render_value=False)) password_confirm = forms.CharField( max_length=128, widget=forms.PasswordInput(render_value=False)) newsletter = forms.BooleanField(required=False) preflang = forms.CharField( max_length=3, widget=forms.Select(choices=settings.SUPPORTED_LANGUAGES)) recaptcha = captcha_fields.ReCaptchaField() class Meta: model = User fields = ('username', 'email', 'preflang') widgets = { 'username': forms.TextInput(attrs={'autocomplete': 'off'}), } def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) if not settings.RECAPTCHA_PRIVATE_KEY: del self.fields['recaptcha'] def clean_password(self): password = self.cleaned_data['password'] message = check_password_complexity(password) if message: self._errors['password'] = forms.util.ErrorList([message]) return password def clean_username(self): username = self.cleaned_data['username'] if UserProfile.objects.filter(username=username).exists(): raise forms.ValidationError( _('User profile with this Username already exists.')) return username def clean_email(self): email = self.cleaned_data['email'] if not email or not email.strip(): raise forms.ValidationError(_('This field is required.')) if UserProfile.objects.filter(email=email).exists(): raise forms.ValidationError( _('User profile with this Email already exists.')) return email def clean(self): super(RegisterForm, self).clean() data = self.cleaned_data validate_user_identity(self, data) if 'password' in data and 'password_confirm' in data: if data['password'] != data['password_confirm']: self._errors['password_confirm'] = forms.util.ErrorList( [_('Passwords do not match.')]) if 'full_name' in data: data['full_name'] = data['full_name'].strip() return data
class RegisterForm(forms.ModelForm): username = UsernameField() password = forms.CharField( max_length=255, widget=forms.PasswordInput(render_value=False)) password_confirm = forms.CharField( max_length=255, widget=forms.PasswordInput(render_value=False)) recaptcha = captcha_fields.ReCaptchaField() class Meta: model = UserProfile widgets = { 'username': forms.TextInput(attrs={'autocomplete': 'off'}), } def __init__(self, *args, **kwargs): super(RegisterForm, self).__init__(*args, **kwargs) if not settings.RECAPTCHA_PRIVATE_KEY: del self.fields['recaptcha'] def clean_password(self): password = self.cleaned_data['password'] message = check_password_complexity(password) if message: self._errors['password'] = forms.util.ErrorList([message]) return password def clean(self): """Ensure password and password_confirm match.""" super(RegisterForm, self).clean() data = self.cleaned_data if 'password' in data and 'password_confirm' in data: if data['password'] != data['password_confirm']: self._errors['password_confirm'] = forms.util.ErrorList([ _('Passwords do not match.')]) return data