def edit(name=None): user_access_token = request.args.get('access_token', request.form.get('access_token')) if not user_access_token and g.user is None: return redirect(url_for('user.login', next=request.path)) def is_format_request(formats): if isinstance(formats, basestring): return request.args.get(formats, None) is not None for format in formats: if is_format_request(format): return True return False def is_data_request(): return is_format_request(['gwiz', 'json', 'csv', 'html', 'gwiz_json']) execute_sql = request.method == 'POST' or is_data_request() query = None if request.method == 'POST': def extract_meta_var_fields(): index = 0 meta = [] while True: name = request.form.get('var-name%d' % index) if not name: return meta type = request.form.get('var-type%d' % index, 'string') default = request.form.get('var-default%d' % index, 'string') meta.append((name, {'default': default, 'type': type})) index += 1 def vars_from_request(meta_vars, empty_vars): vars = [] for name, options in meta_vars: value = request.form.get(name) if value: value = convert_value(value, options.get('type')) elif not empty_vars: continue vars.append((name, value)) return vars def get_editors(): editors = request.form.getlist('editors') if editors: users = User.get_by_username(editors) return users return [] meta_vars = extract_meta_var_fields() sql = request.form['sql'] name = request.form.get('query-name') if name == 'new': name = None if not name and g.user is None: return redirect(url_for('user.login', next=request.path)) connection_string = request.form.get('connection-string') connection = ConnectionString.find(connection_string, True) editors = get_editors() if name and request.form.get('user-action') == 'Save': if g.user is None: return redirect(url_for('user.login', next=request.path)) query, created = save(name, sql, meta_vars, connection, editors) if created: full_vars = vars_from_request(meta_vars, False) return redirect(url_for('.edit', name = name, **dict(full_vars))) vars = vars_from_request(meta_vars, True) else: if not name: return redirect(url_for('.new')) vars = [] for key, value in request.args.iteritems(): if not value: continue vars.append((key, value)) query = Query.find(name, access_token=user_access_token) if not query: return redirect(url_for('.new')) sql = query.sql meta_vars = query.meta_vars connection = query.connection editors = query.editors data_table = None error = None json_data = None access_token = None if execute_sql: transform = request.args.get('transform', None) if not transform: transform = request.args.get('transformer', None) try: json_data = None if connection: if connection.url.startswith('google://') or connection.url.startswith('http://') or connection.url.startswith('https://'): description, data, columns_order = query_google_data_source(connection, sql, meta_vars, vars) else: description, data, columns_order = query_execute_sql(connection, sql, meta_vars, vars) if transform: data = Transformer.execute(transform, data, True) if is_format_request('json'): json_data = json.dumps(data, default=handle_datetime) elif len(data) > 0: data_table = data_to_datatable(description, data) json_data = data_table.ToJSon(columns_order=columns_order) if not json_data: json_data = json.dumps([]) data_table = None except Exception, ex: logging.exception("Failed to execute query %s", ex) error = str(ex) if is_format_request('gwiz'): if error: return Response(json.dumps({"error": error}), mimetype='application/json') return Response(data_table.ToJSonResponse(columns_order=columns_order), mimetype='application/json') if is_format_request('gwiz_json'): if error: return Response(json.dumps({"error": error}), mimetype='application/json') if data_table: return Response(data_table.ToJSon(columns_order=columns_order), mimetype='application/json') else: return Response(json.dumps({"info": 'No results returned'}), mimetype='application/json') if is_format_request('json'): if error: return Response(json.dumps({"error": error}), mimetype='application/json') return Response(json_data, mimetype='application/json') elif is_format_request('html'): return Response(data_table.ToHtml(columns_order=columns_order)) elif is_format_request('csv'): return Response(data_table.ToCsv(columns_order=columns_order), mimetype='text/csv')
connection.url.startswith('http://') or \ connection.url.startswith('https://'): description, data, columns_order = query_google_data_source( connection, sql, fields_meta_vars, query_vars) else: description, data, columns_order = query_execute_sql( connection, sql, fields_meta_vars, query_vars) if transform: data = Transformer.execute(transform, data, True) if is_format_request('json'): json_data = json.dumps(data, default=handle_datetime) elif len(data) > 0: data_table = data_to_datatable(description, data) json_data = data_table.ToJSon(columns_order=columns_order) if not json_data: json_data = json.dumps([]) data_table = None except Exception, ex: logging.exception("Failed to execute query %s", ex) error = str(ex)