diff --git a/conntrackt/models.py b/conntrackt/models.py --- a/conntrackt/models.py +++ b/conntrackt/models.py @@ -149,6 +149,26 @@ class Entity(models.Model): return reverse("entity", kwargs={'pk': self.pk}) + def clean(self): + """ + Performs additional validation checks on the submitted data. It will + verify the following: + + - That entity is not linked to any other entity in case of project + change. + """ + + # Perform the check if entity is being updated. + if self.pk: + # Fetch the old data from database. + # @TODO: Is it better to do copying during __init__ instead? + old_object = Entity.objects.get(pk=1) + + # Make sure that entity has no communications in current project if + # moving it around. + if self.project != old_object.project and (self.incoming_communications() or self.outgoing_communications()): + raise ValidationError("The entity cannot be moved to different project as long as it has valid communications with entities in current project.") + class Interface(models.Model): """