Skip to content

Commit

Permalink
add front for import
Browse files Browse the repository at this point in the history
  • Loading branch information
denisova-ok committed Dec 22, 2024
1 parent 371b28d commit 4f4f85a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
38 changes: 38 additions & 0 deletions server/static/js/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
let importButton = document.getElementById('import');

importButton.addEventListener('click', importData);

async function importData(event) {
let fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json';

fileInput.onchange = event => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();

reader.onload = function(e) {
try {
const jsonData = JSON.parse(e.target.result);
sendJsonData(jsonData);
} catch (error) {
alert("Ошибка при чтении файла JSON: " + error.message);
}
};
reader.readAsText(file);
}
}
fileInput.click()
}

async function sendJsonData(jsonData) {
await fetch(new URL('/import_db', 'http://localhost:8000').href, {
method: "POST",
headers: {
'Accept': 'application/json',
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify(jsonData)
});
}
3 changes: 2 additions & 1 deletion server/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
<img style="width: 100%; height: 100%" src="{{ url_for('static', filename='img/Frame 1.png') }}"/>
<div class="frame1">
<button class="button_registr" style="margin:1%" onclick="window.location.href='/registration'">Зарегистрировать команду</button>
<button class="button_import">Импортировать данные</button>
<button class="button_import" id="import">Импортировать данные</button>
<div style="text-align: center"><span style="color: black; font-size: 25px; font-weight: 300; word-wrap: break-word">Команда уже есть? </span><span onclick="window.location.href='/login'" class="click_text1">Зайти в профиль</span></div>
</div>
<div class="frame_bottom">
<button class="button_admin" onclick="window.location.href='/admin'">Панель администратора</button>
</div>
</div>
<script src="{{ url_for('static', filename='js/import.js') }}"></script>
</body>
</html>
6 changes: 2 additions & 4 deletions server/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,10 @@ def get_filtered_games():
return filtered_games






@app.route('/import_db', methods=['POST'])
def import_db():
new_data = json.loads(flask.request.data)
print(new_data)
return app.response_class(status=200)

@app.route('/export_db', methods=['POST'])
Expand Down

0 comments on commit 4f4f85a

Please sign in to comment.