uv_create.jinja 1.89 KB
Newer Older
tleb's avatar
tleb committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{% 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 %}

    {% if not field.is_hidden %}
    <p>
        {{ field.errors }}
        <label for="{{ field.name }}">{{ field.label }}</label>
        {{ field }}


        {% if field.name == 'code' %}
        <a href="javascript:void(0)" id="autofill">Fill</a>
        {% 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()
            if (today.getMonth() < 7) {  // student year, starts in september
                year--
            }
            const url = "{{ url('api:uv_endpoint') }}?year=" + year + "&code=" + codeInput.value
            $.getJSON(url, function(data) {
                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]
                        }

                    }
                }
            })
        })

    })
</script>
{% endblock %}