Added websites validators

This commit is contained in:
Marc Aymerich 2015-03-10 22:27:49 +00:00
parent 2f7861db33
commit f7aac57a84

View file

@ -0,0 +1,30 @@
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from .models import Website
def validate_domain_protocol(website, domain, protocol):
if protocol == Website.HTTP:
qset = Q(
Q(protocol=Website.HTTP) |
Q(protocol=Website.HTTP_AND_HTTPS) |
Q(protocol=Website.HTTPS_ONLY)
)
elif protocol == Website.HTTPS:
qset = Q(
Q(protocol=Website.HTTPS) |
Q(protocol=Website.HTTP_AND_HTTPS) |
Q(protocol=Website.HTTPS_ONLY)
)
elif protocol in (Website.HTTP_AND_HTTPS, Website.HTTPS_ONLY):
qset = Q()
else:
raise ValidationError({
'protocol': _("Unknown protocol %s") % protocol
})
if domain.websites.filter(qset).exclude(pk=website.pk).exists():
raise ValidationError({
'domains': 'A website is already defined for "%s" on protocol %s' % (domain, protocol),
})