Do full email validation in oauth signup form master github/master
authorMagnus Hagander
Wed, 2 Jul 2025 20:20:52 +0000 (22:20 +0200)
committerMagnus Hagander
Wed, 2 Jul 2025 20:28:30 +0000 (22:28 +0200)
These fields aren't editable anyway, but if we don't do the full
validation we will instead crash if for example the same account
creation form is submitted twice (happens surprisingly often). Now we
will instead show a validation error message.

pgweb/account/forms.py

index 31cd374136d8d2e2a954faac490c78a5e5b8ac5f..fb3641b0a5808d24f6a8223e215ed3f4833ff438 100644 (file)
@@ -28,6 +28,18 @@ def _clean_username(username):
     raise forms.ValidationError("This username is already in use")
 
 
+def _clean_email(email):
+    email = email.lower()
+
+    if User.objects.filter(email=email).exists():
+        raise forms.ValidationError("A user with this email address is already registered")
+
+    if SecondaryEmail.objects.filter(email=email).exists():
+        raise forms.ValidationError("This email address is already attached to a different user")
+
+    return email
+
+
 # Override some error handling only in the default authentication form
 class PgwebAuthenticationForm(AuthenticationForm):
     def clean(self):
@@ -91,15 +103,7 @@ class SignupForm(forms.Form):
         return _clean_username(self.cleaned_data['username'])
 
     def clean_email(self):
-        email = self.cleaned_data['email'].lower()
-
-        if User.objects.filter(email=email).exists():
-            raise forms.ValidationError("A user with this email address is already registered")
-
-        if SecondaryEmail.objects.filter(email=email).exists():
-            raise forms.ValidationError("This email address is already attached to a different user")
-
-        return email
+        return _clean_email(self.cleaned_data['email'])
 
 
 class SignupOauthForm(forms.Form):
@@ -122,7 +126,7 @@ class SignupOauthForm(forms.Form):
         return _clean_username(self.cleaned_data['username'])
 
     def clean_email(self):
-        return self.cleaned_data['email'].lower()
+        return _clean_email(self.cleaned_data['email'])
 
 
 class UserProfileForm(forms.ModelForm):