Skip to content

Commit

Permalink
Merge pull request #23 from rebkwok/master
Browse files Browse the repository at this point in the history
Move validation of domain to save so it is called after the pre_save signal
  • Loading branch information
mghughes committed Jan 13, 2016
2 parents c06e0ce + 36ceb4a commit 2b1601f
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions multisite/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,16 @@ def __unicode__(self):

def save_base(self, *args, **kwargs):
self.full_clean()
super(Alias, self).save_base(*args, **kwargs)

def clean_fields(self, exclude=None, *args, **kwargs):
errors = {}
try:
super(Alias, self).clean_fields(exclude=exclude, *args, **kwargs)
except ValidationError as e:
errors = e.update_error_dict(errors)

try:
self.clean_domain()
except ValidationError as e:
errors = e.update_error_dict(errors)

if errors:
raise ValidationError(errors)

def clean_domain(self):
# For canonical Alias, domains must match Site domains.
# This needs to be validated here so that it is executed *after* the
# Site pre-save signal updates the domain (an AliasInline modelform
# on SiteAdmin will be saved (and it's clean methods run before
# the Site is saved)
if self.is_canonical and self.domain != self.site.domain:
raise ValidationError(
{'domain': ['Does not match %r' % self.site]}
)
super(Alias, self).save_base(*args, **kwargs)

def validate_unique(self, exclude=None):
errors = {}
Expand All @@ -226,7 +213,7 @@ def validate_unique(self, exclude=None):
field_error = self.unique_error_message(self.__class__,
(field_name,))
if field_name not in errors or \
field_error not in errors[field_name]:
str(field_error) not in [str(err) for err in errors[field_name]]:
qset = self.__class__.objects.filter(
**{field_name + '__iexact': getattr(self, field_name)}
)
Expand Down

0 comments on commit 2b1601f

Please sign in to comment.