Enhanced call validation for log and registration

This commit is contained in:
Sebastian Lohff 2017-01-22 19:50:51 +01:00
parent c4b3b68c9c
commit d9e12e362d
2 changed files with 15 additions and 5 deletions

View File

@ -6,7 +6,7 @@ from crispy_forms.layout import Submit
from django.urls import reverse from django.urls import reverse
from .models import User, Reference, QSO from .models import User, Reference, QSO
from .validators import CallUsernameValidator from .validators import CallUsernameValidator, CallLogValidator
class CustomUserCreationForm(UserCreationForm): class CustomUserCreationForm(UserCreationForm):
class Meta: class Meta:
@ -62,7 +62,7 @@ class QSOForm(forms.ModelForm):
raise forms.ValidationError("Reference already exists") raise forms.ValidationError("Reference already exists")
try: try:
CallUsernameValidator()(data) CallLogValidator()(data)
except forms.ValidationError: except forms.ValidationError:
raise forms.ValidationError("Enter a valid callsign (1-2 chars, a number, 1-4 chars, maybe a /[A-Z])") raise forms.ValidationError("Enter a valid callsign (1-2 chars, a number, 1-4 chars, maybe a /[A-Z])")

View File

@ -9,10 +9,20 @@ import re
@deconstructible @deconstructible
class CallUsernameValidator(validators.RegexValidator): class CallUsernameValidator(validators.RegexValidator):
#regex = r'^[\w.@+-]+$' #regex = r'^[\w.@+-]+$'
regex = r'^(?:[A-Z]+/)?[A-Z]{1,2}[0-9][A-Z]{1,4}(?:-[0-9])?(?:/p)?$' regex = r'^(?:[A-Z]+/)?[A-Z]{1,2}[0-9][A-Z]{1,4}(?:-[0-9])??$'
message = _( message = _(
'Enter a valid Callsign as Username, ALL UPPERCASE, if needed with /A /B,' 'Enter a valid Callsign as Username, ALL UPPERCASE, if needed with -1 / -2,'
'e.g. DL7BST, DN1BER/A, DL/OE1FOO' 'e.g. DL7BST, DN1BER-1, DL/OE1FOO.'
)
flags = re.ASCII if six.PY3 else 0
@deconstructible
class CallLogValidator(validators.RegexValidator):
#regex = r'^[\w.@+-]+$'
regex = r'^(?:[A-Z]+/)?[A-Z]{1,2}[0-9][A-Z]{1,4}(?:-[0-9])?(?:/[A-Z]{1,3})?$'
message = _(
'Enter a valid callsign, ALL UPPERCASE, if needed with -1 / -2,'
'e.g. DL7BST, DN1BER-1, DL/OE1FOO, DN1FTW-1/p'
) )
flags = re.ASCII if six.PY3 else 0 flags = re.ASCII if six.PY3 else 0