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

Debug #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 32 additions & 16 deletions src/mtd_sync/mtd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from flask import current_app

from sqlalchemy import select, exists
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
from sqlalchemy.sql import func, update


from sqlalchemy.dialects.postgresql import insert as pg_insert

from geonature.utils.env import DB, db
Expand Down Expand Up @@ -228,18 +229,28 @@ def associate_actors(actors, CorActor, pk_name, pk_value):
)
if not id_organism:
values["id_role"] = DB.session.scalar(
select(User.id_role).filter_by(email=actor["email"])
select(User.id_role)
.filter_by(email=actor["email"])
.where(User.groupe.is_(False))
)
else:
values["id_organism"] = id_organism
statement = (
pg_insert(CorActor)
.values(**values)
.on_conflict_do_nothing(
index_elements=[pk_name, "id_organism", "id_nomenclature_actor_role"],
try:
statement = (
pg_insert(CorActor)
.values(**values)
.on_conflict_do_nothing(
index_elements=[
pk_name,
"id_organism" if id_organism else "id_role",
"id_nomenclature_actor_role",
],
)
)
)
DB.session.execute(statement)
DB.session.execute(statement)
except IntegrityError as I:
db.session.rollback()
print("ERROR ASSOCIATE ACTORS")


def associate_dataset_modules(dataset):
Expand All @@ -264,12 +275,17 @@ 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 @@ -306,9 +322,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