def filter_row_count(filter_pos): query = Query.from_dict(flask.request.json) if current_user_has_permission(query): return flask.jsonify(query.filter_row_count(filter_pos)) else: return flask.make_response(acl.inline_permission_denied_message(), 403)
def preview(): query = Query.from_dict(flask.request.json['query']) def header(column: Column): if column.sortable(): if query.sort_column_name == column.column_name and query.sort_order == 'ASC': icon = _.span(class_=('fa fa-sort-amount-asc'))[''] elif query.sort_column_name == column.column_name and query.sort_order == 'DESC': icon = _.span(class_=('fa fa-sort-amount-desc'))[''] else: icon = '' return _.a(href="#", name=flask.escape( column.column_name))[icon, ' ', flask.escape(column.column_name)] else: return flask.escape(column.column_name) if current_user_has_permission(query): rows = [ _render_preview_row(query, row) for row in query.run( limit=flask.request.json['limit'], offset=flask.request.json['offset'], include_personal_data=acl.current_user_has_permission( personal_data_acl_resource)) ] else: rows = _.tr[_.td(colspan=len(query.column_names))[ acl.inline_permission_denied_message()]] return str( bootstrap.table(headers=[ header(query.data_set.columns[c]) for c in query.column_names ], rows=rows))
def save(): query = Query.from_dict(flask.request.json) if current_user_has_permission(query): query.save() flask.flash(f'Saved query ' + query.query_id, 'success') return flask.jsonify( flask.url_for('data_sets.data_set_page', data_set_id=query.data_set.id, query_id=query.query_id)) else: flask.make_response( f'Not enough permissions to save query "{query.query_id}"', 403)
def data_set_preview(data_set_id): query = Query(data_set_id=data_set_id) if not current_user_has_permission(query): return acl.inline_permission_denied_message() else: if current_user_has_permission(query): rows = [ _render_preview_row(query, row) for row in query.run( limit=7, offset=0, include_personal_data=acl.current_user_has_permission( personal_data_acl_resource)) ] else: rows = _.tr[_.td(colspan=len(query.column_names))[ acl.inline_permission_denied_message()]] return str( bootstrap.table(headers=[ flask.escape(column_name) for column_name in query.column_names ], rows=rows))
def initialize_query(): data_set_id = flask.request.json['data_set_id'] query_id = flask.request.json['query_id'] query_dict = flask.request.json['query'] if query_dict: query = Query.from_dict(query_dict) elif query_id: query = Query.load(query_id, data_set_id) else: query = Query(data_set_id=data_set_id) return flask.jsonify({ 'query': query.to_dict(), 'all_columns': [column.to_dict() for column in query.data_set.columns.values()], 'row_count': query.data_set.row_count(), 'data_set_name': query.data_set.name })
def download_csv(): query = Query.from_dict(json.loads(flask.request.form['query'])) if not current_user_has_permission(query): return flask.abort(403, 'Not enough permissions to download this data set') else: file_name = query.data_set_id + ('-' + query.query_id if query.query_id else '') \ + '-' + datetime.date.today().isoformat() + '.csv' response = flask.make_response( query.as_csv( flask.request.form['delimiter'], flask.request.form['decimal-mark'], acl.current_user_has_permission(personal_data_acl_resource))) response.headers['Content-type'] = 'text/csv; charset = utf-8' response.headers[ 'Content-disposition'] = f'attachment; filename="{file_name}"' return response
def distribution_chart(pos: int): query = Query.from_dict(flask.request.json) column = list(query.data_set.columns.values())[pos] if not current_user_has_permission(query): return flask.make_response(acl.inline_permission_denied_message(), 403) elif column.column_name in query.data_set.personal_data_column_names \ and not acl.current_user_has_permission(personal_data_acl_resource): return flask.make_response( acl.inline_permission_denied_message('Restricted personal data'), 403) else: if column.type == 'number': data = query.number_distribution(column.column_name) elif column.type == 'text': data = query.text_distribution(column.column_name) elif column.type == 'text[]': data = query.text_array_distribution(column.column_name) elif column.type == 'date': data = query.date_distribution(column.column_name) else: data = [] return flask.jsonify({'column': column.to_dict(), 'data': data})