diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-01-24 18:46:27 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-01-24 18:46:27 +0100 |
commit | e874b48bc99c2081ca918bf3e6ef6602f8148307 (patch) | |
tree | 240a04d141dbbce702de39f17b6859fb888a342e /archaeological_context_records/models.py | |
parent | 73d21de642443d03a76285a9038389a5acbdd92d (diff) | |
download | Ishtar-e874b48bc99c2081ca918bf3e6ef6602f8148307.tar.bz2 Ishtar-e874b48bc99c2081ca918bf3e6ef6602f8148307.zip |
Prevent incoherent RelationType
* check that no relation type with symmetrical=True and an inverse relation are
set
* after saving check that theinverse_relation of the inverse_relation
point to the saved object
* test update
Diffstat (limited to 'archaeological_context_records/models.py')
-rw-r--r-- | archaeological_context_records/models.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index a8c6a1dcc..9a4fa7880 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -19,6 +19,7 @@ from django.conf import settings from django.contrib.gis.db import models +from django.core.exceptions import ValidationError from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _, ugettext, pgettext @@ -252,6 +253,24 @@ class RelationType(GeneralType): verbose_name_plural = _(u"Relation types") ordering = ('order',) + def clean(self): + # cannot have symmetrical and an inverse_relation + if self.symmetrical and self.inverse_relation: + raise ValidationError( + _(u"Cannot have symmetrical and an inverse_relation")) + + def save(self, *args, **kwargs): + obj = super(RelationType, self).save(*args, **kwargs) + # after saving check that the inverse_relation of the inverse_relation + # point to the saved object + if self.inverse_relation \ + and (not self.inverse_relation.inverse_relation + or self.inverse_relation.inverse_relation != self): + self.inverse_relation.inverse_relation = self + self.inverse_relation.symmetrical = False + self.inverse_relation.save() + return obj + class RecordRelations(models.Model): left_record = models.ForeignKey(ContextRecord, related_name='left_relations') |