Sith/pedagogy/templates/pedagogy/uv_edit.jinja

87 lines
2.6 KiB
Django/Jinja
Raw Normal View History

2019-10-17 23:28:59 +00:00
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Edit UV{% endtrans %}
{% endblock %}
{% block content %}
<h2>{% trans %}Edit UV{% endtrans %}</h2>
<form action="" method="post" enctype="multipart/form-data" id="uv_edit">
{% csrf_token %}
{{ form.non_field_errors() }}
{% for field in form %}
2019-10-17 23:45:49 +00:00
{% if field.is_hidden %}
{{ field }}
{% else %}
2019-10-17 23:28:59 +00:00
<p>
{{ field.errors }}
<label for="{{ field.name }}">{{ field.label }}</label>
{{ field }}
{% if field.name == 'code' %}
2019-10-20 15:09:36 +00:00
<button type="button" id="autofill">{% trans %}Import from UTBM{% endtrans %}</button>
2019-10-17 23:28:59 +00:00
{% endif %}
</p>
{% endif %}
{% endfor %}
<p><input type="submit" value="{% trans %}Update{% endtrans %}" /></p>
</form>
{% endblock %}
{% block script %}
{{ super() }}
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
const autofillBtn = document.getElementById('autofill')
const codeInput = document.querySelector('input[name="code"]')
autofillBtn.addEventListener('click', () => {
const today = new Date()
let year = today.getFullYear()
2019-10-20 15:09:36 +00:00
if (today.getMonth() < 7) { // student year starts in september
2019-10-17 23:28:59 +00:00
year--
}
const url = "{{ url('api:uv_endpoint') }}?year=" + year + "&code=" + codeInput.value
deleteQuickNotifs()
2019-10-17 23:28:59 +00:00
2019-10-21 08:34:46 +00:00
$.ajax({
dataType: "json",
url: url,
success: function(data, _, xhr) {
if (xhr.status != 200) {
2019-10-21 14:52:51 +00:00
createQuickNotif("{% trans %}Unknown UV code{% endtrans %}")
2019-10-21 08:34:46 +00:00
return
}
for (let key in data) {
if (data.hasOwnProperty(key)) {
const el = document.querySelector('[name="' + key + '"]')
if (el.tagName == 'TEXTAREA') {
el.parentNode.querySelector('.CodeMirror').CodeMirror.setValue(data[key])
} else {
el.value = data[key]
}
}
2019-10-17 23:28:59 +00:00
}
2019-10-20 15:09:36 +00:00
2019-10-21 14:52:51 +00:00
createQuickNotif('{% trans %}Successful autocomplete{% endtrans %}')
2019-10-21 08:34:46 +00:00
},
error: function(_, _, statusMessage) {
2019-10-21 14:52:51 +00:00
createQuickNotif('{% trans %}An error occured: {% endtrans %}' + statusMessage)
2019-10-21 08:34:46 +00:00
},
2019-10-17 23:28:59 +00:00
})
})
})
</script>
{% endblock %}