Beispiel #1
0
def export(info_role):
    """Export data from pr_occtax.v_export_occtax view (parameter)

    .. :quickref: Occtax; Export data from pr_occtax.v_export_occtax

    :query str format: format of the export ('csv', 'geojson', 'shapefile', 'gpkg')

    """
    export_view_name = blueprint.config["export_view_name"]
    export_geom_column = blueprint.config["export_geom_columns_name"]
    export_columns = blueprint.config["export_columns"]
    export_srid = blueprint.config["export_srid"]
    export_format = request.args[
        "format"] if "format" in request.args else "geojson"
    export_col_name_additional_data = blueprint.config[
        "export_col_name_additional_data"]

    export_view = GenericTableGeo(
        tableName=export_view_name,
        schemaName="pr_occtax",
        engine=DB.engine,
        geometry_field=export_geom_column,
        srid=export_srid,
    )
    columns = (export_columns if len(export_columns) > 0 else
               [db_col.key for db_col in export_view.db_cols])

    releve_repository = ReleveRepository(export_view)
    q = releve_repository.get_filtered_query(info_role,
                                             from_generic_table=True)
    q = get_query_occtax_filters(
        request.args,
        export_view,
        q,
        from_generic_table=True,
        obs_txt_column=blueprint.config["export_observer_txt_column"],
    )

    if current_app.config["OCCTAX"]["ADD_MEDIA_IN_EXPORT"]:
        q, columns = releve_repository.add_media_in_export(q, columns)
    data = q.all()

    file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S")
    file_name = filemanager.removeDisallowedFilenameChars(file_name)

    #Ajout des colonnes additionnels
    additional_col_names = []
    query_add_fields = DB.session.query(TAdditionalFields).filter(
        TAdditionalFields.modules.any(module_code="OCCTAX")).filter(
            TAdditionalFields.exportable == True)
    global_add_fields = query_add_fields.filter(
        ~TAdditionalFields.datasets.any()).all()
    if "id_dataset" in request.args:
        dataset_add_fields = query_add_fields.filter(
            TAdditionalFields.datasets.any(
                id_dataset=request.args['id_dataset'])).all()
        global_add_fields = [*global_add_fields, *dataset_add_fields]

    additional_col_names = [field.field_name for field in global_add_fields]
    if export_format == "csv":
        # set additional data col at the end (remove it and inset it ...)
        columns.remove(export_col_name_additional_data)
        columns = columns + additional_col_names
        columns.append(export_col_name_additional_data)
        if additional_col_names:
            serialize_result = [
                as_dict_with_add_cols(export_view, row,
                                      export_col_name_additional_data,
                                      additional_col_names) for row in data
            ]
        else:
            serialize_result = [export_view.as_dict(row) for row in data]
        return to_csv_resp(file_name, serialize_result, columns, ";")
    elif export_format == "geojson":
        if additional_col_names:
            features = []
            for row in data:
                properties = as_dict_with_add_cols(
                    export_view, row, export_col_name_additional_data,
                    additional_col_names)
                feature = Feature(properties=properties,
                                  geometry=to_shape(
                                      getattr(row, export_geom_column)))
                features.append(feature)
            serialize_result = FeatureCollection(features)

        else:
            serialize_result = FeatureCollection([
                export_view.as_geofeature(d, fields=export_columns)
                for d in data
            ])
        return to_json_resp(serialize_result,
                            as_file=True,
                            filename=file_name,
                            indent=4,
                            extension="geojson")
    else:
        try:
            db_cols = [
                db_col for db_col in export_view.db_cols
                if db_col.key in export_columns
            ]
            dir_name, file_name = export_as_geo_file(
                export_format=export_format,
                export_view=export_view,
                db_cols=db_cols,
                geojson_col=None,
                data=data,
                file_name=file_name,
            )
            db_cols = [
                db_col for db_col in export_view.db_cols
                if db_col.key in export_columns
            ]

            return send_from_directory(dir_name, file_name, as_attachment=True)
        except GeonatureApiError as e:
            message = str(e)

        return render_template(
            "error.html",
            error=message,
            redirect=current_app.config["URL_APPLICATION"] + "/#/occtax",
        )
Beispiel #2
0
def export(info_role):
    """Export data from pr_occtax.v_export_occtax view (parameter)

    .. :quickref: Occtax; Export data from pr_occtax.v_export_occtax

    :query str format: format of the export ('csv', 'geojson', 'shapefile')

    """
    export_view_name = blueprint.config["export_view_name"]
    export_geom_column = blueprint.config["export_geom_columns_name"]
    export_columns = blueprint.config["export_columns"]
    export_srid = blueprint.config["export_srid"]

    export_view = GenericTableGeo(
        tableName=export_view_name,
        schemaName="pr_occtax",
        engine=DB.engine,
        geometry_field=export_geom_column,
        srid=export_srid,
    )

    releve_repository = ReleveRepository(export_view)
    q = releve_repository.get_filtered_query(info_role, from_generic_table=True)
    q = get_query_occtax_filters(
        request.args,
        export_view,
        q,
        from_generic_table=True,
        obs_txt_column=blueprint.config["export_observer_txt_column"],
    )

    data = q.all()

    file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S")
    file_name = filemanager.removeDisallowedFilenameChars(file_name)

    export_format = request.args["format"] if "format" in request.args else "geojson"
    if export_format == "csv":
        columns = (
            export_columns
            if len(export_columns) > 0
            else [db_col.key for db_col in export_view.db_cols]
        )
        return to_csv_resp(
            file_name, [export_view.as_dict(d) for d in data], columns, ";"
        )
    elif export_format == "geojson":
        results = FeatureCollection(
            [export_view.as_geofeature(d, columns=export_columns) for d in data]
        )
        return to_json_resp(
            results, as_file=True, filename=file_name, indent=4, extension="geojson"
        )
    else:
        try:
            db_cols = [
                db_col for db_col in export_view.db_cols if db_col.key in export_columns
            ]
            dir_name, file_name = export_as_geo_file(
                export_format=export_format,
                export_view=export_view,
                db_cols=db_cols,
                geojson_col=None,
                data=data,
                file_name=file_name,
            )
            return send_from_directory(dir_name, file_name, as_attachment=True)

        except GeonatureApiError as e:
            message = str(e)

        return render_template(
            "error.html",
            error=message,
            redirect=current_app.config["URL_APPLICATION"] + "/#/occtax",
        )
Beispiel #3
0
def export(info_role):
    """Export data from pr_occtax.export_occtax_sinp view (parameter)

    .. :quickref: Occtax; Export data from pr_occtax.export_occtax_sinp

    :query str format: format of the export ('csv', 'geojson', 'shapefile', 'medias')

    """
    export_view_name = blueprint.config["export_view_name"]
    export_geom_column = blueprint.config["export_geom_columns_name"]
    export_columns = blueprint.config["export_columns"]
    export_srid = blueprint.config["export_srid"]

    export_view = GenericTableGeo(
        tableName=export_view_name,
        schemaName="pr_occtax",
        engine=DB.engine,
        geometry_field=export_geom_column,
        srid=export_srid,
    )

    releve_repository = ReleveRepository(export_view)
    q = releve_repository.get_filtered_query(info_role,
                                             from_generic_table=True)
    q = get_query_occtax_filters(
        request.args,
        export_view,
        q,
        from_generic_table=True,
        obs_txt_column=blueprint.config["export_observer_txt_column"],
    )

    data = q.all()

    file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S")
    file_name = filemanager.removeDisallowedFilenameChars(file_name)

    export_format = request.args[
        "format"] if "format" in request.args else "geojson"
    if export_format == "csv":
        columns = (export_columns if len(export_columns) > 0 else
                   [db_col.key for db_col in export_view.db_cols])
        return to_csv_resp(file_name, [export_view.as_dict(d) for d in data],
                           columns, ";")
    elif export_format == "geojson":
        results = FeatureCollection([
            export_view.as_geofeature(d, columns=export_columns) for d in data
        ])
        return to_json_resp(results,
                            as_file=True,
                            filename=file_name,
                            indent=4,
                            extension="geojson")
    #MET 21/10/2020 Ajout d'un export medias
    elif export_format == "medias":
        try:
            releve_repository_for_media = ReleveRepository(TRelevesOccurrence)
            q = releve_repository_for_media.get_filtered_query(info_role)

            parameters = request.args

            # Filters
            q = get_query_occtax_filters(parameters, TRelevesOccurrence, q)
            data = q.all()

            user = info_role
            user_cruved = get_or_fetch_user_cruved(session=session,
                                                   id_role=info_role.id_role,
                                                   module_code="OCCTAX")

            #on crée le dossier s'il n'existe pas
            dir_path = str(ROOT_DIR / "backend/static/medias/exports")
            if not os.path.exists(dir_path):
                os.makedirs(dir_path)
            #on le clean
            filemanager.delete_recursively(dir_path)
            featureCollection = []

            zip_path = dir_path + "/" + file_name + ".zip"
            zp_file = zipfile.ZipFile(zip_path, mode="w")
            for n in data:
                releve_cruved = n.get_releve_cruved(user, user_cruved)
                feature = n.get_geofeature(
                    relationships=('t_occurrences_occtax',
                                   'cor_counting_occtax', 'medias'))
                if 'properties' in feature:
                    if 't_occurrences_occtax' in feature['properties']:
                        for occurence in feature['properties'][
                                't_occurrences_occtax']:
                            for counting in occurence['cor_counting_occtax']:
                                if 'medias' in counting:
                                    for media in counting['medias']:
                                        if media['media_path'] is not None:
                                            file_path = str(
                                                ROOT_DIR / "backend/"
                                            ) + "/" + media['media_path']
                                            if os.path.exists(file_path):
                                                zp_file.write(
                                                    file_path,
                                                    os.path.basename(
                                                        file_path))
                                            #copyfile( file_path, dir_path + "/" + os.path.basename(file_path))
                                            #featureCollection.append(file_path)
            zp_file.close()
            return send_from_directory(dir_path,
                                       file_name + ".zip",
                                       as_attachment=True)
        except GeonatureApiError as e:
            message = str(e)
    else:
        try:
            filemanager.delete_recursively(str(ROOT_DIR /
                                               "backend/static/shapefiles"),
                                           excluded_files=[".gitkeep"])
            db_cols = [
                db_col for db_col in export_view.db_cols
                if db_col.key in export_columns
            ]
            dir_path = str(ROOT_DIR / "backend/static/shapefiles")

            export_view.as_shape(db_cols=db_cols,
                                 data=data,
                                 dir_path=dir_path,
                                 file_name=file_name)

            return send_from_directory(dir_path,
                                       file_name + ".zip",
                                       as_attachment=True)

        except GeonatureApiError as e:
            message = str(e)

        return render_template(
            "error.html",
            error=message,
            redirect=current_app.config["URL_APPLICATION"] + "/#/occtax",
        )