paov 4 years ago
parent 77b7f43ba1
commit 5d61568386
  1. 2
      choose/templates/choose.html
  2. 9
      choose/templates/vote.html
  3. 13
      choose/views.py

@ -108,7 +108,7 @@
function sum_price() { function sum_price() {
s = 0 s = 0
for (let tea in choice) { for (let tea in choice) {
s += choice[tea] / 50 * parseInt(a[tea].price) s += choice[tea] / a[tea].ms[1] * parseInt(a[tea].price)
} }
const butt = document.getElementById('confirm_button'); const butt = document.getElementById('confirm_button');
const sum_h = document.getElementById('sum_h'); const sum_h = document.getElementById('sum_h');

@ -16,6 +16,7 @@
<th>Выбрали</th> <th>Выбрали</th>
<th>Проголосовали</th> <th>Проголосовали</th>
<th>Всего голосов</th> <th>Всего голосов</th>
<th>Цена</th>
<th></th> <th></th>
</tr> </tr>
{% for choice in choices %} {% for choice in choices %}
@ -44,6 +45,13 @@
<td> <td>
{{ choice.votes }} {{ choice.votes }}
</td> </td>
<td>
{{ choice.sum }}
<br>
С человека:
<br>
{{ choice.sum_per_usr }}
</td>
<td style="text-align: center;"> <td style="text-align: center;">
{% if choice.class == 'choice' %} {% if choice.class == 'choice' %}
<button class="btn" onclick="vote({{ choice.id }})" <button class="btn" onclick="vote({{ choice.id }})"
@ -201,6 +209,7 @@
} }
}) })
} }
function cancel_vote(id) { function cancel_vote(id) {
const request = new Request( const request = new Request(
'confirm-cancel-vote', 'confirm-cancel-vote',

@ -2,6 +2,7 @@ import json
from collections import OrderedDict from collections import OrderedDict
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.http import JsonResponse, HttpResponse from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.template import loader from django.template import loader
@ -53,26 +54,32 @@ def list_of_teas(request):
def list_of_choices(request): def list_of_choices(request):
arr = [] arr = []
voted = False voted = False
user_num = len(list(User.objects.all())) - 1
my_choice = Choice.objects.filter(users__in=[request.user]).first() my_choice = Choice.objects.filter(users__in=[request.user]).first()
if my_choice is not None: if my_choice is not None:
voted = True voted = True
json.loads(my_choice.choice) summ = sum([Tea.objects.get(id=tea_id).price*mass/Tea.objects.get(id=tea_id).m for tea_id, mass in json.loads(my_choice.choice).items()])
arr.append({'voted': [user.username for user in my_choice.votes.all()], arr.append({'voted': [user.username for user in my_choice.votes.all()],
'chosed': [user.username for user in my_choice.users.all()], 'chosed': [user.username for user in my_choice.users.all()],
'votes': len(list(my_choice.votes.all())) + len(list(my_choice.users.all())), 'votes': len(list(my_choice.votes.all())) + len(list(my_choice.users.all())),
'teas': sorted([Tea.objects.get(id=int(tea_id)).name + ':' + str(mass) for tea_id, mass in 'teas': sorted([Tea.objects.get(id=int(tea_id)).name + ':' + str(mass) for tea_id, mass in
json.loads(my_choice.choice).items()]), json.loads(my_choice.choice).items()]),
'class': 'my_choice', 'id': my_choice.id}) 'class': 'my_choice', 'id': my_choice.id,
'sum': summ,
'sum_per_usr': str(summ / user_num)})
choices = list(Choice.objects.all().exclude(id=my_choice.id)) choices = list(Choice.objects.all().exclude(id=my_choice.id))
else: else:
choices = list(Choice.objects.all()) choices = list(Choice.objects.all())
for choice in choices: for choice in choices:
summ = sum([Tea.objects.get(id=tea_id).price*mass/Tea.objects.get(id=tea_id).m for tea_id, mass in json.loads(choice.choice).items()])
arr.append({'voted': [user.username for user in choice.votes.all()], arr.append({'voted': [user.username for user in choice.votes.all()],
'chosed': [user.username for user in choice.users.all()], 'chosed': [user.username for user in choice.users.all()],
'votes': len(list(choice.votes.all())) + len(list(choice.users.all())), 'votes': len(list(choice.votes.all())) + len(list(choice.users.all())),
'teas': sorted([Tea.objects.get(id=int(tea_id)).name + ':' + str(mass) for tea_id, mass in 'teas': sorted([Tea.objects.get(id=int(tea_id)).name + ':' + str(mass) for tea_id, mass in
json.loads(choice.choice).items()]), json.loads(choice.choice).items()]),
'class': 'choice', 'id': choice.id}) 'class': 'choice', 'id': choice.id,
'sum': summ,
'sum_per_usr': str(summ / user_num)})
return {'choices': arr, 'voted': voted} return {'choices': arr, 'voted': voted}

Loading…
Cancel
Save