def generate_report(title, description): """Generate Excel 1997 file from query. :param title: Query title. :param description: Query description. :return: Response with Excel 1997 attachment. """ df = load_data_frame(request) # Limit the columns to the maximum allowed in Excel 97. max_length = 255 index_len = len(df.index.names) lim_df = df.drop(df.columns[max_length - index_len - 1:len(df.columns) - 1], axis=1) extension = 'xls' engine = 'xlwt' encoding = 'utf-8' content_type = 'application/vnd.ms-excel' # Add content and return response f = NamedTemporaryFile(suffix=extension) ew = ExcelWriter(f.name, engine=engine, encoding=encoding) #print lim_df.to_string() #print f.name lim_df.to_excel(ew) ew.save() #shutil.copyfile(f.name, 'manuel.xls') show_legend = request.REQUEST.get('show_legend', '') table_description = request.REQUEST.get('table_description', '') add_header_and_footer(f.name, title, description, show_legend, table_description) title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] filename = '%s.%s' % (title, extension) # Setup response data = f.read() response = HttpResponse(data) response["Content-Type"] = content_type response["Content-status_code"] = 200 response['Content-Transfer-Encoding'] = 'binary' response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response
def generate_report(title, description, sql): """ Generate Turtle file from query. :param title: Query title. :param description: query description. :param sql: Query sql. :return: Response with Turtle attachment. """ df = load_data_frame(request) multi_index = has_data_frame_multi_level_columns(df) if multi_index: int_df = unpivot(df) else: int_df = df title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] extension = 'ttl' filename = '%s.%s' % (title, extension) res = rdf_report(sql, title, description, data_frame=int_df, rdf_format='turtle') # Setup response content_type = 'text/turtle' response = HttpResponse(res) response["content_type"] = content_type response['Content-Disposition'] = \ 'attachment; filename="{0}"'.format(filename) return response
def generate_report(title, sql): """ Generate Sdmx file from query. :param title: The query title. :param sql: The query sql. :return: Response with Sdmx attachment. """ df = load_data_frame(request) multi_index = has_data_frame_multi_level_columns(df) if multi_index: int_df = unpivot(df) else: int_df = df title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] extension = 'sdmx' filename = '%s.%s' % (title, extension) content_type = 'application/xml' res = sdmx_report(sql, int_df) # Setup response response = HttpResponse(res) response["content_type"] = content_type response['Content-Disposition'] = \ 'attachment; filename="{0}"'.format(filename) return response
def generate_report(title): """ Generate JSON-stat file from query. :return: Response with JSON-stat attachment. """ df = load_data_frame(request) sql = query_sql(request) #df = reconciles_data_frame(df, sql) multi_index = has_data_frame_multi_level_columns(df) if multi_index: int_df = unpivot(df) value = df.columns.levels[0][0] else: int_df = df value = df.columns[len(df.columns)-1] int_df = reconciles_data_frame(int_df, sql) title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] extension = 'json' filename = '%s.%s' % (title, extension) content_type = 'application/json' response = HttpResponse() response["content_type"] = content_type response['Content-Disposition'] = \ 'attachment; filename="{0}"'.format(filename) if value is None: return response val = to_json_stat(int_df, value=value) # Setup response response.write(val) return response
def generate_report(title, description): """Generate Excel 2007 file from query. :param title: Query title. :param description: Query description. :return: Response with Excel 2007 attachment. """ extension = 'xlsx' engine = "openpyxl" encoding = 'utf-8' df = load_data_frame(request) # Add content and return response f = NamedTemporaryFile(suffix=extension) ew = ExcelWriter(f.name, engine=engine, options={'encoding': encoding}) df.to_excel(ew) ew.save() show_legend = request.REQUEST.get('show_legend', '') table_description = request.REQUEST.get('table_description', '') add_header_and_footer(f.name, title, description, show_legend, table_description) # Setup response data = f.read() title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] filename = '%s.%s' % (title, extension) # Setup response content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' response = HttpResponse(data, content_type=content_type) response['Content-Disposition'] = 'attachment; filename="%s"' % filename # Add content and return response return response
def generate_report(title): """ Generate Csv file from query. :param title: The query title. :return: Response with CSV attachment. """ df = load_data_frame(request) title = title.strip().encode("UTF-8").replace(" ", '_') if len(title) > max_length_filename: title = title[:max_length_filename] extension = 'csv' separator = ';' filename = '%s.%s' % (title, extension) content_type = 'text/csv' out_stream = StringIO.StringIO() df.to_csv(out_stream, sep=separator, index=True) # Setup response response = HttpResponse(out_stream.getvalue()) response["Content-Type"] = content_type response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response