Skip to content

Commit

Permalink
🐛 [BUG] Fix error 500 when creating an intervention without a report
Browse files Browse the repository at this point in the history
  • Loading branch information
justinefricou committed Oct 21, 2024
1 parent 7d721f0 commit f04a46c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
**Bug fixes**

- Fix `loadinfrastructure` condition when adding to infra without `eid-field` option (#4328)
- Fix error 500 at intervention creation when target is not a report (suricate workflow)


2.109.2+dev (XXXX-XX-XX)
Expand Down
11 changes: 5 additions & 6 deletions geotrek/maintenance/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 21 additions & 2 deletions geotrek/maintenance/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.test import TestCase
from django.test import TestCase, override_settings, modify_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.maintenance.forms import InterventionForm, ManDayForm, ProjectForm


Expand Down Expand Up @@ -50,6 +51,9 @@ class InterventionFormTest(TestCase):
def setUpTestData(cls):
cls.interv = LightInterventionFactory()
cls.user = UserFactory()
cls.topology = '[{"pk": 2, "kind": "TREK", "offset": 0.0, "paths": [3], "positions": {"0": [0.674882030756843, 0.110030805790642]}}]'
cls.target = SignageFactory(),
cls.interv_status = InterventionStatusFactory(order=30)

def test_end_date_after_start_date(self):
form = InterventionForm(
Expand All @@ -60,6 +64,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))

@override_settings(SURICATE_WORKFLOW_ENABLED=True)
@modify_settings(INSTALLED_APPS={'append': 'geotrek.feedback'})
def test_ok_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):

Expand Down

0 comments on commit f04a46c

Please sign in to comment.