validate upstream in form

This commit is contained in:
Jens Langhammer 2019-03-22 10:55:26 +01:00
parent f33e553cfd
commit 81ac951872
1 changed files with 11 additions and 1 deletions

View File

@ -1,4 +1,5 @@
"""passbook Application Security Gateway Forms"""
from urllib.parse import urlparse
from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
@ -19,9 +20,18 @@ class ApplicationGatewayProviderForm(forms.ModelForm):
if ApplicationGatewayProvider.objects \
.filter(server_name__overlap=current) \
.exclude(pk=self.instance.pk).exists():
raise ValidationError("Server Name already in use.")
raise ValidationError(_("Server Name already in use."))
return current
def clean_upstream(self):
"""Check that upstream begins with http(s)"""
for upstream in self.cleaned_data.get('upstream'):
_parsed_url = urlparse(upstream)
if _parsed_url.scheme not in ('http', 'https'):
raise ValidationError(_("URL Scheme must be either http or https"))
return self.cleaned_data.get('upstream')
class Meta:
model = ApplicationGatewayProvider