sean@1: #!/usr/bin/env python3 sean@1: sean@1: """ Display previously saved issues from a database on webpage via CGI. sean@1: sean@1: author: Sascha L. Teichmann sean@1: author: Bernhard Reiter sean@1: author: Sean Engelhardt sean@1: sean@1: (c) 2010,2015 by Intevation GmbH sean@1: sean@1: This is Free Software unter the terms of the sean@1: GNU GENERAL PUBLIC LICENSE Version 3 or later. sean@1: See http://www.gnu.org/licenses/gpl-3.0.txt for details sean@1: sean@1: sean@1: ##Usage Example: ## bernhard@7: see display_issues_demo.py or __main__ section below. sean@1: """ sean@1: sean@1: import sqlite3 as db sean@1: import cgitb sean@1: import roundup_content_data as rcd gernot@13: import os sean@1: sean@1: sean@1: def make_js_object_string(array): sean@1: formated = [] sean@1: sean@1: for item in array: sean@1: formated.append("{points: " + str(item) + "}") sean@1: sean@1: return ",".join(formated) sean@1: sean@1: sean@1: def make_js_object_date(array): sean@1: formated = [] sean@1: sean@1: for item in array: sean@1: formated.append("{date : new Date('" + str(item) + "')}") sean@1: sean@1: return ", ".join(formated) sean@1: gernot@14: def get_webpage(graph=None): sean@1: gernot@14: if graph is None: gernot@14: graph = os.path.dirname(os.path.realpath(__file__)) + '/graph.html' gernot@13: gernot@13: with open(graph, "r") as html_chart_file: sean@1: base_html_data = html_chart_file.read() sean@1: sean@1: base_html_data = (base_html_data sean@1: .replace("var critical=[];", "var critical=[" + make_js_object_string(rcd.data_dict["critical"]) + "]") sean@1: .replace("var urgent=[];", "var urgent=[" + make_js_object_string(rcd.data_dict["urgent"]) + "]") sean@1: .replace("var bug=[];", "var bug=[" + make_js_object_string(rcd.data_dict["bug"]) + "]") sean@1: .replace("var feature=[];", "var feature=[" + make_js_object_string(rcd.data_dict["feature"]) + "]") sean@1: .replace("var wish=[];", "var wish=[" + make_js_object_string(rcd.data_dict["wish"]) + "]") sean@1: .replace("var timestamp=[];", "var timestamp=[" + make_js_object_date(rcd.data_dict["date"]) + "]")) sean@1: sean@1: return base_html_data sean@1: gernot@14: def compile_db_stats_html(db_file, sql_select, graph=None): sean@1: sean@1: con = None sean@1: cur = None sean@1: sean@1: try: sean@1: con = db.connect(db_file) sean@1: cur = con.cursor() sean@1: cur.execute(sql_select) sean@1: sean@1: for row in cur.fetchall(): sean@1: rcd.data_dict["date"].append(row[0]) sean@1: rcd.data_dict["critical"].append(row[1]) sean@1: rcd.data_dict["urgent"].append(row[2]) sean@1: rcd.data_dict["bug"].append(row[3]) sean@1: rcd.data_dict["feature"].append(row[4]) sean@1: rcd.data_dict["wish"].append(row[5]) sean@1: finally: sean@1: if cur: sean@1: cur.close() sean@1: if con: sean@1: con.close() sean@1: gernot@14: return get_webpage(graph) sean@1: gernot@13: def render_webpage(content): gernot@13: for line in content.split("\n"): gernot@13: print(line) gernot@13: gernot@13: def render_db_stats_as_html(db_file, sql_select): gernot@13: render_webpage(compile_db_stats_html(db_file, sql_select)) sean@1: bernhard@3: if __name__ == '__main__': bernhard@3: cgitb.enable() bernhard@7: #spit out HTML file directly, thus no need to give headers to the server bernhard@7: #print("Content-Type: text/html") bernhard@7: #print() bernhard@3: bernhard@17: render_db_stats_as_html("./demo1.db", rcd.SELECT_ALL)