|
|
|
@ -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} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|