diff --git a/model/__init__.py b/model/__init__.py new file mode 100644 index 0000000..dce5180 --- /dev/null +++ b/model/__init__.py @@ -0,0 +1,3 @@ +from . import sqlite_model + +model = sqlite_model.SqliteModel() diff --git a/model/sqlite_model.py b/model/sqlite_model.py new file mode 100644 index 0000000..4314855 --- /dev/null +++ b/model/sqlite_model.py @@ -0,0 +1,39 @@ +import sqlite3 +from . import sqlite_sql_requests +import json + + +class SqliteModel: + db: sqlite3.Connection + + def open_model(self) -> None: + """ + This method must be called before any action on model + :return: + """ + + self.db = sqlite3.connect('squads.sqlite', check_same_thread=False) + self.db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r)) + + def close_model(self) -> None: + """ + This method should be called before program exit + :return: + """ + + self.db.close() + + def list_squads_by_tag(self, tag: str) -> list: + """ + Take tag or part of tag and return all squads with tag matches this pattern + + :param tag: + :return: + """ + + squads = self.db.execute(sqlite_sql_requests.squads_by_tag, {'tag': tag.upper()}).fetchall() + + # for squad in squads: + # squad['User tags'] = json.loads(squad['User tags']) + + return squads diff --git a/model/sqlite_sql_requests.py b/model/sqlite_sql_requests.py new file mode 100644 index 0000000..e48d05a --- /dev/null +++ b/model/sqlite_sql_requests.py @@ -0,0 +1,16 @@ +squads_by_tag = """select + name as "Squadron name", + tag as "Tag", + member_count as "Members", + owner_name as "Owner", + platform as "Platform", + created as "Created UTC", + power_name as "Power name", + super_power_name as "Super power name", + faction_name as "Faction name", + --user_tags as "User tags", + inserted_timestamp as "Updated UTC", + squad_id +from squads_view +where tag = :tag; +""" \ No newline at end of file diff --git a/web/__init__.py b/web/__init__.py new file mode 100644 index 0000000..5dacbb4 --- /dev/null +++ b/web/__init__.py @@ -0,0 +1,38 @@ +import os +import falcon +import json + +os.chdir('..') + +from model import model +import dynamic_js +import utils + + +class SquadsInfoByTag: + def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str) -> None: + resp.content_type = falcon.MEDIA_JSON + resp.text = json.dumps(model.list_squads_by_tag(tag)) + + +class SquadsInfoByTagHtml: + def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, tag: str) -> None: + resp.content_type = falcon.MEDIA_HTML + model_answer = model.list_squads_by_tag(tag) + + # for squad in model_answer: + # squad['User tags'] = utils.humanify_resolved_user_tags(utils.resolve_user_tags(squad['User tags'])) + + resp.text = dynamic_js.activity_table_html_template.replace('{items}', json.dumps(model_answer)) + + +application = falcon.App() +application.add_route('/api/squads/now/by-tag/{tag}', SquadsInfoByTag()) +application.add_route('/squads/now/by-tag/{tag}', SquadsInfoByTagHtml()) + +if __name__ == '__main__': + model.open_model() + import waitress + import os + application.add_static_route('/js', os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'js')) + waitress.serve(application, host='127.0.0.1', port=9486) diff --git a/web/dynamic_js.py b/web/dynamic_js.py new file mode 100644 index 0000000..179a2cf --- /dev/null +++ b/web/dynamic_js.py @@ -0,0 +1,33 @@ +activity_table_html_template = """ + +
+ + + + + + + +""" \ No newline at end of file diff --git a/web/static/js/json2htmltable.js b/web/static/js/json2htmltable.js new file mode 100644 index 0000000..e41b176 --- /dev/null +++ b/web/static/js/json2htmltable.js @@ -0,0 +1,42 @@ +// 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; +} \ No newline at end of file diff --git a/web/static/js/table_styles.css b/web/static/js/table_styles.css new file mode 100644 index 0000000..73cd581 --- /dev/null +++ b/web/static/js/table_styles.css @@ -0,0 +1,18 @@ +table { + 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; + text-align:center; + /*white white-space:pre-wrap; + word-wrap:break-word; */ +} \ No newline at end of file