def create_report(self): connection = model.Connection.get(model.Connection.id == self.connection_id) filename = self.get_json_path() if self.sql.strip() == '': result = DBM(connection.name).quick_query("SELECT 1") else: result = DBM(connection.name).quick_query(self.sql) self.nb_rows = 0 with open(filename, 'wb') as jsonfile: je = JsonEncoder() jsonfile.write(je.encode(result)) self.last_run = datetime.datetime.now() self.save()
def __init__(self, pagepath, page_id): WebPage.__init__(self, pagepath) self.main_template = "chart_creation.tpl" self.request_params = dict(request.params) self.chart = None self.charts = ChartInfo.select() self.table_fields = ChartInfo._meta.fields self.action = None if self.request_params.has_key("action"): self.action = self.request_params['action'] if self.action == "create_all": json_return = {} for chart in self.charts: chart.create_report() json_return.update(chart.get_json_repr()) Je = JsonEncoder() self.special_return = Je.encode(json_return) elif self.action == 'new': self.chart = ChartInfo.create(name='PlaceHolderName', connection_id=1) self.chart.save() redirect(self.pagepath + "?chart_id=" + str(self.chart.id) +'&action=edit') elif self.request_params.has_key('chart_id'): for chart in self.charts: if str(chart.id)==self.request_params['chart_id']: self.chart = chart if self.action == 'edit' or self.action == 'new': self.chart.create_report_if_needed() self.highchart = HighChart(self, self.chart) elif self.action == 'update': self.chart.sql = self.request_params['sql'] self.chart.cache_duration = self.request_params['cache_duration'] self.chart.name = self.request_params['name'] self.chart.description = self.request_params['description'] self.chart.group = self.request_params['group'] self.chart.save() self.chart.create_report() redirect(self.pagepath + "?chart_id=" + str(self.chart.id) +'&action=edit' )
def create_report(self): start_time = time.time() if self.source_type == 'SQLquery': query_info = model.SQLquery.get(model.SQLquery.id == self.source_id) connection = model.Connection.get(model.Connection.id == query_info.connection_id) filename = os.path.join(Config().cache_folder, self.name + ".csv") json_filename = os.path.join(Config().cache_folder, self.name + ".json") json_array = [] result = DBM(connection.name).query(query_info.sql) self.nb_rows = 0 with open(filename, 'wb') as csvfile: csv_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) csv_writer.writerow(result.columns) csvfile.flush() for index, row in enumerate(result.rows): row = list(row) csv_writer.writerow(row) json_array.append(row) self.nb_rows = index + 1 csvfile.flush() json_file = open(json_filename, "wb") Je = JsonEncoder() json_file.write(Je.encode({ "aaData": json_array })) self.last_run = datetime.datetime.now() self.last_duration = Humanize.pretty_delta(time.time() - start_time) self.save()
def __init__(self, pagepath, page_id): WebPage.__init__(self, pagepath) self.report = None self.main_template = "report_creation.tpl" self.request_params = dict(request.params) self.reports = ReportInfo.select() self.table_fields = ReportInfo._meta.fields self.action = None if self.request_params.has_key("action"): self.action = self.request_params['action'] if self.action == "create_all": json_return = {} for report in self.reports: report.create_report() json_return.update(report.get_json_repr()) Je = JsonEncoder() self.special_return = Je.encode(json_return) elif self.action == 'new': self.sql_query = SQLquery.create(sql='select 1', connection_id=1) self.report = ReportInfo.create(name='PlaceHolderName', description='', source_type='SQLquery', \ source_id=self.sql_query.id) redirect(self.pagepath + "?report=PlaceHolderName&action=edit" ) elif self.request_params.has_key('report'): self.report = [report for report in self.reports if report.name==self.request_params['report']][0] self.sql_query = SQLquery.get(SQLquery.id == self.report.source_id) if self.action == 'edit' or self.action == 'new': self.current_report = self.report self.report.create_report_if_needed() self.last_run = report.last_run self.datatable = Datatables(self, self.report.name, self.report.get_header(), self.report.row_callback) # # self.header = report.get_header() # self.css.append(Css("DT_bootstrap.css")) # self.foot_js.append(Js("jquery.dataTables.min.js")) # self.foot_js.append(Js("DT_bootstrap.js")) # # self.header = report.get_header() # self.id_table = random_string(10) # # self.drop_down_nb = True # self.drop_down_values = [[20, 50, 100, -1], [20, 50, 100, "All"]] # self.drop_down_text = "records per page" # self.global_search = True # self.global_search_text = "Search all columns:" # self.pagination = True # self.show_nb_records = True # self.records_per_page = 20 # self.column_search = True # self.foot_js_templates = [Template("datatables_js_ready", self)] # self.ajax_url = "/cache/" + str(report.name) + ".json" # self.csv_url = "/cache/" + str(report.name) + ".csv" elif self.action == 'update': self.sql_query.sql = self.request_params['sql_query_sql'] self.sql_query.save() self.report.cache_duration = self.request_params['cache_duration'] self.report.name = self.request_params['name'] self.report.description = self.request_params['description'] self.report.row_callback = self.request_params['row_callback'] self.report.report_type = self.request_params['report_type'] self.report.save() self.report.create_report() redirect(self.pagepath + "?report=" + self.report.name +'&action=edit' )
def get_json(self): Je = JsonEncoder() return Je.encode()