def test_to_csv(example_ledger): types, rows = run_query(example_ledger.all_entries, example_ledger.options, 'balances', numberify=True) assert excel.to_csv(types, rows) types, rows = run_query(example_ledger.all_entries, example_ledger.options, 'select account, tags, date, day', numberify=True) assert excel.to_csv(types, rows)
def test_to_csv(example_ledger): types, rows = run_query( example_ledger.all_entries, example_ledger.options, 'balances', numberify=True) assert excel.to_csv(types, rows) types, rows = run_query( example_ledger.all_entries, example_ledger.options, 'select account, tags, date, day', numberify=True) assert excel.to_csv(types, rows)
def test_to_csv(example_ledger: FavaLedger) -> None: types, rows = run_query( example_ledger.all_entries, example_ledger.options, "balances", numberify=True, ) assert excel.to_csv(types, rows) types, rows = run_query( example_ledger.all_entries, example_ledger.options, "select account, tags, date, day", numberify=True, ) assert excel.to_csv(types, rows)
def query(): name = request.args.get('name', 'query_result') result_format = request.args.get('result_format', 'html') query_string = request.args.get('query_string', '') numberify = bool(result_format != 'html') if not query_string: return render_template('query.html') try: types, rows = g.api.query(query_string, numberify) except Exception as e: return render_template('query.html', error=e) if result_format == 'html': return render_template('query.html', result_types=types, result_rows=rows) else: filename = "{}.{}".format(secure_filename(name.strip()), result_format) if result_format == 'csv': data = to_csv(types, rows) else: if not app.config['HAVE_EXCEL']: abort(501) data = to_excel(types, rows, result_format, query_string) return send_file(data, as_attachment=True, attachment_filename=filename)
def query(): name = request.args.get('name', 'query_result') result_format = request.args.get('result_format', 'html') query_string = request.args.get('query_string', '') numberify = bool(result_format != 'html') if not query_string: return render_template('query.html') try: types, rows = g.api.query(query_string, numberify) except Exception as e: return render_template('query.html', error=e) if result_format == 'html': return render_template('query.html', result_types=types, result_rows=rows) else: filename = "{}.{}".format(secure_filename(name.strip()), result_format) if result_format == 'csv': data = to_csv(types, rows) else: if not app.config['HAVE_EXCEL']: abort(501) data = to_excel(types, rows, result_format, query_string) return send_file( data, as_attachment=True, attachment_filename=filename)
def query_to_file(self, query_string, result_format): """Get query result as file. Arguments: query_string: A string, the query to run. result_format: The file format to save to. Returns: A tuple (name, data), where name is either 'query_result' or the name of a custom query if the query string is 'run name_of_query'. ``data`` contains the file contents. Raises: FavaAPIException: If the result format is not supported or the query failed. """ name = "query_result" try: statement = self.parser.parse(query_string) except query_parser.ParseError as exception: raise FavaAPIException(str(exception)) if statement.__class__.__name__ == "RunCustom": name = statement.query_name try: query = next( (query for query in self.queries if query.name == name) ) except StopIteration: raise FavaAPIException('Query "{}" not found.'.format(name)) query_string = query.query_string try: types, rows = run_query( self.ledger.all_entries, self.ledger.options, query_string, numberify=True, ) except ( query_compile.CompilationError, query_parser.ParseError, ) as exception: raise FavaAPIException(str(exception)) if result_format == "csv": data = to_csv(types, rows) else: if not HAVE_EXCEL: raise FavaAPIException("Result format not supported.") data = to_excel(types, rows, result_format, query_string) return name, data
def query_to_file(self, query_string, result_format): """Get query result as file. Arguments: query_string: A string, the query to run. result_format: The file format to save to. Returns: A tuple (name, data), where name is either 'query_result' or the name of a custom query if the query string is 'run name_of_query'. ``data`` contains the file contents. Raises: FavaAPIException: If the result format is not supported or the query failed. """ name = 'query_result' try: statement = self.parser.parse(query_string) except query_parser.ParseError as exception: raise FavaAPIException(str(exception)) if statement.__class__.__name__ == 'RunCustom': name = statement.query_name try: query = next((query for query in self.queries if query.name == name)) except StopIteration: raise FavaAPIException('Query "{}" not found.'.format(name)) query_string = query.query_string try: types, rows = run_query( self.ledger.all_entries, self.ledger.options, query_string, numberify=True) except (query_compile.CompilationError, query_parser.ParseError) as exception: raise FavaAPIException(str(exception)) if result_format == 'csv': data = to_csv(types, rows) else: if not HAVE_EXCEL: raise FavaAPIException('Result format not supported.') data = to_excel(types, rows, result_format, query_string) return name, data
def download_query(result_format, name='query_result'): query_string = request.args.get('query_string', '') try: types, rows = g.api.query(query_string, numberify=True) except (query_compile.CompilationError, query_parser.ParseError): abort(400) filename = "{}.{}".format(secure_filename(name.strip()), result_format) if result_format == 'csv': data = to_csv(types, rows) else: if not app.config['HAVE_EXCEL']: abort(501) data = to_excel(types, rows, result_format, query_string) return send_file(data, as_attachment=True, attachment_filename=filename)
def test_to_csv(example_api): types, rows = example_api.query('balances', numberify=True) assert to_csv(types, rows) types, rows = example_api.query('select account, tags, date, day', numberify=True) assert to_csv(types, rows)