Skip to content

Commit 4bf883d

Browse files
fix(spp_hazard): address gemini-code-assist review findings
- _parse_datetime_string accepts datetime instances (avoids TypeError on programmatic values). - Area-linking query wrapped in a savepoint so an invalid geometry cannot leave the transaction aborted for subsequent operations. - Migration column check scoped to current_schema() to avoid false positives from same-named tables in other schemas.
1 parent ed7d0e5 commit 4bf883d

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

spp_hazard/migrations/19.0.2.1.0/post-migration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def _column_exists(cr, table, column):
9494
SELECT 1
9595
FROM information_schema.columns
9696
WHERE table_name = %s AND column_name = %s
97+
AND table_schema = current_schema()
9798
""",
9899
(table, column),
99100
)

spp_hazard/models/hazard_incident.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ def _parse_datetime_string(value):
2828
Returns:
2929
datetime object
3030
"""
31-
# Replace 'Z' with '+00:00' for fromisoformat compatibility
32-
normalized = value.replace("Z", "+00:00")
33-
dt = datetime.fromisoformat(normalized)
31+
if isinstance(value, datetime):
32+
dt = value
33+
else:
34+
# Replace 'Z' with '+00:00' for fromisoformat compatibility
35+
normalized = value.replace("Z", "+00:00")
36+
dt = datetime.fromisoformat(normalized)
3437
# Odoo requires naive (UTC) datetimes
3538
if dt.tzinfo is not None:
3639
dt = dt.astimezone(UTC).replace(tzinfo=None)
@@ -525,17 +528,20 @@ def _link_areas_from_geometry(self, geometry_dict):
525528
geojson_str = json.dumps(geometry_dict) if isinstance(geometry_dict, dict) else geometry_dict
526529

527530
try:
528-
self.env.cr.execute(
529-
"""
530-
SELECT id FROM spp_area
531-
WHERE geo_polygon IS NOT NULL
532-
AND ST_Intersects(
533-
geo_polygon::geometry,
534-
ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326)
531+
# Savepoint: a failed query (e.g. invalid geometry) would otherwise
532+
# abort the whole transaction, not just this optional linking step.
533+
with self.env.cr.savepoint():
534+
self.env.cr.execute(
535+
"""
536+
SELECT id FROM spp_area
537+
WHERE geo_polygon IS NOT NULL
538+
AND ST_Intersects(
539+
geo_polygon::geometry,
540+
ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326)
541+
)
542+
""",
543+
(geojson_str,),
535544
)
536-
""",
537-
(geojson_str,),
538-
)
539545
area_ids = [row[0] for row in self.env.cr.fetchall()]
540546
if area_ids:
541547
self.write({"area_ids": [Command.set(area_ids)]})

0 commit comments

Comments
 (0)