Skip to content

Commit

Permalink
Don't do 2 update per editing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jul 11, 2024
1 parent 5098914 commit bf4db86
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions geoportal/c2cgeoportal_geoportal/views/layers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2012-2023, Camptocamp SA
# Copyright (c) 2012-2024, Camptocamp SA
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -70,6 +70,22 @@
CACHE_REGION = get_region("std")


class _LogLastUpdate:
def __init__(self, layer: "main.Layer"):
self.layer = layer

def __call__(self, request: pyramid.request.Request, feature: Feature, obj: Any) -> None:
del feature # unused

last_update_date = Layers.get_metadata(self.layer, "lastUpdateDateColumn")
if last_update_date is not None:
setattr(obj, last_update_date, datetime.now())

last_update_user = Layers.get_metadata(self.layer, "lastUpdateUserColumn")
if last_update_user is not None:
setattr(obj, last_update_user, request.user.id)


class Layers:
"""
All the layers view (editing).
Expand Down Expand Up @@ -299,9 +315,6 @@ def check_geometry(_: Any, feature: Feature, obj: Any) -> None:
features = protocol.create(self.request)
if isinstance(features, HTTPException):
raise features
if features is not None:
for feature in features.features: # pylint: disable=no-member
self._log_last_update(layer, feature)
return features
except TopologicalError as e:
self.request.response.status_int = 400
Expand Down Expand Up @@ -363,7 +376,6 @@ def check_geometry(_: Any, feature: Feature, obj: Any) -> None:
feature = protocol.update(self.request, feature_id)
if isinstance(feature, HTTPException):
raise feature
self._log_last_update(layer, feature)
return cast(Feature, feature)
except TopologicalError as e:
self.request.response.status_int = 400
Expand All @@ -384,15 +396,6 @@ def _validate_geometry(geom: Geometry) -> None:
reason = models.DBSession.query(func.ST_IsValidReason(func.ST_GeomFromEWKB(geom))).scalar()
raise TopologicalError(reason)

def _log_last_update(self, layer: "main.Layer", feature: Feature) -> None:
last_update_date = self.get_metadata(layer, "lastUpdateDateColumn")
if last_update_date is not None:
setattr(feature, last_update_date, datetime.now())

last_update_user = self.get_metadata(layer, "lastUpdateUserColumn")
if last_update_user is not None:
setattr(feature, last_update_user, self.request.user.id)

@staticmethod
def get_metadata(layer: "main.Layer", key: str, default: Optional[str] = None) -> Optional[str]:
metadata = layer.get_metadata(key)
Expand Down Expand Up @@ -436,7 +439,10 @@ def security_cb(_: Any, obj: Any) -> None:
if allowed.scalar() == 0:
raise HTTPForbidden()

protocol = self._get_protocol_for_layer(layer, before_delete=security_cb)
log_last_update = _LogLastUpdate(layer)
protocol = self._get_protocol_for_layer(
layer, before_create=log_last_update, before_update=log_last_update, before_delete=security_cb
)
response = protocol.delete(self.request, feature_id)
if isinstance(response, HTTPException):
raise response
Expand Down

0 comments on commit bf4db86

Please sign in to comment.