From 7da5e4911be10f1265b9dac9a7c82fc9098110f2 Mon Sep 17 00:00:00 2001 From: Justine Fricou Date: Fri, 18 Oct 2024 17:48:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20[BUG]=20Fix=20error=20500=20when?= =?UTF-8?q?=20creating=20an=20intervention=20without=20a=20report?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog.rst | 1 + geotrek/maintenance/forms.py | 11 +++++----- geotrek/maintenance/tests/test_forms.py | 29 +++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 3f0ce22836..b7c3d5a63b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ CHANGELOG **Bug fixes** - Fix `loadinfrastructure` condition when adding to infra without `eid-field` option (#4328) +- Fix intervention creation when target is not a report (suricate workflow) 2.109.2+dev (XXXX-XX-XX) diff --git a/geotrek/maintenance/forms.py b/geotrek/maintenance/forms.py index b50a2c61a1..ac417d9455 100644 --- a/geotrek/maintenance/forms.py +++ b/geotrek/maintenance/forms.py @@ -167,12 +167,11 @@ def clean(self, *args, **kwargs): if 'geotrek.feedback' in settings.INSTALLED_APPS and settings.SURICATE_WORKFLOW_ENABLED: target = self.instance.target intervention_is_updated = self.instance.pk - target_is_a_report = target and isinstance(target, Report) - report_is_programmed_or_late = target.status and target.status.identifier in ["programmed", "late_resolution"] - intervention_is_being_resolved_without_end_date = status == InterventionStatus.objects.get(order=30) and end_date is None - if intervention_is_updated and target_is_a_report and report_is_programmed_or_late and \ - intervention_is_being_resolved_without_end_date: - self.add_error('end_date', _('End date is required.')) + if target and isinstance(target, Report): + report_is_programmed_or_late = target.status and target.status.identifier in ["programmed", "late_resolution"] + intervention_is_being_resolved_without_end_date = status == InterventionStatus.objects.get(order=30) and end_date is None + if intervention_is_updated and report_is_programmed_or_late and intervention_is_being_resolved_without_end_date: + self.add_error('end_date', _('End date is required.')) return clean_data def save(self, *args, **kwargs): diff --git a/geotrek/maintenance/tests/test_forms.py b/geotrek/maintenance/tests/test_forms.py index 5d4f635d3b..16880d7043 100644 --- a/geotrek/maintenance/tests/test_forms.py +++ b/geotrek/maintenance/tests/test_forms.py @@ -1,7 +1,12 @@ -from django.test import TestCase +from unittest import skipIf + +from django.conf import settings +from django.test import TestCase, override_settings from geotrek.authent.tests.factories import UserFactory from geotrek.common.tests import TranslationResetMixin -from geotrek.maintenance.tests.factories import InterventionJobFactory, LightInterventionFactory, ManDayFactory +from geotrek.maintenance.tests.factories import InterventionJobFactory, LightInterventionFactory, ManDayFactory, InterventionStatusFactory +from geotrek.signage.tests.factories import SignageFactory +from geotrek.core.tests.factories import TopologyFactory, PathFactory from geotrek.maintenance.forms import InterventionForm, ManDayForm, ProjectForm @@ -50,6 +55,11 @@ class InterventionFormTest(TestCase): def setUpTestData(cls): cls.interv = LightInterventionFactory() cls.user = UserFactory() + topo = TopologyFactory() + path = PathFactory() + cls.topology = '[{"pk": %d, "paths": [%d], "positions": {"0": [0.674882030756843, 0.110030805790642]}}]' % (topo.pk, path.pk) + cls.target = SignageFactory(), + cls.interv_status = InterventionStatusFactory(order=30) def test_end_date_after_start_date(self): form = InterventionForm( @@ -60,6 +70,21 @@ def test_end_date_after_start_date(self): self.assertFalse(form.is_valid()) self.assertIn("Begin date is after end date", str(form.errors)) + @skipIf(not settings.TREKKING_TOPOLOGY_ENABLED, 'Test with dynamic segmentation only') + @override_settings(SURICATE_WORKFLOW_ENABLED=True) + def test_create_intervention_if_target_is_not_report(self): + form = InterventionForm( + data={ + "name": "abc", + "topology": self.topology, + "target": self.target, + "begin_date": "10/02/2024", + "status": self.interv_status + }, + user=self.user, + ) + self.assertTrue(form.is_valid()) + class ProjectDateFormTest(TranslationResetMixin, TestCase):