election_detail.jinja 14.2 KB
Newer Older
Sli's avatar
Sli committed
1
2
3
{% extends "core/base.jinja" %}

{% block title %}
4
{{ object.title }}
Sli's avatar
Sli committed
5
6
{% endblock %}

7
8
9
{% block head %}
{{ super() -}}
<style type="text/css">
10
time {
11
    font-weight: bolder;
12
13
14
}

th {
15
16
17
18
19
20
21
    padding: 5px;
    margin: 5px;
    border: solid 1px darkgrey;
    border-collapse: collapse;
    vertical-align: top;
    overflow: hidden;
    text-overflow: ellipsis;
22
23
24
}

.election__title {
25
    margin: 0;
26
    margin-bottom: 5px;
27
28
29
}

.election__description {
30
    margin: 0;
31
32
33
}

.election__details {
34
    margin-bottom: 5px;
35
36
}

37
.election__details p {
38
    margin: 0;
39
40
41
}

.election__details p:not(:last-child) {
42
    margin-bottom: 5px;
43
44
45
}

.election__elector-infos {
46
47
48
49
50
51
52
53
    font-weight: bolder;
    color: darkgreen;
}

.election__vote {
    margin-bottom: 5px;
}

54
55
56
57
.election__vote-form {
    width: auto;
}

58
59
60
61
.role {

}

62
.role .role__title {
63
    background: lightgrey;
64
65
}

66
.role__multiple-choices-label {
67
    color: darkgreen;
68
69
}

70
71
72
73
.role__error {
    color: darkred;
}

74
.role .role_candidates {
75
    background: white;
76
77
}

78
.list-per-role {
79
80
    padding: 5px;
    max-width: 310px;
81
82
83
84

}

.list-per-role__candidates {
85
86
    list-style: none;
    margin: 0;
87
88
89
}

.list-per-role__candidate:not(:last-child) {
90
    margin-bottom: 15px;
91
92
}

93
.candidate__infos {
94
95
    display: flex;
    flex-flow: row nowrap;
96
97
}

98
99
100
101
.candidate__infos:not(:last-child) {
    margin-bottom: 5px;
}

102
.candidate__picture-wrapper {
103
104
105
    display: flex;
    justify-content: center;
    align-items: center;
106

107
108
    width: 150px;
    height: 150px;
109
110
    min-width: 150px;
    min-height: 150px;
111

112
    background-color: lightgrey;
113
114
115
}

.candidate__picture {
116
117
    max-width: 150px;
    max-height: 150px;
118
119
}

120
.candidate__details {
121
    margin-left: 5px;
122
123
124
}

.candidate__full-name {
125
    display: block;
126

127
    font-weight: bolder;
128
129
130
}

.candidate__nick-name {
131
    font-style: italic;
132
133
134
}

.candidate__program {
135
136
    display: block;
    margin-top: 5px;
137
138
139
}

.candidate__vote-input {
140
141
142
143
144
145
146
    position: absolute;
    border: 0;
    height: 1px;
    width: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
147
148
149
}

.candidate__vote-choice {
150
151
    margin-top: 5px;
    padding: 15px;
152

153
    border: solid 1px darkgrey;
154

155
156
157
    color: dimgray;
    text-align: center;
    font-weight: bolder;
158
159
}

160
161
.candidate__vote-input:not(:checked):not(:disabled) + .candidate__vote-choice:hover,
.candidate__vote-input:not(:checked):not(:disabled) + .candidate__vote-choice:focus {
162
    background: lightgrey;
163
}
164

165
.candidate__vote-input:checked + .candidate__vote-choice {
166
    padding: 14px;
167
168
169
    border-width: 2px;
    border-color: darkgreen;
    color: darkgreen;
170
171
}

172
173
174
175
176
177
.candidate__vote-input:not(:disabled) + .candidate__vote-choice {
    cursor: pointer;
}

.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover,
.candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus {
178
    background: palegreen;
179
180
}

181
182
183
184
185
.role_error .candidate__vote-input:checked + .candidate__vote-choice {
    border-color: darkred;
    color: darkred;
}

186
187
.role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover,
.role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus {
188
189
190
    background: indianred;
}

191
192
193
194
.election__results {
    text-align: center;
}

195
.election__sumbit-section {
196
    margin-bottom: 5px;
197
198
199
}

.election__sumbit-button {
200
201
202
203
    display: block;
    width: 100%;
    padding: 20px;
    background: white;
204
    border: solid 15px #4081cb;
205
206
207
208
    text-align: center;
    font-size: 200%;
    font-weight: bolder;
    cursor: pointer;
209
}
210
211
212
213
214

.election__sumbit-button:hover,
.election__sumbit-button:focus {
    background-color: lightskyblue;
}
215

216
217
218
219
.election__add-elements {
    margin-bottom: 5px;
}

220
221
222
223
224
225
226
.election__add-elements a {
    display: inline-block;
    border: solid 1px darkgrey;
    height: 20px;
    line-height: 20px;
    padding: 10px;
}
227
228
229
</style>
{%- endblock %}

Sli's avatar
Sli committed
230
{% block content %}
231
232
    <h3 class="election__title">{{ election.title }}</h3>
    <p class="election__description">{{ election.description }}</p>
233
    <hr>
234
    <section class="election__details">
235
236
        <p>
            {%- if election.is_vote_active %}
237
            {% trans %}Polls close {% endtrans %}
238
239
            {%- elif election.is_vote_finished %}
            {% trans %}Polls closed {% endtrans %}
240
241
            {%- else %}
            {% trans %}Polls will open {% endtrans %}
242
243
            <time datetime="{{ election.start_date }}">{{ election.start_date|localtime|date(DATETIME_FORMAT)}}</time>
                        {% trans %} at {% endtrans %}<time>{{ election.start_date|localtime|time(DATETIME_FORMAT)}}</time>
244
245
            {% trans %}and will close {% endtrans %}
            {%- endif %}
246
247
            <time datetime="{{ election.end_date }}">{{ election.end_date|localtime|date(DATETIME_FORMAT)}}</time>
                        {% trans %} at {% endtrans %}<time>{{ election.end_date|localtime|time(DATETIME_FORMAT)}}</time>
248
        </p>
249
        {%- if election.has_voted(user) %}
250
        <p class="election__elector-infos">
251
            {%- if election.is_vote_active %}
252
            <span>{% trans %}You already have submitted your vote.{% endtrans %}</span>
253
254
255
            {%- else %}
            <span>{% trans %}You have voted in this election.{% endtrans %}</span>
            {%- endif %}
256
257
        </p>
        {%- endif %}
258
    </section>
259
    <section class="election__vote">
260
        <form action="{{ url('election:vote', election.id) }}" method="post" class="election__vote-form" name="vote-form" id="vote-form">
261
262
            {% csrf_token %}
            <table>
Sli's avatar
Sli committed
263
                {%- set election_lists = election.election_lists.all() -%}
264
265
266
                <caption></caption>
                <thead>
                    <th>{% trans %}Blank vote{% endtrans %}</th>
267
                    {%- for election_list in election_lists %}
268
                    <th>{{ election_list.title }}</th>
269
                    {%- endfor %}
270
                </thead>
Sli's avatar
Sli committed
271
                {%- for role in election.roles.all() %}
272
273
                {%- set count = [0] %}
                {%- set role_data = election_form.data.getlist(role.title) if role.title in election_form.data else [] %}
274
                <tbody data-max-choice="{{role.max_choice}}" class="role{{ ' role_error' if role.title in election_form.errors else '' }}{{ ' role__multiple-choices' if role.max_choice > 1 else ''}}">
275
                    <tr class="role__title">
276
277
                        <td colspan="{{ election_lists.count() + 1 }}">
                            <span>{{ role.title }}</span>
Sli's avatar
Sli committed
278
279
280
281
                            {% if user.can_edit(role) and election.is_vote_editable -%}
                                <a href="{{url('election:update_role', role_id=role.id)}}">{% trans %}Edit{% endtrans %}</a>
                                <a href="{{url('election:delete_role', role_id=role.id)}}">{% trans %}Delete{% endtrans %}</a>
                            {%- endif -%}
282
                            {%- if role.max_choice > 1 and not election.has_voted(user) and election.can_vote(user) %}
283
                            <strong class="role__multiple-choices-label">{% trans %}You may choose up to{% endtrans %} {{ role.max_choice }} {% trans %}people.{% endtrans %}</strong>
284
285
286
287
288
289
290
291
292
293
                            {%- endif %}
                            {%- if election_form.errors[role.title] is defined %}
                            {%- for error in election_form.errors.as_data()[role.title] %}
                            <strong class="role__error">{{ error.message }}</strong>
                            {%- endfor %}
                            {%- endif %}
                        </td>
                    </tr>
                    <tr class="role_candidates">
                        <td class="list-per-role">
294
                            {%- if role.max_choice == 1 and election.can_vote(user) %}
295
                            <input id="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-input" type="radio" name="{{ role.title }}" value {{ '' if role_data in election_form else 'checked' }} {{ 'disabled' if election.has_voted(user) else '' }}>
296
297
298
299
300
                            <label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice">
                                <span>{% trans %}Choose blank vote{% endtrans %}</span>
                            </label>
                            {%- set _ = count.append(count.pop() + 1) %}
                            {%- endif %}
301
                            {%- if election.is_vote_finished %}
302
303
                            {%- set results = election_results[role.title]['blank vote'] %}
                            <div class="election__results">
304
                                <strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %)</strong>
305
306
                            </div>
                            {%- endif %}
307
308
309
310
                        </td>
                        {%- for election_list in election_lists %}
                        <td class="list-per-role">
                            <ul class="list-per-role__candidates">
Sli's avatar
Sli committed
311
                                {%- for candidature in election_list.candidatures.filter(role=role) %}
312
313
                                <li class="list-per-role__candidate candidate">
                                    <figure class="candidate__infos">
314
                                        <div class="candidate__picture-wrapper">
315
                                            {%- if candidature.user.profile_pict and user.is_subscriber_viewable %}
316
                                            <img class="candidate__picture" src="{{ candidature.user.profile_pict.get_download_url() }}" alt="{% trans %}Profile{% endtrans %}">
317
318
                                            {%- endif %}
                                        </div>
319
                                        <figcaption class="candidate__details">
320
321
                                            <cite class="candidate__full-name">{{ candidature.user.first_name }} <em class="candidate__nick-name">{{candidature.user.nick_name or ''}} </em>{{ candidature.user.last_name }}</cite>
                                            <q class="candidate__program">{{ candidature.program or '' }}</q>
Sli's avatar
Sli committed
322
323
324
325
326
327
328
329
                                            {%- if user.can_edit(candidature) -%}
                                            {% if election.is_vote_editable %}
                                            <a href="{{url('election:update_candidate', candidature_id=candidature.id)}}">{% trans %}Edit{% endtrans %}</a>
                                            {% endif %}
                                            {% if election.can_candidate -%}
                                            <a href="{{url('election:delete_candidate', candidature_id=candidature.id)}}">{% trans %}Delete{% endtrans %}</a>
                                            {%- endif -%}
                                            {%- endif -%}
330
331
                                        </figcaption>
                                    </figure>
332
333
                                    {%- if election.can_vote(user) %}
                                    <input id="id_{{ role.title }}_{{ count[0] }}" type="{{ 'checkbox' if role.max_choice > 1 else 'radio' }}" {{ 'checked' if candidature.id|string in role_data else '' }} {{ 'disabled' if election.has_voted(user) else '' }} name="{{ role.title }}" value="{{ candidature.id }}" class="candidate__vote-input">
334
335
336
337
                                    <label for="id_{{ role.title }}_{{ count[0] }}" class="candidate__vote-choice">
                                        <span>{% trans %}Choose{% endtrans %} {{ candidature.user.nick_name or candidature.user.first_name }}</span>
                                    </label>
                                    {%- set _ = count.append(count.pop() + 1) %}
338
                                    {%- endif %}
339
                                    {%- if election.is_vote_finished %}
340
341
                                    {%- set results = election_results[role.title][candidature.user.username] %}
                                    <div class="election__results">
342
                                        <strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ results.percent }} %)</strong>
343
                                    </div>
344
345
346
347
348
349
350
351
352
353
354
                                    {%- endif %}
                                </li>
                                {%- endfor %}
                            </ul>
                        </td>
                        {%- endfor %}
                    </tr>
                </tbody>
                {%- endfor %}
            </table>
        </form>
355
    </section>
356
    {%- if not election.has_voted(user) and election.can_vote(user) %}
357
    <section class="election__sumbit-section">
358
        <button class="election__sumbit-button" form="vote-form">{% trans %}Submit the vote !{% endtrans %}</button>
359
    </section>
360
    {%- endif %}
361
    <section class="election__add-elements">
362
    {%- if (election.can_candidate(user) and election.is_candidature_active) or (user.can_edit(election) and election.is_vote_editable) %}
363
        <a href="{{ url('election:candidate', election_id=object.id) }}">{% trans %}Candidate{% endtrans %}</a>
Sli's avatar
Sli committed
364
    {%- endif %}
365
        <a href="{{ url('election:create_list', election_id=object.id) }}">{% trans %}Add a new list{% endtrans %}</a>
Sli's avatar
Sli committed
366
    {%- if user.can_edit(election) %}
Sli's avatar
Sli committed
367
        {% if election.is_vote_editable %}
368
        <a href="{{ url('election:create_role', election_id=object.id) }}">{% trans %}Add a new role{% endtrans %}</a>
Sli's avatar
Sli committed
369
        {% endif %}
370
        <a href="{{ url('election:update', election_id=object.id) }}">{% trans %}Edit{% endtrans %}</a>
371
372
    {%- endif %}
    </section>
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{% endblock %}

{% block script %}
{{ super() }}
<script type="text/javascript">
document.querySelectorAll('.role__multiple-choices').forEach(setupRestrictions);

function setupRestrictions(role) {
    var selectedChoices = [];
    role.querySelectorAll('input').forEach(setupRestriction);

    function setupRestriction(choice) {
        if (choice.checked)
            selectedChoices.push(choice);
        choice.addEventListener('change', onChange);

        function onChange() {
            if (choice.checked)
                selectedChoices.push(choice);
            else
                selectedChoices.splice(selectedChoices.indexOf(choice), 1);
            while (selectedChoices.length > role.dataset.maxChoice)
                selectedChoices.shift().checked = false;
        }
    }
}
</script>
400
{% endblock %}