From 4b818249ca70f53e30cc95f60fed4d53bb860e24 Mon Sep 17 00:00:00 2001 From: denisova-ok Date: Sun, 22 Dec 2024 23:21:47 +0300 Subject: [PATCH] change export logic --- server/static/js/export.js | 26 +++++++++++++++++++++ server/static/js/org_table.js | 28 ----------------------- server/templates/host.html | 1 + server/templates/page_org_statictics.html | 3 ++- server/templates/player_page.html | 3 ++- server/templates/profile.html | 3 ++- server/templates/status.html | 3 ++- server/web_server.py | 4 +++- 8 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 server/static/js/export.js diff --git a/server/static/js/export.js b/server/static/js/export.js new file mode 100644 index 0000000..776b687 --- /dev/null +++ b/server/static/js/export.js @@ -0,0 +1,26 @@ +let exportButton = document.getElementById('export'); +exportButton.addEventListener('click', exportDB); +async function exportDB() { + await fetch(new URL('/export_db', 'http://localhost:8000').href, { + method: "POST", + headers: { + 'Accept': 'application/json', + "Content-type": "application/json; charset=UTF-8" + }}) + .then(res => res.json()) + .then(content => { + const jsonString = JSON.stringify(content, null, 2); + const blob = new Blob([jsonString], { type: "application/json" }); + + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = "data.json"; + + document.body.appendChild(a); + a.click(); + + document.body.removeChild(a); + URL.revokeObjectURL(url); + }) +} \ No newline at end of file diff --git a/server/static/js/org_table.js b/server/static/js/org_table.js index 593066f..0f97934 100644 --- a/server/static/js/org_table.js +++ b/server/static/js/org_table.js @@ -1,7 +1,6 @@ let wDialog = document.getElementById("WDialog"); const Button = document.getElementById("close"); const Button1 = document.getElementById("close1"); -const exportButton = document.getElementById('export'); Button.addEventListener('click', () => { wDialog.close(); @@ -11,7 +10,6 @@ Button1.addEventListener('click', () => { wDialog.close(); }) -exportButton.addEventListener('click', exportToJSON); async function init() { console.log(game_id); @@ -121,32 +119,6 @@ async function saveUsers(event) { }) } -async function exportToJSON() { - await fetch(new URL('/get_list_people', 'http://localhost:8000').href, { - method: "GET", - headers: { - 'Accept': 'application/json', - "Content-type": "application/json; charset=UTF-8" - }}) - .then(res => res.json()) - .then(content => { - const jsonString = JSON.stringify(content, null, 2); - const blob = new Blob([jsonString], { type: "application/json" }); - - const url = URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = "data.json"; - - document.body.appendChild(a); - a.click(); - - document.body.removeChild(a); - URL.revokeObjectURL(url); - }) - - -} init() \ No newline at end of file diff --git a/server/templates/host.html b/server/templates/host.html index bc148ba..137af4d 100644 --- a/server/templates/host.html +++ b/server/templates/host.html @@ -42,5 +42,6 @@ + \ No newline at end of file diff --git a/server/templates/page_org_statictics.html b/server/templates/page_org_statictics.html index 174a39a..1491379 100644 --- a/server/templates/page_org_statictics.html +++ b/server/templates/page_org_statictics.html @@ -16,7 +16,7 @@
- +
@@ -32,5 +32,6 @@
+ \ No newline at end of file diff --git a/server/templates/player_page.html b/server/templates/player_page.html index 4f069f3..f639a1a 100644 --- a/server/templates/player_page.html +++ b/server/templates/player_page.html @@ -12,7 +12,7 @@
- +
@@ -22,5 +22,6 @@
+ \ No newline at end of file diff --git a/server/templates/profile.html b/server/templates/profile.html index 661b2c3..c1ff835 100644 --- a/server/templates/profile.html +++ b/server/templates/profile.html @@ -12,7 +12,7 @@
- +
@@ -35,5 +35,6 @@
+ \ No newline at end of file diff --git a/server/templates/status.html b/server/templates/status.html index 923a3ee..a73f50e 100644 --- a/server/templates/status.html +++ b/server/templates/status.html @@ -12,7 +12,7 @@
- + @@ -76,5 +76,6 @@
+ \ No newline at end of file diff --git a/server/web_server.py b/server/web_server.py index acd564b..0efef23 100644 --- a/server/web_server.py +++ b/server/web_server.py @@ -280,7 +280,7 @@ def get_actions_statistics(): #запрос на получение всех событий actions = [] # actions = [{'player_id': 2, 'type': 2, 'date': datetime.datetime(2024, 11, 21, 0, 0)}, - # {'player_id': 1, 'type': 1, 'date': datetime.datetime(2024, 12, 12, 0, 0)}] + # {'player_id': 1, 'type': 1, 'date': datetime.datetime(2024, 12, 12, 0, 0)}] for action in actions: statistics[action['type']] += 1 return statistics @@ -530,6 +530,8 @@ def export_db(): # вместо filename - название файла для экспорта. если всё норм, то export_db вернёт True if not db.export_db("filename"): export_status = 500 + # в return можно прокинуть данные как словарик (пример ниже) и они сохранятся в json + # return {'abc': 'abc', 'dfd': 5, 'dsas': 6} return app.response_class(status=export_status) # @app.route('/import_db', methods=['POST'])