Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kml selection exports #5300

Open
wants to merge 13 commits into
base: production
Choose a base branch
from
24 changes: 19 additions & 5 deletions specifyweb/frontend/js_src/lib/components/QueryBuilder/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export function QueryExportButtons({
function doQueryExport(
url: string,
delimiter: string | undefined,
bom: boolean | undefined
bom: boolean | undefined,
selectedRows: ReadonlySet<number> | undefined
): void {
if (typeof getQueryFieldRecords === 'function')
queryResource.set('fields', getQueryFieldRecords());
Expand All @@ -70,6 +71,8 @@ export function QueryExportButtons({
generateMappingPathPreview(baseTableName, mappingPath)
),
recordSetId,
selectedRows:
selectedRows === undefined ? undefined : Array.from(selectedRows),
delimiter,
bom,
}),
Expand All @@ -93,7 +96,8 @@ export function QueryExportButtons({
*Will be only called if query is not distinct,
*selection not enabled when distinct selected
*/
async function exportSelected(): Promise<void> {

async function exportCsvSelected(): Promise<void> {
const name = `${
queryResource.isNew()
? `${queryText.newQueryName()} ${genericTables[baseTableName].label}`
Expand Down Expand Up @@ -158,8 +162,13 @@ export function QueryExportButtons({
showConfirmation={showConfirmation}
onClick={(): void => {
selectedRows.size === 0
? doQueryExport('/stored_query/exportcsv/', separator, utf8Bom)
: exportSelected().catch(softFail);
? doQueryExport(
'/stored_query/exportcsv/',
separator,
utf8Bom,
undefined
)
: exportCsvSelected().catch(softFail);
}}
>
{queryText.createCsv()}
Expand All @@ -171,7 +180,12 @@ export function QueryExportButtons({
showConfirmation={showConfirmation}
onClick={(): void =>
hasLocalityColumns(fields)
? doQueryExport('/stored_query/exportkml/', undefined, undefined)
? doQueryExport(
'/stored_query/exportkml/',
undefined,
undefined,
selectedRows.size === 0 ? undefined : selectedRows
)
: setState('warning')
}
>
Expand Down
8 changes: 6 additions & 2 deletions specifyweb/stored_queries/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def do_export(spquery, collection, user, filename, exporttype, host):
distinct=spquery['selectdistinct'], delimiter=spquery['delimiter'], bom=spquery['bom'])
elif exporttype == 'kml':
query_to_kml(session, collection, user, tableid, field_specs, path, spquery['captions'], host,
recordsetid=recordsetid, strip_id=False)
recordsetid=recordsetid, strip_id=False, selected_rows=spquery.get('selectedrows', None))
message_type = 'query-export-to-kml-complete'

Message.objects.create(user=user, content=json.dumps({
Expand Down Expand Up @@ -235,7 +235,7 @@ def row_has_geocoords(coord_cols, row):


def query_to_kml(session, collection, user, tableid, field_specs, path, captions, host,
recordsetid=None, strip_id=False):
recordsetid=None, strip_id=False, selected_rows=None):
"""Build a sqlalchemy query using the QueryField objects given by
field_specs and send the results to a kml file at the given
file path.
Expand All @@ -244,6 +244,10 @@ def query_to_kml(session, collection, user, tableid, field_specs, path, captions
"""
set_group_concat_max_len(session.connection())
query, __ = build_query(session, collection, user, tableid, field_specs, recordsetid, replace_nulls=True)
if selected_rows:
model = models.models_by_tableid[tableid]
id_field = getattr(model, model._id)
query = query.filter(id_field.in_(selected_rows))

logger.debug('query_to_kml starting')

Expand Down
Loading