sean@11: #!/usr/bin/env python3 sean@11: sean@11: """ Display previously saved issues from a database on webpage via CGI. sean@11: sean@11: author: Sascha L. Teichmann sean@11: author: Bernhard Reiter sean@11: author: Sean Engelhardt sean@11: sean@11: (c) 2010,2015 by Intevation GmbH sean@11: sean@11: This is Free Software unter the terms of the sean@11: GNU GENERAL PUBLIC LICENSE Version 3 or later. sean@11: See http://www.gnu.org/licenses/gpl-3.0.txt for details sean@11: sean@11: sean@11: ##Usage Example: ## sean@11: sean@11: render_db_stats_as_html(rcd.DATABASE_DEMO, rcd.SELECT_ALL) sean@11: """ sean@11: sean@11: import sqlite3 as db sean@12: import cgitb, os, sys, inspect sean@11: import roundup_cc.roundup_content_data as rcd sean@11: sean@12: PATH = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) sean@11: HTML_DATA = "graph.html" sean@12: PATH_TO_HTML = PATH + "/" + HTML_DATA sean@11: sean@11: def make_js_object_string(array): sean@11: formated = [] sean@11: sean@11: for item in array: sean@11: formated.append("{points: " + str(item) + "}") sean@11: sean@11: return ",".join(formated) sean@11: sean@11: sean@11: def make_js_object_date(array): sean@11: formated = [] sean@11: sean@11: for item in array: sean@11: formated.append("{date : new Date('" + str(item) + "')}") sean@11: sean@11: return ", ".join(formated) sean@11: sean@11: sean@11: def get_webpage(): sean@11: sean@11: with open(PATH_TO_HTML, "r") as html_chart_file: sean@11: base_html_data = html_chart_file.read() sean@11: sean@11: base_html_data = (base_html_data sean@11: .replace("var critical=[];", "var critical=[" + make_js_object_string(rcd.data_dict["critical"]) + "]") sean@11: .replace("var urgent=[];", "var urgent=[" + make_js_object_string(rcd.data_dict["urgent"]) + "]") sean@11: .replace("var bug=[];", "var bug=[" + make_js_object_string(rcd.data_dict["bug"]) + "]") sean@11: .replace("var feature=[];", "var feature=[" + make_js_object_string(rcd.data_dict["feature"]) + "]") sean@11: .replace("var wish=[];", "var wish=[" + make_js_object_string(rcd.data_dict["wish"]) + "]") sean@11: .replace("var timestamp=[];", "var timestamp=[" + make_js_object_date(rcd.data_dict["date"]) + "]")) sean@11: sean@11: return base_html_data sean@11: sean@11: sean@11: # def render_webpage(content): sean@11: # webpage_string = "" sean@11: # for line in content.split("\n"): sean@11: # print(line) sean@11: sean@11: sean@11: def render_db_stats_as_html(db_file, sql_select): sean@11: sean@11: con = None sean@11: cur = None sean@11: sean@11: try: sean@11: con = db.connect(db_file) sean@11: cur = con.cursor() sean@11: cur.execute(sql_select) sean@11: sean@11: for row in cur.fetchall(): sean@11: rcd.data_dict["date"].append(row[0]) sean@11: rcd.data_dict["critical"].append(row[1]) sean@11: rcd.data_dict["urgent"].append(row[2]) sean@11: rcd.data_dict["bug"].append(row[3]) sean@11: rcd.data_dict["feature"].append(row[4]) sean@11: rcd.data_dict["wish"].append(row[5]) sean@11: finally: sean@11: if cur: sean@11: cur.close() sean@11: if con: sean@11: con.close() sean@11: sean@11: # render_webpage(get_webpage()) sean@11: # print(get_webpage()) sean@11: return get_webpage() sean@11: sean@11: # cgitb.enable() sean@11: # print("Content-Type: text/html") sean@11: # print()