From 0a09f18144dbadc8fb027d8e44f09e4ffbb3392b Mon Sep 17 00:00:00 2001 From: andriacap <111564663+andriacap@users.noreply.github.com> Date: Tue, 25 Apr 2023 16:57:05 +0200 Subject: [PATCH] feat: delete site (#62) * feat: delete site Add code from backend to delete site (frontend is already implemented) Reviewed-by: andriac [Refs_ticket]: #5 , #6 * test: add test for delete item site Reviewed-by: andriac [Refs_ticket]: #5 , #6 --- backend/gn_module_monitoring/routes/site.py | 23 ++++++++++++++++--- .../tests/test_routes/test_site.py | 15 ++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/backend/gn_module_monitoring/routes/site.py b/backend/gn_module_monitoring/routes/site.py index e11075dc5..7e25ba0b9 100644 --- a/backend/gn_module_monitoring/routes/site.py +++ b/backend/gn_module_monitoring/routes/site.py @@ -1,13 +1,18 @@ from flask import request from flask.json import jsonify +from geonature.core.gn_commons.schemas import ModuleSchema +from geonature.utils.env import db from sqlalchemy.orm import Load, joinedload from werkzeug.datastructures import MultiDict -from geonature.core.gn_commons.schemas import ModuleSchema - from gn_module_monitoring.blueprint import blueprint from gn_module_monitoring.config.repositories import get_config -from gn_module_monitoring.monitoring.models import BibTypeSite, TMonitoringSites, TNomenclatures, TMonitoringModules +from gn_module_monitoring.monitoring.models import ( + BibTypeSite, + TMonitoringModules, + TMonitoringSites, + TNomenclatures, +) from gn_module_monitoring.monitoring.schemas import BibTypeSiteSchema, MonitoringSitesSchema from gn_module_monitoring.routes.monitoring import create_or_update_object_api_sites_sites_group from gn_module_monitoring.utils.routes import ( @@ -88,12 +93,14 @@ def get_sites(): page=page, ) + @blueprint.route("/sites/", methods=["GET"]) def get_site_by_id(id_base_site): site = TMonitoringSites.query.get_or_404(id_base_site) schema = MonitoringSitesSchema() return schema.dump(site) + @blueprint.route("/sites/geometries", methods=["GET"]) def get_all_site_geometries(): params = MultiDict(request.args) @@ -144,3 +151,13 @@ def post_sites(): customConfig.update(post_data["dataComplement"][keys]["config"]) get_config(module_code, force=True, customSpecConfig=customConfig) return create_or_update_object_api_sites_sites_group(module_code, object_type), 201 + + +@blueprint.route("/sites/", methods=["DELETE"]) +def delete_site(_id): + item = TMonitoringSites.find_by_id(_id) + db.session.delete(item) + db.session.commit() + return { + "success": f"Item with {item.id_g} from table {item.__tablename__} is successfully deleted" + }, 200 diff --git a/backend/gn_module_monitoring/tests/test_routes/test_site.py b/backend/gn_module_monitoring/tests/test_routes/test_site.py index 8b7d5c9af..7b66c59a2 100644 --- a/backend/gn_module_monitoring/tests/test_routes/test_site.py +++ b/backend/gn_module_monitoring/tests/test_routes/test_site.py @@ -1,6 +1,7 @@ import pytest from flask import url_for +from gn_module_monitoring.monitoring.models import TMonitoringSites from gn_module_monitoring.monitoring.schemas import BibTypeSiteSchema, MonitoringSitesSchema from gn_module_monitoring.monitoring.models import TMonitoringSites @@ -175,3 +176,17 @@ def test_post_sites(self, site_to_post_with_types, types_site, site_group_withou res.as_dict()["base_site_name"] == site_to_post_with_types["properties"]["base_site_name"] ) + + def test_delete_site(self, sites): + site = list(sites.values())[0] + id_base_site = site.id_base_site + item = TMonitoringSites.find_by_id(id_base_site) + r = self.client.delete(url_for("monitorings.delete_site", _id=id_base_site)) + + assert ( + r.json["success"] + == f"Item with {item.id_g} from table {item.__tablename__} is successfully deleted" + ) + with pytest.raises(Exception) as e: + TMonitoringSites.query.get_or_404(id_base_site) + assert "404 Not Found" in str(e.value)