diff --git a/utils.py b/utils.py index d33dc7f..6244538 100644 --- a/utils.py +++ b/utils.py @@ -2,10 +2,10 @@ import json import os import time import enum +import typing import requests - from EDMCLogging import get_main_logger logger = get_main_logger() @@ -143,3 +143,83 @@ def _get_bearer() -> str: raise e return bearer + + +html_table_generator = """ +// Thanks to https://stackoverflow.com/a/21065846 +var _table_ = document.createElement('table'), + _tr_ = document.createElement('tr'), + _th_ = document.createElement('th'), + _td_ = document.createElement('td'); + +// Builds the HTML Table out of myList json data from Ivy restful service. +function buildHtmlTable(arr) { + var table = _table_.cloneNode(false), + columns = addAllColumnHeaders(arr, table); + for (var i = 0, maxi = arr.length; i < maxi; ++i) { + var tr = _tr_.cloneNode(false); + for (var j = 0, maxj = columns.length; j < maxj; ++j) { + var td = _td_.cloneNode(false); + cellValue = arr[i][columns[j]]; + td.appendChild(document.createTextNode(arr[i][columns[j]] || '')); + tr.appendChild(td); + } + table.appendChild(tr); + } + return table; +} + +// Adds a header row to the table and returns the set of columns. +// Need to do union of keys from all records as some records may not contain +// all records +function addAllColumnHeaders(arr, table) { + var columnSet = [], + tr = _tr_.cloneNode(false); + for (var i = 0, l = arr.length; i < l; i++) { + for (var key in arr[i]) { + if (arr[i].hasOwnProperty(key) && columnSet.indexOf(key) === -1) { + columnSet.push(key); + var th = _th_.cloneNode(false); + th.appendChild(document.createTextNode(key)); + tr.appendChild(th); + } + } + } + table.appendChild(tr); + return columnSet; +} +""" + +activity_table_html_template = """ + + + + + + + + + +""" + +activity_table_html_styles = """ +.table { + width: 100%; + margin-bottom: 20px; + border: 1px solid #dddddd; + border-collapse: collapse; +} +.table th { + font-weight: bold; + padding: 5px; + background: #efefef; + border: 1px solid #dddddd; +} +.table td { + border: 1px solid #dddddd; + padding: 5px; +}""" # TODO: fix css for table diff --git a/web.py b/web.py index 5b0c6c2..736ce89 100644 --- a/web.py +++ b/web.py @@ -4,6 +4,8 @@ import model import json import falcon +import utils + """ Request /activity/cqc?platform=pc[&limit=50&after=&before] @@ -26,11 +28,35 @@ class Activity: resp.text = json.dumps(model.get_activity_changes(**args_activity_changes)) except Exception as e: - resp.text = json.dumps({'status': 'error', 'msg': str(e)}) + raise falcon.HTTPInternalServerError(description=str(e)) + + +class ActivityHtml: + def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, leaderboard: str) -> None: + Activity().on_get(req, resp, leaderboard) + table_in_json: str = resp.text + resp.content_type = falcon.MEDIA_HTML + resp.text = utils.activity_table_html_template.replace('{items}', json.dumps(table_in_json)) + # what? f-strings? .format? never heard about them + + +class JS: + def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, file: str) -> None: + resp.content_type = falcon.MEDIA_JS + if file == 'json2htmltable.js': + resp.text = utils.html_table_generator + + elif file == 'table_styles.css': + resp.text = utils.activity_table_html_styles + + else: + raise falcon.HTTPNotFound app = falcon.App() app.add_route('/activity/{leaderboard}', Activity()) +app.add_route('/js/{file}', JS()) +app.add_route('/{leaderboard}', ActivityHtml()) if __name__ == '__main__': waitress.serve(app, host='127.0.0.1', port=9485)