election_detail.jinja 11.8 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: 5px;
91 92 93
}

.candidate {
94 95
    display: flex;
    flex-flow: row nowrap;
96 97 98
}

.candidate__picture-wrapper {
99 100 101
    display: flex;
    justify-content: center;
    align-items: center;
102

103 104
    width: 150px;
    height: 150px;
105 106
    min-width: 150px;
    min-height: 150px;
107

108
    background-color: lightgrey;
109 110 111
}

.candidate__picture {
112 113
    max-width: 150px;
    max-height: 150px;
114 115 116
}

.candidate__infos {
117
    margin-left: 5px;
118 119 120
}

.candidate__full-name {
121
    display: block;
122

123
    font-weight: bolder;
124 125 126
}

.candidate__nick-name {
127
    font-style: italic;
128 129 130
}

.candidate__program {
131 132
    display: block;
    margin-top: 5px;
133 134 135
}

.candidate__vote-input {
136 137 138 139 140 141 142
    position: absolute;
    border: 0;
    height: 1px;
    width: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
143 144 145
}

.candidate__vote-choice {
146 147
    margin-top: 5px;
    padding: 15px;
148

149
    border: solid 1px darkgrey;
150

151 152 153
    color: dimgray;
    text-align: center;
    font-weight: bolder;
154 155
}

156 157
.candidate__vote-input:not(:checked):not(:disabled) + .candidate__vote-choice:hover,
.candidate__vote-input:not(:checked):not(:disabled) + .candidate__vote-choice:focus {
158
    background: lightgrey;
159
}
160

161
.candidate__vote-input:checked + .candidate__vote-choice {
162
    padding: 14px;
163 164 165
    border-width: 2px;
    border-color: darkgreen;
    color: darkgreen;
166 167
}

168 169 170 171 172 173
.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 {
174
    background: palegreen;
175 176
}

177 178 179 180 181
.role_error .candidate__vote-input:checked + .candidate__vote-choice {
    border-color: darkred;
    color: darkred;
}

182 183
.role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:hover,
.role_error .candidate__vote-input:checked:not(:disabled) + .candidate__vote-choice:focus {
184 185 186
    background: indianred;
}

187
.election__sumbit-section {
188
    margin-bottom: 5px;
189 190 191
}

.election__sumbit-button {
192 193 194 195
    display: block;
    width: 100%;
    padding: 20px;
    background: white;
196
    border: solid 15px #4081cb;
197 198 199 200
    text-align: center;
    font-size: 200%;
    font-weight: bolder;
    cursor: pointer;
201
}
202 203 204 205 206

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

208 209 210 211
.election__add-elements {
    margin-bottom: 5px;
}

212 213 214 215 216 217 218
.election__add-elements a {
    display: inline-block;
    border: solid 1px darkgrey;
    height: 20px;
    line-height: 20px;
    padding: 10px;
}
219 220 221
</style>
{%- endblock %}

Sli's avatar
Sli committed
222
{% block content %}
223 224
    <h3 class="election__title">{{ election.title }}</h3>
    <p class="election__description">{{ election.description }}</p>
225
    <hr>
226
    <section class="election__details">
227 228
        <p>
            {%- if election.is_vote_active %}
229
            {% trans %}Polls close {% endtrans %}
230 231 232 233 234
            {%- else %}
            {% trans %}Polls will open {% endtrans %}
            <time datetime="{{ election.start_date }}">{{ election.start_date|date("l d F Y") }}</time> at <time>{{ election.start_date|time("G:i") }}</time>
            {% trans %}and will close {% endtrans %}
            {%- endif %}
235 236
            <time datetime="{{ election.end_date }}">{{ election.end_date|date("l d F Y") }}</time> at <time>{{ election.end_date|time("G:i") }}</time>
        </p>
237
        {%- if election.has_voted(user) %}
238
        <p class="election__elector-infos">
239
            {%- if election.is_vote_active %}
240
            <span>{% trans %}You already have submitted your vote.{% endtrans %}</span>
241 242 243
            {%- else %}
            <span>{% trans %}You have voted in this election.{% endtrans %}</span>
            {%- endif %}
244 245
        </p>
        {%- endif %}
246
    </section>
247
    <section class="election__vote">
248 249 250 251 252 253
        <form action="/election/1/vote" method="post" class="election__vote-form" name="vote-form" id="vote-form">
            {% csrf_token %}
            <table>
                {%- set election_lists = election.election_list.all() -%}
                <caption></caption>
                <thead>
254
                    {%- if not election.has_voted(user) %}
255
                    <th>{% trans %}Blank vote{% endtrans %}</th>
256
                    {%- endif %}
257
                    {%- for election_list in election_lists %}
258
                    <th>{{election_list.title}}</th>
259
                    {%- endfor %}
260 261 262 263
                </thead>
                {%- for role in election.role.all() %}
                {%- set count = [0] %}
                {%- set role_data = election_form.data.getlist(role.title) if role.title in election_form.data else [] %}
264
                <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 ''}}">
265 266 267
                    <tr class="role__title">
                        <td colspan="{{election_lists.count() + 1}}">
                            <span>{{role.title}}</span>
268
                            {%- if role.max_choice > 1 and not election.has_voted(user) %}
269
                            <strong class="role__multiple-choices-label">{% trans %}You may choose up to{% endtrans %} {{ role.max_choice }} {% trans %}people.{% endtrans %}</strong>
270 271 272 273 274 275 276 277 278
                            {%- 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">
279
                        {%- if election.can_vote(user) %}
280
                        <td class="list-per-role">
281 282
                            {%- if role.max_choice == 1 %}
                            <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 '' }}>
283 284 285 286 287 288
                            <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 %}
                        </td>
289
                        {%- endif %}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                        {%- for election_list in election_lists %}
                        <td class="list-per-role">
                            <ul class="list-per-role__candidates">
                                {%- for candidature in election_list.candidature.filter(role=role) %}
                                <li class="list-per-role__candidate">
                                    <figure class="candidate">
                                        <div class="candidate__picture-wrapper">
                                            {%- if candidature.user.profile_pict %}
                                            <img class="candidate__picture" src="{{candidature.user.profile_pict.get_download_url()}}" alt="{% trans %}Profile{% endtrans %}">
                                            {%- endif %}
                                        </div>
                                        <figcaption class="candidate__infos">
                                            <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>
                                        </figcaption>
                                    </figure>
306 307
                                    {%- 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">
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                                    <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) %}
                                    {%- endif %}
                                </li>
                                {%- endfor %}
                            </ul>
                        </td>
                        {%- endfor %}
                    </tr>
                </tbody>
                {%- endfor %}
            </table>
        </form>
323
    </section>
324
    {%- if not election.has_voted(user) %}
325
    <section class="election__sumbit-section">
326
        <button class="election__sumbit-button" form="vote-form">{% trans %}Submit the vote !{% endtrans %}</button>
327
    </section>
328
    {%- endif %}
329
    {%- if user.can_edit(election) %}
330 331 332 333
    <section class="election__add-elements">
        <a href="{{url('election:create_list')}}">{% trans %}Add a new list{% endtrans %}</a>
        <a href="{{url('election:create_role')}}">{% trans %}Add a new role{% endtrans %}</a>
    </section>
334
    {%- endif %}
335
    {%- if election.can_candidate(user) or user.can_edit(election) %}
336 337 338 339 340 341 342
    <section class="election__add-candidature">
        <form action="{{url('election:candidate', election_id=election.id)}}" method="post">{{candidate_form}}
        {% csrf_token %}
        <p><input type="submit" value="{% trans %}Candidate{% endtrans %}" /></p>
        </form>
    </section>
    {%- endif %}
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
{% 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>
Sli's avatar
Sli committed
370
{% endblock %}