NDOJ/templates/user/import/index.html

112 lines
3.5 KiB
HTML
Raw Permalink Normal View History

2021-07-28 22:58:42 +00:00
{% extends "user/user-base.html" %}
{% block js_media %}
2023-01-27 23:11:10 +00:00
<script>
2021-07-28 22:58:42 +00:00
function getCookie(name) {
2023-01-27 23:11:10 +00:00
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
2021-07-28 22:58:42 +00:00
}
2023-01-27 23:11:10 +00:00
}
return cookieValue;
2021-07-28 22:58:42 +00:00
}
2023-01-27 23:11:10 +00:00
2021-07-28 22:58:42 +00:00
const csrftoken = getCookie('csrftoken');
$(function() {
2023-01-27 23:11:10 +00:00
$('#load_button').on('click', function(e) {
e.preventDefault();
var files = $('#csv_file').prop('files');
if (files.length == 1) {
$('#load_button').addClass('disabled');
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
var file = files[0];
//if (file.type != 'text/csv') {
// alert("{{_('Upload CSV only')}}");
// return;
//}
var form_data = new FormData();
form_data.append('csv_file', file, file.name);
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
var xhr = new XMLHttpRequest();
xhr.open('POST', "{{url('import_users_post_file')}}", true);
xhr.setRequestHeader('X-CSRFToken', csrftoken);
xhr.onload = function () {
if (xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
$('#load_button').removeClass('disabled');
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
if (json.done) {
window.import_users = json.data
$('#table_csv').html(json.html);
$('#confirm_button').removeClass('disabled');
}
else {
window.import_users = []
$('#table_csv').html(json.msg);
$('#confirm_button').addClass('disabled');
}
} else {
alert('Fail to read file.');
2021-07-28 22:58:42 +00:00
}
2023-01-27 23:11:10 +00:00
};
xhr.send(form_data);
}
})
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
$('#confirm_button').on('click', function() {
$(this).addClass('disabled');
var data = {
'users': window.import_users
};
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
if (!data.users || data.users.length == 0) {
alert('No valid users');
return;
}
2021-07-28 22:58:42 +00:00
2023-01-27 23:11:10 +00:00
$('#table_csv').html('');
$('#log').html('Working...');
$.post({
url: "{{url('import_users_submit')}}",
data: JSON.stringify(data),
contentType:"application/json; charset=utf-8",
dataType:"text",
fail: function() {alert('Fail to import')},
success: function(data) {
data = JSON.parse(data);
var msg = data.msg.split('\n');
$('#log').html('')
for (var i of msg) {
$('#log').append(`<p>${i}</p>`);
}
}
2021-07-28 22:58:42 +00:00
})
2023-01-27 23:11:10 +00:00
})
2021-07-28 22:58:42 +00:00
});
2023-01-27 23:11:10 +00:00
</script>
2021-07-28 22:58:42 +00:00
{% endblock %}
{% block body %}
2023-01-27 23:11:10 +00:00
{% csrf_token %}
<center>
2021-07-28 22:58:42 +00:00
<label for="csv_file">{{_('User File')}}:</label>
<input type="file" accept=".csv" id="csv_file">
<a href="{{url('import_users_sample')}}">{{_('Sample')}}</a>
<div style="display: inline-flex">
2023-01-27 23:11:10 +00:00
<button id="load_button" style="margin-left: 1em">{{_('Load')}}</button>
<button id="confirm_button" style="margin-left: 1em" class="disabled">{{_('Import')}}</button>
2021-07-28 22:58:42 +00:00
</div>
2023-01-27 23:11:10 +00:00
</center>
<br>
<table id="table_csv" class="table"></table>
<p style="margin-left: 2em" id="log"></p>
2021-08-16 21:42:54 +00:00
{% endblock %}