aboutsummaryrefslogtreecommitdiffstats
path: root/app.py
blob: a4a3f0d83d0976718ceb01ed32fcc375df8df4f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import database
import urllib.parse
import flask
import json
import os

app = flask.Flask(__name__)

if not os.path.exists(".docker"):
    import dotenv
    dotenv.load_dotenv(dotenv_path = "db.env")
    host = "srv.home"
else:
    host = "db" 

@app.route("/")
def serve_index():
    return flask.render_template(
        "index.html.j2",
        title = "UK Gender Pay Gap",
        charts = get_charts()["index"]
    )

def get_charts():
    with open("charts.json", "r") as f:
        return json.load(f)

@app.route("/api/charts.json")
def serve_charts():
    return flask.jsonify(get_charts())


@app.route("/search_click", methods = ["POST"])
def search_redirect():
    return flask.redirect("/search?s=%s" % urllib.parse.quote_plus(dict(flask.request.form)["search"]))

@app.route("/plot/<name>/apply_click", methods = ["POST"])
def apply_redirect(name):
    new_args = {}
    for k, v in flask.request.form.items():
        if v != "No filter":
            new_args[k] = v

    # print("/" + "/".join(flask.request.full_path.split("/")[1:-1]) + "?" + urllib.parse.urlencode(new_args))
    return flask.redirect("/" + "/".join(flask.request.full_path.split("/")[1:-1]) + "?" + urllib.parse.urlencode(new_args))

@app.route("/api/years")
def api_get_years():
    pay_type = flask.request.args.get("Pay Type")
    sic_type = flask.request.args.get("SIC Type")
    employer_type = flask.request.args.get("Employer Type")
    employer_size = flask.request.args.get("Employer Size")
    # print("sic_type", sic_type)
    # print("employer_type", employer_type)
    # print("employer_size", employer_size)
    if pay_type is None or pay_type.lower() not in {'hourly', 'bonuses'}:
        return flask.abort(400, "The key `pay type` must be equal to 'hourly' or 'bonuses'")
    with database.PayGapDatabase(host = host) as db:
        return flask.jsonify(db.get_pay_by_year(pay_type, sic_section_name = sic_type, employer_size = employer_size, employer_type = employer_type))

@app.route("/api/sic_sec")
def api_get_sic_section_pay():
    pay_type = flask.request.args.get("Pay Type")
    year = flask.request.args.get("year")
    if pay_type is None or pay_type.lower() not in {'hourly', 'bonuses'}:
        return flask.abort(400, "The key `pay type` must be equal to 'hourly' or 'bonuses'")
    with database.PayGapDatabase(host = host) as db:
        if year is not None:
            if year not in db.get_years():
                return flask.abort(400, "Unrecognised year '%s'. The year option must be in %s" % (year, ", ".join(db.get_years())))
        
        return flask.jsonify(db.get_pay_by_sic_section(pay_type, year))


@app.route("/search")
def search():
    with database.PayGapDatabase(host = host) as db:
        search_text = flask.request.args.get("s")
        companies = db.search_company(search_text)
        if len(companies) == 1:
            return flask.redirect("/company/%s" % companies[0][1])

        return flask.render_template(
            "search.html.j2",
            title = "Search",
            companies = companies
        )

def get_chart_elem(url):
    for i in get_charts()["index"]:
        print(i["url"], url)
        # if i["url"] == url:
        #     return i
        if url.startswith(i["url"]):
            return i

@app.route("/plot/<name>")
def serve_large_plot(name):
    with database.PayGapDatabase(host = host) as db:
        # print(flask.request.full_path)
        elem = get_chart_elem(flask.request.full_path)
        filters = elem["filters"]
        for k, v in filters.items():
            if v == "<SICType>":
                filters[k] = {"options": db.get_sic_sections()}
            if v == "<CompanyType>":
                filters[k] = {"options": db.get_company_types()}
            if v == "<CompanySize>":
                 filters[k] = {"options": db.get_company_sizes()}
            if v == "<Years>":
                filters[k] = {"options": db.get_years()}

    elem["url"] = flask.request.full_path
    # print("elem", elem)
    current_filters = dict(flask.request.args)
    # print("current_filters", current_filters)
    return flask.render_template(
        "plot.html.j2",
        title = elem["title"],
        elem = elem,
        alt = "Lorem ipsum.",
        filters = filters,
        current_filters = current_filters,
        len = len
    )

if __name__ == "__main__":
    app.run("0.0.0.0", port = 5005, debug = True)