def export_all_observations(module_code, type, method,jd):
    """
    Export all the observations made on a site group.
    Following formats are available:
    * csv
    * geojson
    * shapefile
    """
    
    view = GenericTableGeo(
        tableName="v_export_" + module_code.lower()+"_"+method, 
        schemaName="gn_monitoring", 
        engine=DB.engine

    )
    columns = view.tableDef.columns
    q = DB.session.query(*columns)
    #data = q.all()
    #----------------------------
    data = DB.session.query(*columns).filter(columns.id_dataset == jd).all()
    #-------------------------------------

    filename = module_code+"_"+method+"_"+dt.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S")

    if type == 'csv':
        return to_csv_resp(
            filename,
            data=serializeQuery(data, q.column_descriptions),
            separator=";",
            columns=[db_col.key for db_col in columns if db_col.key != 'geom'], # Exclude the geom column from CSV
        )
    else:
        raise NotFound("type export not found")
Exemple #2
0
def export_taxon_web(info_role):
    """Optimized route for taxon web export.

    .. :quickref: Synthese;

    This view is customisable by the administrator
    Some columns are mandatory: cd_ref

    POST parameters: Use a list of cd_ref (in POST parameters)
         to filter the v_synthese_taxon_for_export_view

    :query str export_format: str<'csv'>

    """

    taxon_view = GenericTable(
        tableName="v_synthese_taxon_for_export_view",
        schemaName="gn_synthese",
        engine=DB.engine
    )
    columns = taxon_view.tableDef.columns
    # Test de conformité de la vue v_synthese_for_export_view
    try:
        assert hasattr(taxon_view.tableDef.columns, "cd_ref")

    except AssertionError as e:
        return {"msg": """
                        View v_synthese_taxon_for_export_view
                        must have a cd_ref column \n
                        trace: {}
                        """.format(str(e))
                }, 500

    id_list = request.get_json()

    # check R and E CRUVED to know if we filter with cruved
    cruved = cruved_scope_for_user_in_module(
        info_role.id_role, module_code="SYNTHESE"
    )[0]

    subq = DB.session.query(
        VSyntheseForWebApp.cd_ref,
        func.count(distinct(
            VSyntheseForWebApp.id_synthese
        )).label("nb_obs"),
        func.min(VSyntheseForWebApp.date_min).label("date_min"),
        func.max(VSyntheseForWebApp.date_max).label("date_max")
    ).filter(
        VSyntheseForWebApp.id_synthese.in_(id_list)
    ).group_by(VSyntheseForWebApp.cd_ref)

    if cruved["R"] > cruved["E"]:
        # filter on cruved specifying the column
        # id_dataset, id_synthese, id_digitiser
        #   and observer in the v_synthese_for_export_view
        subq = synthese_query.filter_query_with_cruved(
            VSyntheseForWebApp,
            subq,
            info_role,
            id_synthese_column="id_synthese",
            id_dataset_column="id_dataset",
            observers_column="observers",
            id_digitiser_column="id_digitiser",
            with_generic_table=False,
        )
    subq = subq.subquery()

    q = DB.session.query(
        *columns,
        subq.c.nb_obs,
        subq.c.date_min,
        subq.c.date_max
    ).join(
        subq,
        subq.c.cd_ref == columns.cd_ref
    )

    return to_csv_resp(
        datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S"),
        data=serializeQuery(q.all(), q.column_descriptions),
        separator=";",
        columns=[db_col.key for db_col in columns] +
        ["nb_obs", "date_min", "date_max"]
    )