Skip to content

Commit

Permalink
change export logic
Browse files Browse the repository at this point in the history
  • Loading branch information
denisova-ok committed Dec 22, 2024
1 parent 4f4f85a commit 4b81824
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
26 changes: 26 additions & 0 deletions server/static/js/export.js
Original file line number Diff line number Diff line change
@@ -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);
})
}
28 changes: 0 additions & 28 deletions server/static/js/org_table.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -11,7 +10,6 @@ Button1.addEventListener('click', () => {
wDialog.close();
})

exportButton.addEventListener('click', exportToJSON);

async function init() {
console.log(game_id);
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions server/templates/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
</div>
</div>
<script src="{{ url_for('static', filename='js/org_table.js') }}"></script>
<script src="{{ url_for('static', filename='js/export.js') }}"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion server/templates/page_org_statictics.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div style="width: 525px; justify-content:first baseline; align-items: center; display: flex; flex-direction: column;">
<img src="../static/img/Frame 2.png/">
<div class="groupButton">
<button class="buttonP">Экспортировать данные</button>
<button class="buttonP" id="export">Экспортировать данные</button>
<button class="buttonPD" disabled>Статистика команды</button>
<button class="buttonP" onclick="window.location.href=`/host/${game_id}`">Данные об игроках</button>
</div>
Expand All @@ -32,5 +32,6 @@
</div>
</div>
<script src="{{ url_for('static', filename='js/org_page.js') }}"></script>
<script src="{{ url_for('static', filename='js/export.js') }}"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion server/templates/player_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div style="width: 525px; justify-content:first baseline; align-items: center; display: flex; flex-direction: column;">
<img src="../static/img/Frame 2.png/">
<div class="groupButton">
<button class="buttonP">Экспортировать данные</button>
<button class="buttonP" id="export">Экспортировать данные</button>
<button class="buttonP" onclick="window.location.href='/profile'">Анкета</button>
<button class="buttonP" onclick="window.location.href='/status'">Текущее состояние</button>
</div>
Expand All @@ -22,5 +22,6 @@
</div>
</div>
<script src="{{ url_for('static', filename='js/player_page.js') }}"></script>
<script src="{{ url_for('static', filename='js/export.js') }}"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion server/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div style="width: 525px; justify-content:first baseline; align-items: center; display: flex; flex-direction: column;">
<img src="../static/img/Frame 2.png/">
<div class="groupButton">
<button class="buttonP">Экспортировать данные</button>
<button class="buttonP" id="export">Экспортировать данные</button>
<button class="buttonPD" disabled>Анкета</button>
<button class="buttonP" onclick="window.location.href='/status'">Текущее состояние</button>
</div>
Expand All @@ -35,5 +35,6 @@
</form>
</div>
</div>
<script src="{{ url_for('static', filename='js/export.js') }}"></script>
</body>
</html>
3 changes: 2 additions & 1 deletion server/templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div style="width: 525px; justify-content:space-between; align-items: center; display: flex; flex-direction: column;">
<img src="../static/img/Frame 2.png/">
<div class="groupButton">
<button class="buttonP">Экспортировать данные</button>
<button class="buttonP" id="export">Экспортировать данные</button>
<button class="buttonP" onclick="window.location.href='/profile'">Анкета</button>
<button class="buttonPD" disabled>Текущее состояние</button>
<button class="confirm">Подтвердить получение подарка</button>
Expand Down Expand Up @@ -76,5 +76,6 @@
</div>
</div>
<script src="{{ url_for('static', filename='js/player_page.js') }}"></script>
<script src="{{ url_for('static', filename='js/export.js') }}"></script>
</body>
</html>
4 changes: 3 additions & 1 deletion server/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit 4b81824

Please sign in to comment.