Skip to content

Commit

Permalink
feat: delete site (#62)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andriacap authored and amandine-sahl committed Oct 5, 2023
1 parent 42266a5 commit e2b944b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 20 additions & 3 deletions backend/gn_module_monitoring/routes/site.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -88,12 +93,14 @@ def get_sites():
page=page,
)


@blueprint.route("/sites/<int:id_base_site>", 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)
Expand Down Expand Up @@ -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/<int:_id>", 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
15 changes: 15 additions & 0 deletions backend/gn_module_monitoring/tests/test_routes/test_site.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)

0 comments on commit e2b944b

Please sign in to comment.