From f7aac57a84e9d0b825f2c24dfe8f430c7dbd5ccf Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Tue, 10 Mar 2015 22:27:49 +0000 Subject: [PATCH] Added websites validators --- orchestra/apps/websites/validators.py | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 orchestra/apps/websites/validators.py diff --git a/orchestra/apps/websites/validators.py b/orchestra/apps/websites/validators.py new file mode 100644 index 00000000..bfb8af5a --- /dev/null +++ b/orchestra/apps/websites/validators.py @@ -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), + })