From 34b3657205af782a438a5bc9d2fe955fc36e87ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 11 Jul 2024 15:39:50 +0200 Subject: [PATCH] Don't do 2 update per editing --- .../c2cgeoportal_geoportal/views/layers.py | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/geoportal/c2cgeoportal_geoportal/views/layers.py b/geoportal/c2cgeoportal_geoportal/views/layers.py index 7e6b2db04aa..bfdd273dd16 100644 --- a/geoportal/c2cgeoportal_geoportal/views/layers.py +++ b/geoportal/c2cgeoportal_geoportal/views/layers.py @@ -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 request, 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, self.request.user.id) + + class Layers: """ All the layers view (editing). @@ -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 @@ -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 @@ -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) @@ -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