view display_issues.py @ 36:59e1659a0a0b tip

Update 'TODO.creole' * Old TODOs moved to 'doc/old_TODO.creole'
author Magnus Schieder <mschieder@intevation.de>
date Mon, 26 Nov 2018 16:52:45 +0100
parents 80bbd06fe8ec
children
line wrap: on
line source
#!/usr/bin/env python3

""" Display previously saved issues from a database on webpage via CGI.

author: Sascha L. Teichmann <sascha.teichmann@intevation.de>
author: Bernhard Reiter <bernhard@intevation.de>
author: Sean Engelhardt <sean.engelhardt@intevation.de>
author: Magnus Schieder <magnus.schieder@intevation.de>
(c) 2010,2015,2018 by Intevation GmbH

This is Free Software unter the terms of the
GNU GENERAL PUBLIC LICENSE Version 3 or later.
See http://www.gnu.org/licenses/gpl-3.0.txt for details


##Usage Example: ##
see display_issues_demo.py or __main__ section below.
"""

import sqlite3 as db
import cgitb
import roundup_content_data as rcd
import os


def make_js_object_string(array):
    formated = []

    for item in array:
        formated.append("{points: " + str(item) + "}")

    return ",".join(formated)


def make_js_object_date(array):
    formated = []

    for item in array:
        formated.append("{date : new Date('" + str(item) + "')}")

    return ", ".join(formated)

def get_webpage(data_dict, columns, search_parameters, keywords, graph=None):
    """Returns the website with inserted data."""

    if graph is None:
        graph = os.path.dirname(os.path.realpath(__file__)) + '/graph.html'

    with open(graph, "r") as html_chart_file:
        base_html_data = html_chart_file.read()

    if "None" not in columns:
        data_dict["None"] = [0]

    # Converts the data to a JS object.
    js_data_dickt ="{"
    for col in columns.split(", "):
        js_data_dickt += col + ":[" + make_js_object_string(data_dict[col]) + "],"
    js_data_dickt += "}"

    base_html_data = (base_html_data
        .replace("search_parameters", search_parameters)
        .replace("keywords", keywords)
        .replace("js_data_dickt", js_data_dickt)
        .replace("var timestamp=[];", "var timestamp=[" + make_js_object_date(data_dict["date"]) + "]"))

    return base_html_data

def compile_db_stats_html(db_file, sql_select, columns, search_parameters="", keywords="", graph=None):

    data_dict = {"date": []}
    status_list = columns.split(", ")
    for x in status_list:
        data_dict[x] = []

    con = None
    cur = None

    try:
        con = db.connect(db_file)
        cur = con.cursor()
        cur.execute(sql_select)

        for row in cur.fetchall():
            data_dict["date"].append(row[0])
            for x in range(len(status_list)):
                data_dict[status_list[x]].append(row[x+1])

    finally:
        if cur:
            cur.close()
        if con:
            con.close()

    return get_webpage(data_dict, columns, search_parameters, keywords, graph)

def render_webpage(content):
    for line in content.split("\n"):
        print(line)

def render_db_stats_as_html(db_file, sql_select, columns, search_parameters="", keywords=""):
    render_webpage(compile_db_stats_html(db_file, sql_select, columns, search_parameters, keywords))

if __name__ == '__main__':
    cgitb.enable()
    #spit out HTML file directly, thus no need to give headers to the server
    #print("Content-Type: text/html")
    #print()

    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)