From e46729ee04b3a6ff1d2a0d76220bc9d3d5602e94 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 18 Dec 2019 22:40:33 +0530 Subject: [PATCH] Add name validators to all signup forms --- digitalglarus/forms.py | 4 +++- hosting/forms.py | 11 ++++++++++- utils/forms.py | 12 +++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/digitalglarus/forms.py b/digitalglarus/forms.py index a0e685f8..d50b9cc1 100644 --- a/digitalglarus/forms.py +++ b/digitalglarus/forms.py @@ -8,6 +8,7 @@ from utils.forms import LoginFormMixin, SignupFormMixin, BillingAddressForm from .models import MembershipType, MembershipOrder from .models import Booking, BookingOrder +from membership.models import validate_name class LoginForm(LoginFormMixin): @@ -19,7 +20,8 @@ class SignupForm(SignupFormMixin): confirm_password = forms.CharField(widget=forms.PasswordInput()) password = forms.CharField(widget=forms.PasswordInput()) name = forms.CharField(label='name', - widget=forms.TextInput(attrs={'placeholder': 'Full name'})) + widget=forms.TextInput(attrs={'placeholder': 'Full name'}), + validators=[validate_name]) class MembershipBillingForm(BillingAddressForm): diff --git a/hosting/forms.py b/hosting/forms.py index 947cee44..757fe811 100644 --- a/hosting/forms.py +++ b/hosting/forms.py @@ -6,9 +6,10 @@ import tempfile from django import forms from django.conf import settings from django.contrib.auth import authenticate +from django.core.exceptions import ValidationError from django.utils.translation import ugettext_lazy as _ -from membership.models import CustomUser +from membership.models import CustomUser, validate_name from .models import UserHostingKey, GenericProduct logger = logging.getLogger(__name__) @@ -164,6 +165,14 @@ class HostingUserSignupForm(forms.ModelForm): raise forms.ValidationError("Passwords don't match") return confirm_password + def clean_name(self): + name = self.cleaned_data.get('name') + try: + validate_name(name) + except ValidationError as ve: + raise forms.ValidationError(_("Improper Name")) + return name + class UserHostingKeyForm(forms.ModelForm): private_key = forms.CharField(widget=forms.HiddenInput(), required=False) diff --git a/utils/forms.py b/utils/forms.py index fdc67d26..f4588654 100644 --- a/utils/forms.py +++ b/utils/forms.py @@ -3,8 +3,9 @@ from django.contrib.auth import authenticate from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ +from django.core.exceptions import ValidationError -from membership.models import CustomUser +from membership.models import CustomUser, validate_name from .models import ContactMessage, BillingAddress, UserBillingAddress @@ -30,6 +31,15 @@ class SignupFormMixin(forms.ModelForm): raise forms.ValidationError("Passwords don't match") return confirm_password + def clean_name(self): + name = self.cleaned_data.get('name') + try: + validate_name(name) + except ValidationError as ve: + raise forms.ValidationError(_("Improper Name")) + return name + + class LoginFormMixin(forms.Form): email = forms.CharField(widget=forms.EmailInput())