Skip to content

Commit

Permalink
Fix(mtd) fix shcema and api 404 not handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Narcisi committed Aug 27, 2024
1 parent 59335b2 commit 1389d63
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/mtd_sync/conf_schema_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions src/mtd_sync/mtd_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions src/mtd_sync/mtd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1389d63

Please sign in to comment.