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