You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
tea/choose/templates/vote.html

224 lines
6.0 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button class="btn" onclick="location.replace('/logout');">Выйти</button>
{% if not voted %}
<button class="btn" onclick="location.href='/choose'">Проголосовать</button>
{% endif %}
<table>
<tbody>
<tr>
<th>Список чаев</th>
<th>Выбрали</th>
<th>Проголосовали</th>
<th>Всего голосов</th>
<th></th>
</tr>
{% for choice in choices %}
<tr class="{{ choice.class }}">
<td>
<ul>
{% for tea in choice.teas %}
<li>{{ tea }}</li>
{% endfor %}
</ul>
</td>
<td>
<ul>
{% for choice in choice.chosed %}
<li>{{ choice }}</li>
{% endfor %}
</ul>
</td>
<td>
<ul>
{% for vote in choice.voted %}
<li>{{ vote }}</li>
{% endfor %}
</ul>
</td>
<td>
{{ choice.votes }}
</td>
<td style="text-align: center;">
{% if choice.class == 'choice' %}
<button class="btn" onclick="vote({{ choice.id }})"
{% if request.user.username in choice.voted %}disabled{% endif %}>+
</button>
<button class="btn" onclick="cancel_vote({{ choice.id }})"
{% if request.user.username not in choice.voted %}disabled{% endif %}>-
</button>
{% endif %}
{% if choice.class == 'my_choice' %}
<button class="btn" style="width: 138px" onclick="location.href='/choose'">Изменить выбор</button>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
<style>
table {
border: none;
margin-bottom: 20px;
}
table th {
font-weight: bold;
text-align: left;
border: none;
padding: 10px 15px;
background: #d8d8d8;
font-size: 14px;
}
table tr th:first-child {
border-radius: 8px 0 0 8px;
}
table tr th:last-child {
border-radius: 0 8px 8px 0;
width: 35px;
}
table td {
text-align: left;
border: none;
padding: 10px 15px;
font-size: 16px;
vertical-align: center;
}
table tbody tr:nth-child(even) {
background: #f3f3f3;
}
table tr td:first-child {
border-radius: 8px 0 0 8px;
}
table tr td:last-child {
border-radius: 0 8px 8px 0;
}
table tbody tr.my_choice {
background: #fffdcd;
}
ul {
display: block;
list-style-type: disc;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0px;
margin-inline-end: 0px;
padding-inline-start: 10px;
}
.btn {
display: inline-block;
box-sizing: border-box;
padding: 0 13px;
margin: 0 15px 15px 0;
outline: none;
border: 1px solid transparent;
border-radius: 3px;
height: 32px;
line-height: 32px;
font-size: 14px;
font-weight: 500;
text-decoration: none;
color: #fff;
background-color: #65a3be;
cursor: pointer;
user-select: none;
appearance: none;
touch-action: manipulation;
}
.btn:focus-visible {
box-shadow: 0 0 0 3px lightskyblue;
}
.btn:hover {
border-color: transparent;
background-color: #4986a1;
color: #fff;
}
.btn:active {
border-color: #6f9cbc !important;
background-color: #367089 !important;
}
.btn:disabled {
background-color: #afafaf;
color: #fff;
pointer-events: none;
}
</style>
<script>
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
function vote(id) {
const request = new Request(
'confirm-vote',
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({vote: id})
}
);
fetch(request).then((response) => response.json())
.then((data) => {
if (data.response == 'ok' || data.response == 'not found') {
window.location.reload();
} else {
window.alert(data.response)
}
})
}
function cancel_vote(id) {
const request = new Request(
'confirm-cancel-vote',
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({vote: id})
}
);
fetch(request).then((response) => response.json())
.then((data) => {
if (data.response == 'ok' || data.response == 'not found') {
window.location.reload();
} else {
window.alert(data.response)
}
})
}
</script>
</html>