Skip to content

Commit

Permalink
feat/api_post_synthese
Browse files Browse the repository at this point in the history
  • Loading branch information
joelclems committed Feb 21, 2023
1 parent a67ffc7 commit d02b1a7
Show file tree
Hide file tree
Showing 7 changed files with 870 additions and 12 deletions.
1 change: 1 addition & 0 deletions backend/geonature/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def set_sentry_context():
("geonature.core.gn_permissions.backoffice.views:routes", "/permissions_backoffice"),
("geonature.core.users.routes:routes", "/users"),
("geonature.core.gn_synthese.routes:routes", "/synthese"),
("geonature.core.gn_synthese.exchanges.routes:routes", "/exchanges"),
("geonature.core.gn_meta.routes:routes", "/meta"),
("geonature.core.ref_geo.routes:routes", "/geo"),
("geonature.core.auth.routes:routes", "/gn_auth"),
Expand Down
Empty file.
136 changes: 136 additions & 0 deletions backend/geonature/core/gn_synthese/exchanges/repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import uuid
from ..models import Synthese, TSources

from geonature.utils.env import DB


def request_synthese(
id_synthese=None, unique_id_sinp=None, id_source=None, entity_source_pk_value=None
):
if id_source and entity_source_pk_value:
filter_params = {
"id_source": id_source,
"entity_source_pk_value": str(entity_source_pk_value),
}

elif unique_id_sinp:
filter_params = {"unique_id_sinp": unique_id_sinp}

elif id_synthese:
filter_params = {"id_synthese": id_synthese}

return DB.session.query(Synthese).filter_by(**filter_params) if filter_params else None


def get_synthese(
id_synthese=None, unique_id_sinp=None, id_source=None, entity_source_pk_value=None
):
"""
get synthese for exchange
soit par:
- id_synthese
- uuid_sinp
- id_source et entity_source_pk_value
"""

req_synthese = request_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value)

return req_synthese.one() if req_synthese else Synthese()


def create_or_update_synthese(
synthese_data,
id_synthese=None,
unique_id_sinp=None,
id_source=None,
entity_source_pk_value=None,
):
"""
post or patch synthese for exchange
"""

DB.session.commit()

if id_synthese or unique_id_sinp or (id_source and entity_source_pk_value):
synthese = get_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value)

else:
synthese = Synthese()
DB.session.add(synthese)

# synthese from dict -> marshmallow
if "geometry" in synthese_data:
synthese.from_geofeature(synthese_data, recursif=True, col_geom_name="the_geom_4326")
else:
synthese.from_dict(synthese_data["properties"], True)

# on attribut un uuid dans le cas d'un post sans uuid
if (not synthese.id_synthese) and (not synthese.unique_id_sinp):
synthese.unique_id_sinp = uuid.uuid4()

DB.session.commit()

return synthese


def delete_synthese(id_synthese=None):
"""
delete synthese
"""

synthese = request_synthese(id_synthese)

if not synthese:
return 0

res = synthese.delete()
DB.session.commit()
return res


def get_source(id_source):
"""
get source
"""

return DB.session.query(TSources).filter_by(id_source=id_source).one()


def get_sources():
"""
get all sources
"""

return DB.session.query(TSources).all()


def create_or_update_source(source_data):
"""
create or update source
"""

id_source = source_data.get("id_source")

source = get_source(id_source) if id_source else TSources()

if not id_source:
DB.session.add(source)

# passer en marshmallow
source.from_dict(source_data, True)

DB.session.commit()

return source


def delete_source(id_source):
""" """

res = DB.session.query(TSources).filter_by(id_source=id_source).delete()
DB.session.commit()

return res
215 changes: 215 additions & 0 deletions backend/geonature/core/gn_synthese/exchanges/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
"""
Module pour l'api post synthese et pour les fonctionalité d'échanges de données plus général
"""

from flask import Blueprint, request, current_app
from utils_flask_sqla.response import json_resp
from geonature.utils.env import DB, ROOT_DIR
from geonature.core.gn_permissions import decorators as permissions

from .repository import (
get_synthese,
create_or_update_synthese,
delete_synthese,
get_source,
get_sources,
create_or_update_source,
delete_source,
)

from .util import pre_process_synthese_data, post_process_synthese_data, ApiSyntheseException


routes = Blueprint("gn_exchanges", __name__)


@routes.route(
"/synthese/<int:id_synthese>",
methods=["GET"],
defaults={"unique_id_sinp": None, "id_source": None, "entity_source_pk_value": None},
)
@routes.route(
"/synthese/<string:unique_id_sinp>",
methods=["GET"],
defaults={"id_synthese": None, "id_source": None, "entity_source_pk_value": None},
)
@routes.route(
"/synthese/<int:id_source>/<entity_source_pk_value>",
methods=["GET"],
defaults={"id_synthese": None, "unique_id_sinp": None},
)
@permissions.check_cruved_scope("R", module_code="SYNTHESE")
@json_resp
def get_exchanges_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value):
"""
get synthese for exchange
"""

try:
synthese = get_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value)
return post_process_synthese_data(synthese.as_geofeature("the_geom_4326", "id_synthese"))

except Exception as e:
return f"Erreur {str(e)}", 500


def patch_or_post_exchange_synthese(
is_post, id_synthese=None, unique_id_sinp=None, id_source=None, entity_source_pk_value=None
):
"""
post or patch synthese for exchange
post_data:
nomenclature by code (code_type is supposed to be known)
"""

try:
post_data = request.json
# check data
# nomenclature code to synthese
# etc...
synthese_data = pre_process_synthese_data(post_data, is_post)
synthese = create_or_update_synthese(
synthese_data, id_synthese, unique_id_sinp, id_source, entity_source_pk_value
)
return post_process_synthese_data(synthese.as_geofeature("the_geom_4326", "id_synthese"))

except ApiSyntheseException as e:
return e.as_dict(), 500
except Exception as e:
return str(e), 500


@routes.route("/synthese/", methods=["POST"])
@permissions.check_cruved_scope("C", module_code="SYNTHESE")
@json_resp
def post_exchanges_synthese():
"""
post put synthese for exchange
"""

return patch_or_post_exchange_synthese(True)


# @routes.route("/synthese/<int:id_synthese>", methods=["PATCH", 'PUT'], defaults={'unique_id_sinp':None, 'id_source':None, 'entity_source_pk_value':None})
# @routes.route("/synthese/<string:unique_id_sinp>", methods=["PATCH", 'PUT'], defaults={'id_synthese':None, 'id_source':None, 'entity_source_pk_value':None})
@routes.route(
"/synthese/<int:id_source>/<entity_source_pk_value>",
methods=["PATCH", "PUT"],
defaults={"id_synthese": None, "unique_id_sinp": None},
)
@permissions.check_cruved_scope("U", module_code="SYNTHESE")
@json_resp
def patch_exchanges_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value):
"""
patch put synthese for exchange
"""

return patch_or_post_exchange_synthese(
False, id_synthese, unique_id_sinp, id_source, entity_source_pk_value
)


# @routes.route("/synthese/<int:id_synthese>", methods=["DELETE"], defaults={'unique_id_sinp':None, 'id_source':None, 'entity_source_pk_value':None})
# @routes.route("/synthese/<string:unique_id_sinp>", methods=["DELETE"], defaults={'id_synthese':None, 'id_source':None, 'entity_source_pk_value':None})
@routes.route(
"/synthese/<int:id_source>/<entity_source_pk_value>",
methods=["DELETE"],
defaults={"id_synthese": None, "unique_id_sinp": None},
)
@permissions.check_cruved_scope("D", module_code="SYNTHESE")
@json_resp
def delete_exchanges_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value):
"""
delete synthese for exchange
"""

id_synthese = None

try:
synthese = get_synthese(id_synthese, unique_id_sinp, id_source, entity_source_pk_value)
id_synthese = synthese.id_synthese
delete_synthese(synthese.id_synthese)
except ApiSyntheseException as e:
return e.as_dict(), 500
except Exception as e:
return str(e), 500

return id_synthese


@routes.route("/sources", methods=["GET"])
@permissions.check_cruved_scope("R", module_code="SYNTHESE")
@json_resp
def api_get_sources():
"""
api get source
"""
try:
sources = get_sources()
except:
return "Pas de sources définies", 500

return [source.as_dict() for source in sources]


@routes.route("/source/<int:id_source>", methods=["GET"])
@permissions.check_cruved_scope("R", module_code="SYNTHESE")
@json_resp
def api_get_source(id_source):
"""
api get source
"""
try:
source = get_source(id_source)
except:
return "Pas de source défine pour (id_source={})".format(id_source), 500

return source.as_dict()


def patch_or_post_exchange_source():
"""
post or patchsource for exchange
"""

post_data = request.json

source = create_or_update_source(post_data)

return source.as_dict()


@routes.route("/source/", methods=["PATCH"])
@permissions.check_cruved_scope("U", module_code="SYNTHESE")
@json_resp
def api_patch_source():
"""
api patch source
"""

return patch_or_post_exchange_source()


@routes.route("/source/", methods=["POST"])
@permissions.check_cruved_scope("C", module_code="SYNTHESE")
@json_resp
def api_post_source():
"""
api post source
"""

return patch_or_post_exchange_source()


@routes.route("/source/<int:id_source>", methods=["DELETE"])
@permissions.check_cruved_scope("D", module_code="SYNTHESE")
@json_resp
def api_delete_source(id_source):
"""
api delete source
"""

return delete_source(id_source)
Loading

0 comments on commit d02b1a7

Please sign in to comment.