Exemplo n.º 1
0
def get_datasets(info_role):
    """
    Retourne la liste des datasets
    Parameters:
        info_role(TRole)
        active (boolean)
        id_acquisition_framework (integer)

    """
    with_mtd_error = False
    if current_app.config["CAS_PUBLIC"]["CAS_AUTHENTIFICATION"]:
        # synchronise the CA and JDD from the MTD WS
        try:
            mtd_utils.post_jdd_from_user(
                id_user=info_role.id_role, id_organism=info_role.id_organisme
            )
        except Exception as e:
            gunicorn_error_logger.info(e)
            log.error(e)
            with_mtd_error = True
    params = dict(request.args)
    datasets = get_datasets_cruved(info_role, params)
    datasets_resp = {"data": datasets}
    if with_mtd_error:
        datasets_resp["with_mtd_errors"] = True
    if not datasets:
        return datasets_resp, 404
    return datasets_resp
Exemplo n.º 2
0
def get_organismes_jdd(info_role):
    """
    Get all organisms and the JDD where there are actor and where 
    the current user hase autorization with its cruved

    .. :quickref: User;
    """
    params = request.args.to_dict()

    datasets = [dataset["id_dataset"] for dataset in get_datasets_cruved(info_role)]
    q = (
        DB.session.query(BibOrganismes)
        .join(
            CorDatasetActor, BibOrganismes.id_organisme == CorDatasetActor.id_organism
        )
        .filter(CorDatasetActor.id_dataset.in_(datasets))
        .distinct()
    )
    if "orderby" in params:
        try:
            order_col = getattr(BibOrganismes.__table__.columns, params.pop("orderby"))
            q = q.order_by(order_col)
        except AttributeError:
            log.error("the attribute to order on does not exist")
    return [organism.as_dict() for organism in q.all()]
Exemplo n.º 3
0
def get_datasets(info_role):
    """
    Get datasets list
    
    .. :quickref: Metadata;

    :param info_role: add with kwargs
    :type info_role: TRole
    :query boolean active: filter on active fiel
    :query int id_acquisition_framework: get only dataset of given AF
    :returns:  `dict{'data':list<TDatasets>, 'with_erros': <boolean>}`
    """
    with_mtd_error = False
    if current_app.config["CAS_PUBLIC"]["CAS_AUTHENTIFICATION"]:
        # synchronise the CA and JDD from the MTD WS
        try:
            mtd_utils.post_jdd_from_user(id_user=info_role.id_role,
                                         id_organism=info_role.id_organisme)
        except Exception as e:
            gunicorn_error_logger.info(e)
            log.error(e)
            with_mtd_error = True
    params = request.args.to_dict()
    datasets = get_datasets_cruved(info_role, params)
    datasets_resp = {"data": datasets}
    if with_mtd_error:
        datasets_resp["with_mtd_errors"] = True
    if not datasets:
        return datasets_resp, 404
    return datasets_resp
Exemplo n.º 4
0
def general_stats(info_role):
    """Return stats about synthese.

    .. :quickref: Synthese;

        - nb of observations
        - nb of distinct species
        - nb of distinct observer
        - nb ob datasets
    """
    allowed_datasets = get_datasets_cruved(info_role)
    q = DB.session.query(
        func.count(Synthese.id_synthese),
        func.count(func.distinct(Synthese.cd_nom)),
        func.count(func.distinct(Synthese.observers)),
    )
    q = synthese_query.filter_query_with_cruved(Synthese, q, info_role)
    data = q.one()
    data = {
        "nb_data": data[0],
        "nb_species": data[1],
        "nb_observers": data[2],
        "nb_dataset": len(allowed_datasets),
    }
    return data
Exemplo n.º 5
0
def general_stats(info_role):
    """Return stats about synthese.

    .. :quickref: Synthese;

        - nb of observations
        - nb of distinct species
        - nb of distinct observer
        - nb of datasets
    """
    allowed_datasets = get_datasets_cruved(info_role)
    q = select(
        [
            func.count(Synthese.id_synthese),
            func.count(func.distinct(Synthese.cd_nom)),
            func.count(func.distinct(Synthese.observers))
        ]
    )
    synthese_query_obj = SyntheseQuery(Synthese, q, {})
    synthese_query_obj.filter_query_with_cruved(info_role)
    result = DB.session.execute(synthese_query_obj.query)
    synthese_counts = result.fetchone()

    data = {
        "nb_data": synthese_counts[0],
        "nb_species": synthese_counts[1],
        "nb_observers": synthese_counts[2],
        "nb_dataset": len(allowed_datasets),
    }
    return data
Exemplo n.º 6
0
def get_datasets(info_role):
    """
    Retourne la liste des datasets
    Parameters:
        info_role(TRole)
        active (boolean)
        id_acquisition_framework (integer)

    """
    with_mtd_error = False
    if current_app.config["CAS_PUBLIC"]["CAS_AUTHENTIFICATION"]:
        # synchronise the CA and JDD from the MTD WS
        try:
            mtd_utils.post_jdd_from_user(
                id_user=info_role.id_role, id_organism=info_role.id_organisme
            )
        except Exception as e:
            gunicorn_error_logger.info(e)
            log.error(e)
            with_mtd_error = True
    params = dict(request.args)
    datasets = get_datasets_cruved(info_role, params)
    datasets_resp = {"data": datasets}
    if with_mtd_error:
        datasets_resp["with_mtd_errors"] = True
    if not datasets:
        return datasets_resp, 404
    return datasets_resp
Exemplo n.º 7
0
def get_organismes_jdd(info_role):
    """
    Get all organisms and the JDD where there are actor and where 
    the current user hase autorization with its cruved
    .. :quickref: User;
    """

    datasets = [
        dataset["id_dataset"] for dataset in get_datasets_cruved(info_role)
    ]
    organisms = (DB.session.query(BibOrganismes).join(
        CorDatasetActor,
        BibOrganismes.id_organisme == CorDatasetActor.id_organism).filter(
            CorDatasetActor.id_dataset.in_(datasets)).distinct(
                BibOrganismes.id_organisme).all())
    return [organism.as_dict() for organism in organisms]
Exemplo n.º 8
0
def get_organismes_jdd(info_role):
    """
        Retourne tous les organismes qui sont acteurs dans un JDD
        et dont l'utilisateur a des droit sur ce JDD (via son CRUVED)
    """

    datasets = [dataset["id_dataset"] for dataset in get_datasets_cruved(info_role)]
    organisms = (
        DB.session.query(BibOrganismes)
        .join(
            CorDatasetActor, BibOrganismes.id_organisme == CorDatasetActor.id_organism
        )
        .filter(CorDatasetActor.id_dataset.in_(datasets))
        .distinct(BibOrganismes.id_organisme)
        .all()
    )
    return [organism.as_dict() for organism in organisms]
Exemplo n.º 9
0
def get_af_and_ds_metadata(info_role):
    """
    Get all AF with their datasets 
    The Cruved in only apply on dataset in order to see all the AF
    where the user have rights with its dataset
    Use in maplist
    Add the CRUVED permission for each row (Dataset and AD)
    
    .. :quickref: Metadata;

    :param info_role: add with kwargs
    :type info_role: TRole
    :returns:  `dict{'data':list<AF with Datasets>, 'with_erros': <boolean>}`
    """
    with_mtd_error = False
    if current_app.config["CAS_PUBLIC"]["CAS_AUTHENTIFICATION"]:
        # synchronise the CA and JDD from the MTD WS
        try:
            mtd_utils.post_jdd_from_user(id_user=info_role.id_role,
                                         id_organism=info_role.id_organisme)
        except Exception as e:
            gunicorn_error_logger.info(e)
            log.error(e)
            with_mtd_error = True
    params = request.args.to_dict()
    params["orderby"] = "dataset_name"
    datasets = get_datasets_cruved(info_role, params, as_model=True)
    ids_dataset_user = TDatasets.get_user_datasets(info_role, only_user=True)
    ids_dataset_organisms = TDatasets.get_user_datasets(info_role,
                                                        only_user=False)
    ids_afs_user = TAcquisitionFramework.get_user_af(info_role, only_user=True)
    ids_afs_org = TAcquisitionFramework.get_user_af(info_role, only_user=False)
    user_cruved = cruved_scope_for_user_in_module(
        id_role=info_role.id_role,
        module_code="METADATA",
    )[0]

    #  get all af from the JDD filtered with cruved or af where users has rights
    ids_afs_cruved = [
        d.id_acquisition_framework
        for d in get_af_cruved(info_role, as_model=True)
    ]
    list_id_af = [d.id_acquisition_framework
                  for d in datasets] + ids_afs_cruved
    afs = (DB.session.query(TAcquisitionFramework).filter(
        TAcquisitionFramework.id_acquisition_framework.in_(
            list_id_af)).order_by(
                TAcquisitionFramework.acquisition_framework_name).all())

    afs_dict = []
    #  get cruved for each AF and prepare dataset
    for af in afs:
        af_dict = af.as_dict()
        af_dict["cruved"] = af.get_object_cruved(
            user_cruved=user_cruved,
            id_object=af.id_acquisition_framework,
            ids_object_user=ids_afs_user,
            ids_object_organism=ids_afs_org,
        )
        af_dict["datasets"] = []
        afs_dict.append(af_dict)

    #  get cruved for each ds and push them in the af
    for d in datasets:
        dataset_dict = d.as_dict()
        dataset_dict["cruved"] = d.get_object_cruved(
            user_cruved=user_cruved,
            id_object=d.id_dataset,
            ids_object_user=ids_dataset_user,
            ids_object_organism=ids_dataset_organisms,
        )
        af_of_dataset = get_af_from_id(d.id_acquisition_framework, afs_dict)
        af_of_dataset["datasets"].append(dataset_dict)

    afs_resp = {"data": afs_dict}
    if with_mtd_error:
        afs_resp["with_mtd_errors"] = True
    if not datasets:
        return afs_resp, 404
    return afs_resp