mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-11 00:33:17 +00:00
Implement field validation for Site.is_default_site; resolves #289
This commit is contained in:
parent
9079d67392
commit
40ac053e5d
1 changed files with 20 additions and 0 deletions
|
|
@ -15,6 +15,7 @@ from django.contrib.auth.models import Group
|
|||
from django.conf import settings
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from treebeard.mp_tree import MP_Node
|
||||
|
||||
|
|
@ -63,6 +64,25 @@ class Site(models.Model):
|
|||
else:
|
||||
return 'http://%s:%d' % (self.hostname, self.port)
|
||||
|
||||
def clean_fields(self, exclude=None):
|
||||
super(Site, self).clean_fields(exclude)
|
||||
# Only one site can have the is_default_site flag set
|
||||
try:
|
||||
default = Site.objects.get(is_default_site=True)
|
||||
except Site.DoesNotExist:
|
||||
pass
|
||||
except Site.MultipleObjectsReturned:
|
||||
raise
|
||||
else:
|
||||
if self.is_default_site and self.pk != default.pk:
|
||||
raise ValidationError(
|
||||
{'is_default_site': [
|
||||
_("%(hostname)s is already configured as the default site. You must unset that before you can save this site as default.")
|
||||
% { 'hostname': default.hostname }
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
# clear the wagtail_site_root_paths cache whenever Site records are updated
|
||||
def save(self, *args, **kwargs):
|
||||
result = super(Site, self).save(*args, **kwargs)
|
||||
|
|
|
|||
Loading…
Reference in a new issue