def insert_organism(organism): """ Insert un organisme """ if organism is not None: data = organism else: data = dict(request.get_json()) organism = BibOrganismes(**data) if organism.id_organisme: exist_org = DB.session.query(BibOrganismes).get(organism.id_organisme) if exist_org: DB.session.merge(organism) else: DB.session.add(organism) else: DB.session.add(organism) DB.session.commit() DB.session.flush() return organism.as_dict()
def create_cor_object_actors(actors, new_object): """ Create a new cor_dataset_actor/cor_acquisition_framework_actor object for the JDD/AF Input : actors (list) : List of all actors related to the JDD/AF new_object : JDD or AF """ for act in actors: # person = None # id_person = None org = None id_organism = None # For the moment wo do not match the user with the actor provided by the XML -> only the organism # If the email of the contact Person was provided in the XML file, we try to link him to the t_role table # if act["email"]: # # We first check if the Person's email exists in the t_role table # person = ( # DB.session.query(User) # .filter(User.email == act["email"]) # .first() # ) # # If not, we create it as a new Person in the t_role table and get his ID back # if not person: # if act["uuid_organism"]: # org = ( # DB.session.query(BibOrganismes) # .filter(BibOrganismes.uuid_organisme == act["uuid_organism"]) # .first() # ) # person = { # "id_role": None, # "nom_role": act["name"], # "email": act["email"], # } # if org: # person['id_organisme'] = org.id_organisme # resp = users.insert_role(person) # id_person = json.loads(resp.data.decode('utf-8'))['id_role'] # else: # id_person = person.id_role # If the informations about the Organism is provided, we try to link it to the bib_organismes table if act["uuid_organism"] or act["organism"]: # UUID in actually only present on JDD XML files # Filter on UUID is preferable if available since it avoids dupes based on name changes if act["uuid_organism"]: org = (DB.session.query(BibOrganismes).filter( BibOrganismes.uuid_organisme == act["uuid_organism"]).first()) else: org = (DB.session.query(BibOrganismes).filter( BibOrganismes.nom_organisme == act["organism"]).first()) # If no Organism was corresponding in the bib_organismes table, we add it if not org: org = BibOrganismes( **{ "nom_organisme": act["organism"], "uuid_organisme": act["uuid_organism"], }) DB.session.add(org) DB.session.commit() id_organism = org.id_organisme # With at least the Person or the Organism was provided for the actor in the XML file, # we build the data for the correlation if id_organism: dict_cor = { "id_organism": id_organism, "id_nomenclature_actor_role": func.ref_nomenclatures.get_id_nomenclature( "ROLE_ACTEUR", act["actor_role"]), } # We finally build the correlation corresponding to the JDD/AF if isinstance(new_object, TAcquisitionFramework): cor_actor = CorAcquisitionFrameworkActor(**dict_cor) new_object.cor_af_actor.append(cor_actor) elif isinstance(new_object, TDatasets): cor_actor = CorDatasetActor(**dict_cor) new_object.cor_dataset_actor.append(cor_actor)