Mercurial > roundup-cc
comparison roundup_cc_display.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 | display_issues_demo.py@e2864dabdb8c |
children | 9aca070c86bd |
comparison
equal
deleted
inserted
replaced
28:e2864dabdb8c | 29:8d86ba8dee42 |
---|---|
1 #!/usr/bin/env python3 | 1 #!/usr/bin/env python3 |
2 | 2 |
3 """ Display previously saved issues from a database on webpage via CGI. | 3 """ Fetch issues from a roundup-tracker and save them in a databse. |
4 | 4 |
5 author: Sascha L. Teichmann <sascha.teichmann@intevation.de> | 5 author: Sascha L. Teichmann <sascha.teichmann@intevation.de> |
6 author: Bernhard Reiter <bernhard@intevation.de> | 6 author: Bernhard Reiter <bernhard@intevation.de> |
7 author: Sean Engelhardt <sean.engelhardt@intevation.de> | 7 author: Sean Engelhardt <sean.engelhardt@intevation.de> |
8 | 8 |
9 (c) 2010,2015 by Intevation GmbH | 9 (c) 2010, 2015, 2018 by Intevation GmbH |
10 | 10 |
11 This is Free Software unter the terms of the | 11 This is Free Software unter the terms of the |
12 GNU GENERAL PUBLIC LICENSE Version 3 or later. | 12 GNU GENERAL PUBLIC LICENSE Version 3 or later. |
13 See http://www.gnu.org/licenses/gpl-3.0.txt for details | 13 See http://www.gnu.org/licenses/gpl-3.0.txt for details |
14 """ | |
15 import configparser | |
16 import argparse | |
17 from display_issues import * | |
18 | |
19 def main(): | |
20 PRIO = "critical, urgent, bug, feature, wish" | |
21 STATES = "unread, deferred, chatting, need_eg, in_progress, testing, done_cbb, resolved" | |
22 | |
23 parser = argparse.ArgumentParser() | |
24 parser.add_argument("config_file", type=str, metavar="[config file]") | |
25 args = parser.parse_args() | |
26 | |
27 config = configparser.ConfigParser() | |
28 config.read(args.config_file) | |
29 | |
30 db = config.get("DB", "DatabaseFile") | |
31 keywords = config.get("SEARCH", "Keywords", fallback="") | |
14 | 32 |
15 | 33 |
16 ##Usage Example: ## | 34 search = config.get("SEARCH", "Search", fallback="prio") |
17 see display_issues_demo.py or __main__ section below. | 35 if search == "prio": |
18 """ | 36 status = config.get("SEARCH", "Status", fallback="") |
37 columns = config.get("SEARCH", "Priority", fallback=PRIO) | |
38 noPrio = config.get("SEARCH", "IncludeNoPrio", fallback=False) | |
39 if noPrio: | |
40 columns += ", None" | |
19 | 41 |
20 import sqlite3 as db | 42 elif search == "status": |
21 import cgitb | 43 columns = config.get("SEARCH", "Status", fallback=STATES) |
22 import roundup_content_data as rcd | 44 status = "" |
23 import os | |
24 | 45 |
46 else: | |
47 print("Incorrect [SEARCH]Search parameter. (prio, status)") | |
48 return | |
25 | 49 |
26 def make_js_object_string(array): | 50 # roundup uses a "-" in its search parameters. Sql can't handle it. |
27 formated = [] | 51 columns = columns.replace("-", "_") |
28 | 52 |
29 for item in array: | 53 cgitb.enable() # (optional) HTML traceback to browser |
30 formated.append("{points: " + str(item) + "}") | 54 #render_db_stats_as_html("./demo1.db", rcd.SELECT_ALL) |
31 | 55 render_db_stats_as_html(db, |
32 return ",".join(formated) | 56 rcd.build_sql_select(columns).format("timestamp > date('now', '-2 month')"), |
33 | 57 columns, status, keywords) |
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 | 58 |
103 if __name__ == '__main__': | 59 if __name__ == '__main__': |
104 cgitb.enable() | 60 main() |
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) |