Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete site #62

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)