uv_edit.jinja 2.56 KB
Newer Older
tleb's avatar
tleb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{% 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 %}

tleb's avatar
tleb committed
15 16 17 18 19
    {% if field.is_hidden %}

        {{ field }}

    {% else %}
tleb's avatar
tleb committed
20 21 22 23 24 25 26
    <p>
        {{ field.errors }}
        <label for="{{ field.name }}">{{ field.label }}</label>
        {{ field }}


        {% if field.name == 'code' %}
tleb's avatar
tleb committed
27
        <button type="button" id="autofill">{% trans %}Import from UTBM{% endtrans %}</button>
tleb's avatar
tleb committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        {% 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()
tleb's avatar
tleb committed
51
            if (today.getMonth() < 7) {  // student year starts in september
tleb's avatar
tleb committed
52 53 54
                year--
            }
            const url = "{{ url('api:uv_endpoint') }}?year=" + year + "&code=" + codeInput.value
tleb's avatar
tleb committed
55
            deleteQuickNotifs()
tleb's avatar
tleb committed
56

tleb's avatar
tleb committed
57 58 59 60 61
            $.ajax({
                dataType: "json",
                url: url,
                success: function(data, _, xhr) {
                    if (xhr.status != 200) {
tleb's avatar
tleb committed
62
                        createQuickNotif("{% trans %}Unknown UV code{% endtrans %}")
tleb's avatar
tleb committed
63 64 65 66 67 68 69 70 71 72 73 74
                        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]
                            }

                        }
tleb's avatar
tleb committed
75
                    }
tleb's avatar
tleb committed
76

tleb's avatar
tleb committed
77
                    createQuickNotif('{% trans %}Successful autocomplete{% endtrans %}')
tleb's avatar
tleb committed
78 79
                },
                error: function(_, _, statusMessage) {
tleb's avatar
tleb committed
80
                    createQuickNotif('{% trans %}An error occured: {% endtrans %}' + statusMessage)
tleb's avatar
tleb committed
81
                },
tleb's avatar
tleb committed
82 83 84 85 86
            })
        })
    })
</script>
{% endblock %}