From 1389d63704b9b88e58597ee6c57c79d58f40cf25 Mon Sep 17 00:00:00 2001 From: Pierre Narcisi Date: Tue, 27 Aug 2024 14:58:45 +0200 Subject: [PATCH] Fix(mtd) fix shcema and api 404 not handled --- src/mtd_sync/conf_schema_toml.py | 8 ++++---- src/mtd_sync/mtd_sync.py | 11 ++++++++--- src/mtd_sync/mtd_utils.py | 16 +++++++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/mtd_sync/conf_schema_toml.py b/src/mtd_sync/conf_schema_toml.py index 09a2774..0f66972 100644 --- a/src/mtd_sync/conf_schema_toml.py +++ b/src/mtd_sync/conf_schema_toml.py @@ -2,14 +2,14 @@ class GnModuleSchemaConf(Schema): - BASE_URL = fields.String() + BASE_URL = fields.String(load_default="") XML_NAMESPACE = fields.String(load_default="{http://inpn.mnhn.fr/mtd}") - USER = fields.String() - PASSWORD = fields.String() + USER = fields.String(load_default="") + PASSWORD = fields.String(load_default="") ID_INSTANCE_FILTER = fields.Integer(load_default=None) MTD_API_ENDPOINT = fields.Url(load_default="https://preprod-inpn.mnhn.fr/mtd") SYNC_LOG_LEVEL = fields.String(load_default="INFO") - USERS_CAN_SEE_ORGANISM_DATA = False + USERS_CAN_SEE_ORGANISM_DATA = fields.Boolean(load_default=False) JDD_MODULE_CODE_ASSOCIATION = fields.List(fields.String, load_default=["OCCTAX", "OCCHAB"]) ID_PROVIDER_INPN = fields.String(load_default="cas_inpn") ID_USER_SOCLE_1 = fields.Integer(load_default=1) diff --git a/src/mtd_sync/mtd_sync.py b/src/mtd_sync/mtd_sync.py index 02329d3..bbc0390 100644 --- a/src/mtd_sync/mtd_sync.py +++ b/src/mtd_sync/mtd_sync.py @@ -165,7 +165,8 @@ def _get_user_json(cls, user_id): url = urljoin(cls.base_url, cls.id_search_path) url = url.format(user_id=user_id) response = requests.get(url, auth=(cls.user, cls.password)) - return response.json() + if response.status_code == 200: + return response.json() @classmethod def get_user(cls, user_id): @@ -186,11 +187,14 @@ def add_unexisting_digitizer(id_digitizer): ): # not fast - need perf optimization on user call user = INPNCAS.get_user(id_digitizer) + if not user: + return None # to avoid to create org if user.get("codeOrganisme"): user["codeOrganisme"] = None # insert or update user insert_user_and_org(user) + return user def process_af_and_ds(af_list, ds_list, id_role=None): @@ -212,10 +216,11 @@ def process_af_and_ds(af_list, ds_list, id_role=None): actors = af.pop("actors") with db.session.begin_nested(): start_add_user_time = time.time() - add_unexisting_digitizer(af["id_digitizer"] if not id_role else id_role) + new_user = add_unexisting_digitizer(af["id_digitizer"] if not id_role else id_role) + if new_user == None: + continue user_add_total_time += time.time() - start_add_user_time af = sync_af(af) - print(af) associate_actors( actors, CorAcquisitionFrameworkActor, diff --git a/src/mtd_sync/mtd_utils.py b/src/mtd_sync/mtd_utils.py index e420493..bafa976 100644 --- a/src/mtd_sync/mtd_utils.py +++ b/src/mtd_sync/mtd_utils.py @@ -257,9 +257,15 @@ class CasAuthentificationError(GeonatureApiError): def insert_user_and_org(info_user, update_user_organism: bool = True): id_provider_inpn = current_app.config["MTD_SYNC"]["ID_PROVIDER_INPN"] - if not id_provider_inpn in auth_manager: - raise GeonatureApiError(f"Identity provider named {id_provider_inpn} is not registered ! ") - inpn_identity_provider = auth_manager.get_provider(id_provider_inpn) + idprov = AuthenficationCASINPN() + idprov.id_provider = id_provider_inpn + auth_manager.add_provider(id_provider_inpn, idprov) + + # if not id_provider_inpn in auth_manager: + # raise GeonatureApiError( + # f"Identity provider named {id_provider_inpn} is not registered ! " + # ) + inpn_identity_provider = idprov organism_id = info_user["codeOrganisme"] organism_name = info_user.get("libelleLongOrganisme", "Autre") @@ -294,9 +300,9 @@ def insert_user_and_org(info_user, update_user_organism: bool = True): user_info["id_organisme"] = existing_user.id_organisme # Insert or update user - user_ = User(**user_info) - user_info = inpn_identity_provider.insert_or_update_role(user_, "email") + with current_app.app_context(): + user_info = inpn_identity_provider.insert_or_update_role(user_info, "email") # Associate user to a default group if the user is not associated to any group user = existing_user or db.session.get(User, user_id)