def export(info_role): export_view_name = blueprint.config["export_view_name"] export_geom_column = blueprint.config["export_geom_columns_name"] export_id_column_name = blueprint.config["export_id_column_name"] export_columns = blueprint.config["export_columns"] export_srid = blueprint.config["export_srid"] export_view = GenericTable( export_view_name, "pr_occtax", export_geom_column, 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) 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: 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", )
def export(info_role): filters = dict(request.args) if 'limit' in filters: result_limit = filters.pop('limit')[0] else: result_limit = current_app.config['SYNTHESE']['NB_MAX_OBS_EXPORT'] export_format = filters.pop('export_format')[0] allowed_datasets = TDatasets.get_user_datasets(info_role) q = DB.session.query(VSyntheseForExport) q = synthese_query.filter_query_all_filters(VSyntheseForExport, q, filters, info_role, allowed_datasets) q = q.order_by(VSyntheseForExport.date_min.desc()) data = q.limit(result_limit) file_name = datetime.datetime.now().strftime('%Y_%m_%d_%Hh%Mm%S') file_name = filemanager.removeDisallowedFilenameChars(file_name) formated_data = [d.as_dict_ordered() for d in data] export_columns = formated_data[0].keys() if export_format == 'csv': return to_csv_resp( file_name, formated_data, separator=';', columns=export_columns, ) elif export_format == 'geojson': results = FeatureCollection(formated_data) return to_json_resp(results, as_file=True, filename=file_name, indent=4) else: filemanager.delete_recursively(str(ROOT_DIR / 'backend/static/shapefiles'), excluded_files=['.gitkeep']) dir_path = str(ROOT_DIR / 'backend/static/shapefiles') FionaShapeService.create_shapes_struct( db_cols=VSyntheseForExport.db_cols, srid=current_app.config['LOCAL_SRID'], dir_path=dir_path, file_name=file_name, col_mapping=current_app.config['SYNTHESE']['EXPORT_COLUMNS']) for row in data: geom = row.the_geom_local row_as_dict = row.as_dict_ordered() FionaShapeService.create_feature(row_as_dict, geom) FionaShapeService.save_and_zip_shapefiles() return send_from_directory(dir_path, file_name + '.zip', as_attachment=True)
def export(info_role): export_view_name = blueprint.config["export_view_name"] export_geom_column = blueprint.config["export_geom_columns_name"] export_id_column_name = blueprint.config["export_id_column_name"] export_columns = blueprint.config["export_columns"] export_srid = blueprint.config["export_srid"] export_view = GenericTable(export_view_name, "pr_occtax", export_geom_column, 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) 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: 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", )
def schedule_export_filename(export): """ Génération du nom statique du fichier d'export programmé """ return '{}'.format(removeDisallowedFilenameChars(export.get('label')))
def export_filename(export): """ Génération du nom horodaté du fichier d'export """ return '{}_{}'.format(time.strftime("%Y-%m-%d_%H-%M-%S"), removeDisallowedFilenameChars(export.get('label')))
def export_all_habitats( info_role, export_format="csv", ): """ Download all stations The route is in post to avoid a too large query string .. :quickref: Occhab; """ data = request.get_json() export_view = GenericTableGeo( tableName="v_export_sinp", schemaName="pr_occhab", engine=DB.engine, geometry_field="geom_local", srid=current_app.config["LOCAL_SRID"], ) file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) db_cols_for_shape = [] columns_to_serialize = [] for db_col in export_view.db_cols: if db_col.key in blueprint.config["EXPORT_COLUMS"]: if db_col.key != "geometry": db_cols_for_shape.append(db_col) columns_to_serialize.append(db_col.key) results = ( DB.session.query(export_view.tableDef) .filter(export_view.tableDef.columns.id_station.in_(data["idsStation"])) .limit(blueprint.config["NB_MAX_EXPORT"]) ) if export_format == "csv": formated_data = [export_view.as_dict(d, fields=[]) for d in results] return to_csv_resp( file_name, formated_data, separator=";", columns=columns_to_serialize ) elif export_format == "geojson": features = [] for r in results: features.append( Feature( geometry=json.loads(r.geojson), properties=export_view.as_dict(r, fields=columns_to_serialize), ) ) return to_json_resp( FeatureCollection(features), as_file=True, filename=file_name, indent=4 ) else: try: dir_name, file_name = export_as_geo_file( export_format=export_format, export_view=export_view, db_cols=db_cols_for_shape, geojson_col=None, data=results, 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"] + "/#/" + blueprint.config["MODULE_URL"], )
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", )
def export_observations_web(info_role): """Optimized route for observations web export. .. :quickref: Synthese; This view is customisable by the administrator Some columns are mandatory: id_synthese, geojson and geojson_local to generate the exported files POST parameters: Use a list of id_synthese (in POST parameters) to filter the v_synthese_for_export_view :query str export_format: str<'csv', 'geojson', 'shapefiles'> """ params = request.args # set default to csv export_format = "csv" export_view = GenericTableGeo( tableName="v_synthese_for_export", schemaName="gn_synthese", engine=DB.engine, geometry_field=None, srid=current_app.config["LOCAL_SRID"], ) if "export_format" in params: export_format = params["export_format"] # get list of id synthese from POST id_list = request.get_json() db_cols_for_shape = [] columns_to_serialize = [] # loop over synthese config to get the columns for export for db_col in export_view.db_cols: if db_col.key in current_app.config["SYNTHESE"]["EXPORT_COLUMNS"]: db_cols_for_shape.append(db_col) columns_to_serialize.append(db_col.key) q = DB.session.query(export_view.tableDef).filter( export_view.tableDef.columns[ current_app.config["SYNTHESE"]["EXPORT_ID_SYNTHESE_COL"] ].in_(id_list) ) # 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 ] 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 q = synthese_query.filter_query_with_cruved( export_view.tableDef, q, info_role, id_synthese_column=current_app.config["SYNTHESE"]["EXPORT_ID_SYNTHESE_COL"], id_dataset_column=current_app.config["SYNTHESE"]["EXPORT_ID_DATASET_COL"], observers_column=current_app.config["SYNTHESE"]["EXPORT_OBSERVERS_COL"], id_digitiser_column=current_app.config["SYNTHESE"][ "EXPORT_ID_DIGITISER_COL" ], with_generic_table=True, ) results = q.limit(current_app.config["SYNTHESE"]["NB_MAX_OBS_EXPORT"]) file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) # columns = [db_col.key for db_col in export_view.db_cols] if export_format == "csv": formated_data = [ export_view.as_dict(d, columns=columns_to_serialize) for d in results ] return to_csv_resp( file_name, formated_data, separator=";", columns=columns_to_serialize ) elif export_format == "geojson": features = [] for r in results: geometry = ast.literal_eval( getattr( r, current_app.config["SYNTHESE"]["EXPORT_GEOJSON_4326_COL"]) ) feature = Feature( geometry=geometry, properties=export_view.as_dict( r, columns=columns_to_serialize), ) features.append(feature) results = FeatureCollection(features) return to_json_resp(results, as_file=True, filename=file_name, indent=4) else: try: filemanager.delete_recursively( str(ROOT_DIR / "backend/static/shapefiles"), excluded_files=[".gitkeep"] ) dir_path = str(ROOT_DIR / "backend/static/shapefiles") export_view.as_shape( db_cols=db_cols_for_shape, data=results, geojson_col=current_app.config["SYNTHESE"]["EXPORT_GEOJSON_LOCAL_COL"], 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"] + "/#/synthese", )
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", )
def test(info_role): """ Deprecated """ file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) releve_repository = ReleveRepository(TRelevesOccurrence) q = releve_repository.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( str(ROOT_DIR / "backend/static/medias/exports")) 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) #return {"items": media,} # if feature['properties']['t_occurrences_occtax']: # for occurence in feature['properties']['t_occurrences_occtax'] : # featureCollection.append(occurence) #featureCollection.append(feature) zp_file.close() return send_from_directory(dir_path, file_name + ".zip", as_attachment=True)
def export_sinp(info_role): """ Return the data (CSV) at SINP from pr_occtax.export_occtax_sinp view If no paramater return all the dataset allowed of the user params: - id_dataset : integer - uuid_dataset: uuid """ viewSINP = GenericTable('export_occtax_dlb', 'pr_occtax', None) q = DB.session.query(viewSINP.tableDef) params = request.args allowed_datasets = TDatasets.get_user_datasets(info_role) # if params in empty and user not admin, # get the data off all dataset allowed if not params.get('id_dataset') and not params.get('uuid_dataset'): if info_role.tag_object_code != '3': allowed_uuid = (str(TDatasets.get_uuid(id_dataset)) for id_dataset in allowed_datasets) q = q.filter(viewSINP.tableDef.columns.jddId.in_(allowed_uuid)) # filter by dataset id or uuid else: if 'id_dataset' in params: id_dataset = int(params['id_dataset']) uuid_dataset = TDatasets.get_uuid(id_dataset) elif 'uuid_dataset' in params: id_dataset = TDatasets.get_id(params['uuid_dataset']) uuid_dataset = params['uuid_dataset'] # if data_scope 1 or 2, check if the dataset requested is allorws if (info_role.tag_object_code == '1' or info_role.tag_object_code == '2'): if id_dataset not in allowed_datasets: raise InsufficientRightsError( ('User "{}" cannot export dataset no "{}').format( info_role.id_role, id_dataset), 403) elif info_role.tag_object_code == '1': # join on TCounting, TOccurrence, Treleve and corRoleOccurrence # to get users q = q.outerjoin( CorCountingOccurrence, viewSINP.tableDef.columns.permId == CorCountingOccurrence.unique_id_sinp_occtax ).join( TOccurrencesOccurrence, CorCountingOccurrence.id_occurrence_occtax == TOccurrencesOccurrence.id_occurrence_occtax).join( TRelevesOccurrence, TOccurrencesOccurrence.id_releve_occtax == TRelevesOccurrence.id_releve_occtax).outerjoin( corRoleRelevesOccurrence, TRelevesOccurrence.id_releve_occtax == corRoleRelevesOccurrence.columns.id_releve_occtax) q = q.filter( or_( corRoleRelevesOccurrence.columns.id_role == info_role.id_role, TRelevesOccurrence.id_digitiser == info_role.id_role)) q = q.filter(viewSINP.tableDef.columns.jddId == str(uuid_dataset)) data = q.all() export_columns = blueprint.config['export_columns'] file_name = datetime.datetime.now().strftime('%Y-%m-%d-%Hh%Mm%S') return (filemanager.removeDisallowedFilenameChars(file_name), [viewSINP.as_dict(d) for d in data], export_columns, ';')
def export(info_role): export_view_name = blueprint.config['export_view_name'] export_geom_column = blueprint.config['export_geom_columns_name'] export_id_column_name = blueprint.config['export_id_column_name'] export_columns = blueprint.config['export_columns'] export_srid = blueprint.config['export_srid'] export_view = GenericTable(export_view_name, 'pr_occtax', export_geom_column, 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) 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) 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")
def export_all_habitats(info_role, export_format='csv',): """ Download all stations The route is in post to avoid a too large query string .. :quickref: Occhab; """ data = request.get_json() export_view = GenericTable( tableName="v_export_sinp", schemaName="pr_occhab", geometry_field=None, srid=current_app.config["LOCAL_SRID"], ) file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) db_cols_for_shape = [] columns_to_serialize = [] for db_col in export_view.db_cols: if db_col.key in blueprint.config['EXPORT_COLUMS']: if db_col.key != 'geometry': db_cols_for_shape.append(db_col) columns_to_serialize.append(db_col.key) results = DB.session.query(export_view.tableDef).filter( export_view.tableDef.columns.id_station.in_(data['idsStation']) ).limit( blueprint.config['NB_MAX_EXPORT'] ) if export_format == 'csv': formated_data = [ export_view.as_dict(d, columns=[]) for d in results ] return to_csv_resp( file_name, formated_data, separator=";", columns=columns_to_serialize ) elif export_format == 'geojson': features = [] for r in results: features.append( Feature( geometry=json.loads(r.geojson), properties=export_view.as_dict( r, columns=columns_to_serialize) ) ) return to_json_resp( FeatureCollection(features), as_file=True, filename=file_name, indent=4 ) else: try: filemanager.delete_recursively( str(ROOT_DIR / "backend/static/shapefiles"), excluded_files=[".gitkeep"] ) dir_path = str(ROOT_DIR / "backend/static/shapefiles") export_view.as_shape( db_cols=db_cols_for_shape, data=results, geojson_col="geojson", 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"] + "/#/" + blueprint.config['MODULE_URL'], )
def export_observations_web(info_role): """ Optimized route for observations web export This view is customisable by the administrator Some columns arer mandatory: id_sythese, geojson and geojson_local to generate the exported files Parameters : Via POST: Use a list of id_synthese (in POST parameters) to filter the v_synthese_for_export_view Via GET: 'export_format' str<'csv', 'geojson', 'shapefiles'> """ params = request.args # set default to csv export_format = "csv" export_view = GenericTable( "v_synthese_for_export", "gn_synthese", "the_geom_local", current_app.config["LOCAL_SRID"], ) if "export_format" in params: export_format = params["export_format"] # get list of id synthese from POST id_list = request.get_json() db_cols_for_shape = [] columns_to_serialize = [] # loop over synthese config to get the columns for export for db_col in export_view.db_cols: if db_col.key in current_app.config["SYNTHESE"]["EXPORT_COLUMNS"]: db_cols_for_shape.append(db_col) columns_to_serialize.append(db_col.key) q = DB.session.query(export_view.tableDef).filter( export_view.tableDef.columns.idSynthese.in_(id_list) ) # 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 ] 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 q = synthese_query.filter_query_with_cruved( export_view.tableDef, q, info_role, id_synthese_column=current_app.config["SYNTHESE"]["EXPORT_ID_SYNTHESE_COL"], id_dataset_column=current_app.config["SYNTHESE"]["EXPORT_ID_DATASET_COL"], observers_column=current_app.config["SYNTHESE"]["EXPORT_OBSERVERS_COL"], id_digitiser_column=current_app.config["SYNTHESE"][ "EXPORT_ID_DIGITISER_COL" ], with_generic_table=True, ) results = q.limit(current_app.config["SYNTHESE"]["NB_MAX_OBS_EXPORT"]) file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) # columns = [db_col.key for db_col in export_view.db_cols] if export_format == "csv": formated_data = [ export_view.as_dict(d, columns=columns_to_serialize) for d in results ] return to_csv_resp( file_name, formated_data, separator=";", columns=columns_to_serialize ) elif export_format == "geojson": features = [] for r in results: geometry = ast.literal_eval( getattr(r, current_app.config["SYNTHESE"]["EXPORT_GEOJSON_4326_COL"]) ) feature = Feature( geometry=geometry, properties=export_view.as_dict(r, columns=columns_to_serialize), ) features.append(feature) results = FeatureCollection(features) return to_json_resp(results, as_file=True, filename=file_name, indent=4) else: try: filemanager.delete_recursively( str(ROOT_DIR / "backend/static/shapefiles"), excluded_files=[".gitkeep"] ) dir_path = str(ROOT_DIR / "backend/static/shapefiles") export_view.as_shape( db_cols=db_cols_for_shape, data=results, geojson_col=current_app.config["SYNTHESE"]["EXPORT_GEOJSON_LOCAL_COL"], 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"] + "/#/synthese", )
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", )
def export_filename(export): """ Génération du nom du fichier d'export """ return '{}_{}'.format(removeDisallowedFilenameChars(export.get('label')), datetime.now().strftime('%Y_%m_%d_%Hh%Mm%S'))
def export_observations_web(info_role): """Optimized route for observations web export. .. :quickref: Synthese; This view is customisable by the administrator Some columns are mandatory: id_synthese, geojson and geojson_local to generate the exported files POST parameters: Use a list of id_synthese (in POST parameters) to filter the v_synthese_for_export_view :query str export_format: str<'csv', 'geojson', 'shapefiles', 'gpkg'> """ params = request.args export_format = params.get("export_format", "csv") # Test export_format if not export_format in current_app.config["SYNTHESE"]["EXPORT_FORMAT"]: raise BadRequest("Unsupported format") # set default to csv export_view = GenericTableGeo( tableName="v_synthese_for_export", schemaName="gn_synthese", engine=DB.engine, geometry_field=None, srid=current_app.config["LOCAL_SRID"], ) # get list of id synthese from POST id_list = request.get_json() db_cols_for_shape = [] columns_to_serialize = [] # loop over synthese config to get the columns for export for db_col in export_view.db_cols: if db_col.key in current_app.config["SYNTHESE"]["EXPORT_COLUMNS"]: db_cols_for_shape.append(db_col) columns_to_serialize.append(db_col.key) query = select([export_view.tableDef]).where(export_view.tableDef.columns[ current_app.config["SYNTHESE"]["EXPORT_ID_SYNTHESE_COL"]].in_(id_list)) synthese_query_class = SyntheseQuery( export_view.tableDef, query, {}, id_synthese_column=current_app.config["SYNTHESE"] ["EXPORT_ID_SYNTHESE_COL"], id_dataset_column=current_app.config["SYNTHESE"] ["EXPORT_ID_DATASET_COL"], observers_column=current_app.config["SYNTHESE"] ["EXPORT_OBSERVERS_COL"], id_digitiser_column=current_app.config["SYNTHESE"] ["EXPORT_ID_DIGITISER_COL"], with_generic_table=True, ) # 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] if cruved["R"] > cruved["E"]: synthese_query_class.filter_query_with_cruved(info_role) results = DB.session.execute( synthese_query_class.query.limit( current_app.config["SYNTHESE"]["NB_MAX_OBS_EXPORT"])) file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") file_name = filemanager.removeDisallowedFilenameChars(file_name) if export_format == "csv": formated_data = [ export_view.as_dict(d, columns=columns_to_serialize) for d in results ] return to_csv_resp(file_name, formated_data, separator=";", columns=columns_to_serialize) elif export_format == "geojson": features = [] for r in results: geometry = json.loads( getattr( r, current_app.config["SYNTHESE"]["EXPORT_GEOJSON_4326_COL"])) feature = Feature( geometry=geometry, properties=export_view.as_dict(r, columns=columns_to_serialize), ) features.append(feature) results = FeatureCollection(features) return to_json_resp(results, as_file=True, filename=file_name, indent=4) else: try: dir_name, file_name = export_as_geo_file( export_format=export_format, export_view=export_view, db_cols=db_cols_for_shape, geojson_col=current_app.config["SYNTHESE"] ["EXPORT_GEOJSON_LOCAL_COL"], data=results, 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"] + "/#/synthese", )