From a517e25195648c6cf785a6f4fe7f62667356e5f0 Mon Sep 17 00:00:00 2001 From: Andria Capai Date: Wed, 5 Apr 2023 11:55:49 +0200 Subject: [PATCH] feat(back): add custom config when post site Change backend files in order to choose specific config when site is created. Reviewed-by: andriac [Refs_ticket]: #5 , #6 --- backend/gn_module_monitoring/config/repositories.py | 4 +++- backend/gn_module_monitoring/monitoring/objects.py | 4 ++++ backend/gn_module_monitoring/monitoring/serializer.py | 3 +++ backend/gn_module_monitoring/routes/site.py | 6 +++++- backend/gn_module_monitoring/routes/sites_groups.py | 4 ++++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/gn_module_monitoring/config/repositories.py b/backend/gn_module_monitoring/config/repositories.py index 1359d1852..94fe53738 100644 --- a/backend/gn_module_monitoring/config/repositories.py +++ b/backend/gn_module_monitoring/config/repositories.py @@ -77,7 +77,7 @@ def get_config_objects(module_code, config, tree=None, parent_type=None, customS # recursif if tree[object_type]: - get_config_objects(module_code, config, tree[object_type], object_type) + get_config_objects(module_code, config, tree[object_type], object_type,customSpecConfig) def config_object_from_files(module_code, object_type,custom=None): @@ -87,6 +87,8 @@ def config_object_from_files(module_code, object_type,custom=None): generic_config_object = json_config_from_file('generic', object_type) specific_config_object = {} if module_code == 'generic' else json_config_from_file(module_code, object_type) + if module_code == 'generic' and object_type == 'site' and custom is not None: + specific_config_object = custom config_object = generic_config_object config_object.update(specific_config_object) diff --git a/backend/gn_module_monitoring/monitoring/objects.py b/backend/gn_module_monitoring/monitoring/objects.py index b72b7e0c4..b69eb45f4 100644 --- a/backend/gn_module_monitoring/monitoring/objects.py +++ b/backend/gn_module_monitoring/monitoring/objects.py @@ -31,8 +31,12 @@ def preprocess_data(self, data): for id_type_site in data['types_site']: if int(id_type_site) not in type_site_ids: type_site_ids.append(id_type_site) + #TODO: A enlever une fois qu'on aura enelever le champ "id_nomenclature_type_site" du model et de la bdd + data["id_nomenclature_type_site"]=data["types_site"][0] data['types_site'] = type_site_ids + + # module_ids = [module.id_module for module in self._model.modules] # id_module = int(data['id_module']) # if id_module not in module_ids: diff --git a/backend/gn_module_monitoring/monitoring/serializer.py b/backend/gn_module_monitoring/monitoring/serializer.py index 5ccb36295..b052de3ba 100644 --- a/backend/gn_module_monitoring/monitoring/serializer.py +++ b/backend/gn_module_monitoring/monitoring/serializer.py @@ -189,6 +189,9 @@ def populate(self, post_data): # ajout des données en base if hasattr(self._model, 'from_geofeature'): + for key in list(post_data): + if key not in ("properties","geometry","type"): + post_data.pop(key) self._model.from_geofeature(post_data, True) else: self._model.from_dict(properties, True) diff --git a/backend/gn_module_monitoring/routes/site.py b/backend/gn_module_monitoring/routes/site.py index ca10d1100..2431e262f 100644 --- a/backend/gn_module_monitoring/routes/site.py +++ b/backend/gn_module_monitoring/routes/site.py @@ -112,6 +112,10 @@ def get_module_sites(module_code: str): def post_sites(): module_code = "generic" object_type = "site" + customConfig = dict() post_data = dict(request.get_json()) - get_config(module_code, force=True) + for keys in post_data['dataComplement'].keys(): + if 'config' in post_data['dataComplement'][keys]: + customConfig.update( post_data['dataComplement'][keys]['config']) + get_config(module_code, force=True,customSpecConfig=customConfig) return create_or_update_object_api(module_code, object_type), 201 diff --git a/backend/gn_module_monitoring/routes/sites_groups.py b/backend/gn_module_monitoring/routes/sites_groups.py index 954ae35fa..0c1568b37 100644 --- a/backend/gn_module_monitoring/routes/sites_groups.py +++ b/backend/gn_module_monitoring/routes/sites_groups.py @@ -130,6 +130,10 @@ def create_or_update_object_api(module_code, object_type, id=None): module = get_module("module_code", module_code) else: module = {"id_module": "generic"} + #TODO : A enlever une fois que le post_data contiendra geometry et type depuis le front + if object_type == "site": + post_data["geometry"]={'type':'Point', 'coordinates':[2.5,50]} + post_data["type"]='Feature' # on rajoute id_module s'il n'est pas renseigné par défaut ?? if "id_module" not in post_data["properties"]: module["id_module"] = "generic"