Skip to content

Commit 2590afa

Browse files
committed
fix: set_detection_control_status implemented for DetectionData
1 parent d56e34a commit 2590afa

File tree

10 files changed

+61
-40
lines changed

10 files changed

+61
-40
lines changed

core/management/commands/import_detections.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def queue_detection(self, detection_row: Dict[str, Any]):
341341

342342
if linked_detection_same_tileset:
343343
log_event(f"Detection already exists in tileset {self.tile_set.name}, id: {
344-
linked_detection_same_tileset.id}. Skipping...")
344+
linked_detection_same_tileset.id}. Skipping...")
345345
return
346346
else:
347347
linked_detections = DetectionService.get_linked_detections(
@@ -404,15 +404,15 @@ def queue_detection(self, detection_row: Dict[str, Any]):
404404
detection_object.address = serialized_detection["address"]
405405
detection_object.save()
406406

407-
if not detection_data.detection_control_status:
408-
detection_data.detection_control_status = (
409-
linked_detection.detection_data.detection_control_status
410-
)
411-
412407
if not detection_data.detection_validation_status:
413408
detection_data.detection_validation_status = (
414409
linked_detection.detection_data.detection_validation_status
415410
)
411+
412+
if not detection_data.detection_control_status:
413+
detection_data.set_detection_control_status(
414+
linked_detection.detection_data.detection_control_status
415+
)
416416
else:
417417
parcel = (
418418
Parcel.objects.filter(geometry__contains=centroid)
@@ -449,7 +449,7 @@ def queue_detection(self, detection_row: Dict[str, Any]):
449449
self.detection_objects_to_insert.append(detection_object)
450450

451451
if not detection_data.detection_control_status:
452-
detection_data.detection_control_status = (
452+
detection_data.set_detection_control_status(
453453
DetectionControlStatus.NOT_CONTROLLED
454454
)
455455

@@ -510,7 +510,7 @@ def insert_detections(self, force=False):
510510
)
511511
else:
512512
log_event(f"Inserted {
513-
self.total_inserted_detections} detections in total")
513+
self.total_inserted_detections} detections in total")
514514

515515
log_event(f"Elapsed time: {datetime.now() - self.start_time}")
516516

core/management/commands/import_pvs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def handle(self, *args, **options):
140140
detection.detection_data.official_report_date = (
141141
official_report_date
142142
)
143-
detection.detection_data.detection_control_status = (
143+
detection.detection_data.set_detection_control_status(
144144
detection_control_status
145145
)
146146

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generated by Django 5.0.6 on 2025-12-19 14:54
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("core", "0114_email_alter_historicaluser_date_joined_and_more"),
9+
]
10+
11+
operations = [
12+
migrations.RunSQL(
13+
sql="""
14+
CREATE OR REPLACE FUNCTION calculate_surface_m2(geom geometry)
15+
RETURNS double precision
16+
LANGUAGE plpgsql
17+
IMMUTABLE
18+
AS $$
19+
BEGIN
20+
-- For metropolitan France, use Lambert-93 (EPSG:2154)
21+
-- For other territories, you might need different projections
22+
RETURN ST_Area(ST_Transform(geom, 2154));
23+
END;
24+
$$;
25+
""",
26+
reverse_sql="DROP FUNCTION IF EXISTS calculate_surface_m2(geometry);",
27+
),
28+
]

core/models/detection_data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ class DetectionData(TimestampedModelMixin, UuidModelMixin, DeletableModelMixin):
7575
bases=[HistoriedModelMixin], cascade_delete_history=True
7676
)
7777

78+
def set_detection_control_status(self, value: DetectionControlStatus):
79+
self.detection_control_status = value
80+
81+
if (
82+
value != DetectionControlStatus.NOT_CONTROLLED
83+
and self.detection_validation_status
84+
== DetectionValidationStatus.DETECTED_NOT_VERIFIED
85+
):
86+
self.detection_validation_status = DetectionValidationStatus.SUSPECT
87+
7888
class Meta:
7989
indexes = UuidModelMixin.Meta.indexes + [
8090
models.Index(fields=["detection_validation_status"]),

core/serializers/detection_data.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,13 @@ def update(self, instance: DetectionData, validated_data):
192192
)
193193
detection_authorization.save()
194194

195+
detection_control_status = validated_data.pop("detection_control_status", None)
196+
195197
for key, value in validated_data.items():
196198
setattr(instance, key, value)
197199

198-
if (
199-
instance.detection_validation_status
200-
== DetectionValidationStatus.DETECTED_NOT_VERIFIED
201-
):
202-
instance.detection_validation_status = DetectionValidationStatus.SUSPECT
200+
if detection_control_status:
201+
instance.set_detection_control_status(detection_control_status)
203202

204203
instance.user_last_update = user
205204
instance.save()

core/services/detection_bulk_update.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.contrib.gis.geos import MultiPolygon
33

44
from core.models.detection import Detection
5-
from core.models.detection_data import DetectionValidationStatus
65
from core.models.object_type import ObjectType
76
from core.permissions.user import UserPermission
87
from django.core.exceptions import BadRequest
@@ -40,9 +39,6 @@ def update_multiple_detections(
4039
detection, detection_data_fields_to_update, update_data
4140
)
4241

43-
# Apply business rules for validation status
44-
self._apply_validation_status_rules(detection)
45-
4642
detection.detection_data.user_last_update = self.user
4743
detection_datas_to_update.append(detection.detection_data)
4844

@@ -99,22 +95,18 @@ def _update_detection_data_fields(
9995
) -> None:
10096
"""Update detection data fields with new values."""
10197
for field in fields_to_update:
98+
if field == "detection_control_status":
99+
detection.detection_data.set_detection_control_status(
100+
update_data[field]
101+
)
102+
continue
103+
102104
setattr(
103105
detection.detection_data,
104106
field,
105107
update_data[field],
106108
)
107109

108-
def _apply_validation_status_rules(self, detection: Detection) -> None:
109-
"""Apply business rules for validation status changes."""
110-
if (
111-
detection.detection_data.detection_validation_status
112-
== DetectionValidationStatus.DETECTED_NOT_VERIFIED
113-
):
114-
detection.detection_data.detection_validation_status = (
115-
DetectionValidationStatus.SUSPECT
116-
)
117-
118110
def _perform_bulk_updates(
119111
self,
120112
detection_datas_to_update: List,

core/services/detection_process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def merge_double_detections(tile_set_id: int):
6262
detection_to_keep.detection_source = extract_higest_priority_value(
6363
detections, "detection_source"
6464
)
65-
detection_to_keep.detection_data.detection_control_status = (
65+
detection_to_keep.detection_data.set_detection_control_status(
6666
extract_higest_priority_value(
6767
detections_data, "detection_control_status"
6868
)

core/services/external_api.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,11 @@ def update_control_status(
6969
for detection_object in parcel.detection_objects.all():
7070
for detection in detection_object.detections.all():
7171
detection_data = detection.detection_data
72-
detection_data.detection_control_status = control_status
72+
detection_data.set_detection_control_status(control_status)
7373
detection_data.detection_validation_status_change_reason = (
7474
DetectionValidationStatusChangeReason.EXTERNAL_API
7575
)
7676

77-
if (
78-
detection_data.detection_validation_status
79-
== DetectionValidationStatus.DETECTED_NOT_VERIFIED
80-
):
81-
detection_data.detection_validation_status = (
82-
DetectionValidationStatus.SUSPECT
83-
)
84-
8577
detections_data_to_update.append(detection_data)
8678

8779
bulk_update_with_history(

core/services/prior_letter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _update_control_status(self, detection_object: DetectionObject) -> None:
7373
detections_to_update = []
7474
for detection in detection_object.detections.all():
7575
if detection.detection_data:
76-
detection.detection_data.detection_control_status = (
76+
detection.detection_data.set_detection_control_status(
7777
DetectionControlStatus.PRIOR_LETTER_SENT
7878
)
7979
detection.detection_data.user_last_update = self.user

core/views/utils/generate_prior_letter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def update_control_status(detection_object: DetectionObject, user: User):
8383
]:
8484
return
8585

86-
detection.detection_data.detection_control_status = (
86+
detection.detection_data.set_detection_control_status(
8787
DetectionControlStatus.PRIOR_LETTER_SENT
8888
)
8989
detection.detection_data.save()

0 commit comments

Comments
 (0)