email: aliases: Minor refactoring to form validation

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-02-20 03:06:15 -08:00 committed by James Valleroy
parent 3d15ebd436
commit a5a3500630
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -38,13 +38,14 @@ class AliasCreateForm(forms.Form):
def clean_alias(self):
"""Return the checked value for alias."""
value = self.data['alias'].strip().lower()
if not re.match('^[a-z0-9-_\\.]+$', value):
if not re.match(r'^[a-z0-9-_\.]+$', value):
raise ValidationError(_('Contains illegal characters'))
if not re.match('^[a-z0-9].*[a-z0-9]$', value):
if not re.match(r'^[a-z0-9].*[a-z0-9]$', value):
raise ValidationError(_('Must start and end with a-z or 0-9'))
if re.match('^[0-9]+$', value):
# For when uids are automatically aliased to username
if re.match(r'^[0-9]+$', value):
raise ValidationError(_('Cannot be a number'))
if aliases_module.exists(value):