comparison display_issues.py @ 29:8d86ba8dee42

Rename roundup_cc_display.py to display_issues.py and display_issues_demo.py to roundup_cc_display.py
author Magnus Schieder <mschieder@intevation.de>
date Thu, 22 Nov 2018 13:23:51 +0100
parents roundup_cc_display.py@e2864dabdb8c
children 9aca070c86bd
comparison
equal deleted inserted replaced
28:e2864dabdb8c 29:8d86ba8dee42
1 #!/usr/bin/env python3
2
3 """ Display previously saved issues from a database on webpage via CGI.
4
5 author: Sascha L. Teichmann <sascha.teichmann@intevation.de>
6 author: Bernhard Reiter <bernhard@intevation.de>
7 author: Sean Engelhardt <sean.engelhardt@intevation.de>
8
9 (c) 2010,2015 by Intevation GmbH
10
11 This is Free Software unter the terms of the
12 GNU GENERAL PUBLIC LICENSE Version 3 or later.
13 See http://www.gnu.org/licenses/gpl-3.0.txt for details
14
15
16 ##Usage Example: ##
17 see display_issues_demo.py or __main__ section below.
18 """
19
20 import sqlite3 as db
21 import cgitb
22 import roundup_content_data as rcd
23 import os
24
25
26 def make_js_object_string(array):
27 formated = []
28
29 for item in array:
30 formated.append("{points: " + str(item) + "}")
31
32 return ",".join(formated)
33
34
35 def make_js_object_date(array):
36 formated = []
37
38 for item in array:
39 formated.append("{date : new Date('" + str(item) + "')}")
40
41 return ", ".join(formated)
42
43 def get_webpage(data_dict, columns, status, keywords, graph=None):
44
45 if graph is None:
46 graph = os.path.dirname(os.path.realpath(__file__)) + '/graph.html'
47
48 with open(graph, "r") as html_chart_file:
49 base_html_data = html_chart_file.read()
50
51 if "None" not in columns:
52 data_dict["None"] = [0]
53
54
55 js_data_dickt ="{"
56 for col in columns.split(", "):
57 js_data_dickt += col + ":[" + make_js_object_string(data_dict[col]) + "],"
58 js_data_dickt += "}"
59
60 base_html_data = (base_html_data
61 .replace("status", status)
62 .replace("keywords", keywords)
63 .replace("js_data_dickt", js_data_dickt)
64 .replace("var timestamp=[];", "var timestamp=[" + make_js_object_date(data_dict["date"]) + "]"))
65
66 return base_html_data
67
68 def compile_db_stats_html(db_file, sql_select, columns, status="", keywords="", graph=None):
69
70 data_dict = {"date": []}
71 status_list = columns.split(", ")
72 for x in status_list:
73 data_dict[x] = []
74
75 con = None
76 cur = None
77
78 try:
79 con = db.connect(db_file)
80 cur = con.cursor()
81 cur.execute(sql_select)
82
83 for row in cur.fetchall():
84 data_dict["date"].append(row[0])
85 for x in range(len(status_list)):
86 data_dict[status_list[x]].append(row[x+1])
87
88 finally:
89 if cur:
90 cur.close()
91 if con:
92 con.close()
93
94 return get_webpage(data_dict, columns, status, keywords, graph)
95
96 def render_webpage(content):
97 for line in content.split("\n"):
98 print(line)
99
100 def render_db_stats_as_html(db_file, sql_select, columns, status="", keywords=""):
101 render_webpage(compile_db_stats_html(db_file, sql_select, columns, status, keywords))
102
103 if __name__ == '__main__':
104 cgitb.enable()
105 #spit out HTML file directly, thus no need to give headers to the server
106 #print("Content-Type: text/html")
107 #print()
108
109 render_db_stats_as_html("./demo3.db", rcd.SELECT_ALL)
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)