# HG changeset patch # User Branko Majic # Date 2013-07-14 13:27:02 # Node ID 5c7da7c67a3edf4521f36224b262a804a7bff437 # Parent 5deeefd8d0fc753318a1848e0b7f859806982745 Noticket: Added a check for existing communications when attempting to move entity to a different project. Includes test of new functionality. 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): """ diff --git a/conntrackt/tests/test_models.py b/conntrackt/tests/test_models.py --- a/conntrackt/tests/test_models.py +++ b/conntrackt/tests/test_models.py @@ -117,6 +117,18 @@ class EntityTest(TestCase): self.assertEqual(entity.get_absolute_url(), "/conntrackt/entity/1/") + def test_project_move_constraints(self): + """ + Tests if entity is prevented from being moved to different project in + case of existing communications. + """ + + entity = Entity.objects.get(pk=1) + new_project = Project.objects.get(pk=2) + + entity.project = new_project + self.assertRaisesRegexp(ValidationError, "The entity cannot be moved to different project as long as it has valid communications with entities in current project." ,entity.clean) + class InterfaceTest(TestCase): fixtures = ['test-data.json']