mirror of
https://github.com/norohind/SquadsActivityMonitor.git
synced 2025-04-13 13:27:13 +03:00
pretty view as table
This commit is contained in:
parent
181f7892de
commit
1e3e3682b2
82
utils.py
82
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 = """
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<script src="js/json2htmltable.js"></script>
|
||||
<script type="text/javascript">
|
||||
window.onload = () => {
|
||||
document.body.appendChild(buildHtmlTable(JSON.parse({items})));
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
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
|
||||
|
28
web.py
28
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user