From 1a145087648c0e76289b768d518ee151dd6194e8 Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Wed, 24 Jan 2018 11:10:58 +0000 Subject: [PATCH] Add a test to ensure we can clean the object before save --- opentech/apply/funds/models.py | 10 +++++++++- opentech/apply/funds/tests/test_models.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py index c585cd9c7..e665586d4 100644 --- a/opentech/apply/funds/models.py +++ b/opentech/apply/funds/models.py @@ -143,7 +143,15 @@ class Round(AbstractStreamForm): Q(end_date__gte=self.start_date) ) - conflicting_rounds = Round.objects.child_of(self.parent_page).filter( + if hasattr(self, 'parent_page'): + # Check if the create hook has added the parent page, we aren't an object yet. + # Ensures we can access related objects during the clean phase instead of save. + base_query = Round.objects.child_of(self.parent_page) + else: + # don't need parent page, we are an actual object now. + base_query = Round.objects.sibling_of(self) + + conflicting_rounds = base_query.filter( conflict_query ).exclude(id=self.id) diff --git a/opentech/apply/funds/tests/test_models.py b/opentech/apply/funds/tests/test_models.py index 78a65c820..431676d67 100644 --- a/opentech/apply/funds/tests/test_models.py +++ b/opentech/apply/funds/tests/test_models.py @@ -111,3 +111,14 @@ class TestRoundModel(TestCase): overlapping_start = existing_round.end_date - timedelta(1) with self.assertRaises(ValidationError): self.make_round(start_date=overlapping_start, end_date=None) + + def test_can_not_overlap_clean(self): + existing_round = self.make_round() + overlapping_start = existing_round.end_date - timedelta(1) + new_round = RoundFactory.build(start_date=overlapping_start, end_date=None) + + # we add on the parent page which gets included from a pre_create_hook + new_round.parent_page = self.fund + + with self.assertRaises(ValidationError): + new_round.clean() -- GitLab