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/api_post_synthese #1793

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions backend/geonature/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def set_sentry_context():
('geonature.core.routes:routes', '/'),
('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.
160 changes: 160 additions & 0 deletions backend/geonature/core/gn_synthese/exchanges/repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
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()

synthese_data_for_get = {
key : synthese_data['properties'].get(key)
for key in ['id_synthese', 'unique_id_sinp', 'id_source', 'entity_source_pk_value']
}

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:
print('new')
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
216 changes: 216 additions & 0 deletions backend/geonature/core/gn_synthese/exchanges/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
'''
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 (
process_from_post_data,
process_to_get_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 (
process_to_get_data(
synthese.as_geofeature('the_geom_4326', 'id_synthese')
)
)

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

process_to_get_data(
synthese.as_geofeature('the_geom_4326', 'id_synthese')
)
return (
process_to_get_data(
synthese.as_geofeature('the_geom_4326', 'id_synthese')
)
)


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:

# check data
# nomenclature code to synthese
# etc...

post_data = request.json
synthese_data = process_from_post_data(post_data, is_post)
synthese = create_or_update_synthese(synthese_data, id_synthese, unique_id_sinp, id_source, entity_source_pk_value)
return (
process_to_get_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