|
|
|
@ -44,12 +44,14 @@ def list_of_teas(): |
|
|
|
|
|
|
|
|
|
|
|
def list_of_choices(request): |
|
|
|
def list_of_choices(request): |
|
|
|
arr = [] |
|
|
|
arr = [] |
|
|
|
|
|
|
|
voted = False |
|
|
|
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 |
|
|
|
json.loads(my_choice.choice) |
|
|
|
json.loads(my_choice.choice) |
|
|
|
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())), |
|
|
|
'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}) |
|
|
|
@ -59,10 +61,11 @@ def list_of_choices(request): |
|
|
|
for choice in choices: |
|
|
|
for choice in choices: |
|
|
|
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())), |
|
|
|
'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}) |
|
|
|
return {'choices': arr} |
|
|
|
return {'choices': arr, 'voted': voted} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_requiered |
|
|
|
@login_requiered |
|
|
|
@ -72,6 +75,7 @@ def choose(request): |
|
|
|
return HttpResponse(template.render(context, request)) |
|
|
|
return HttpResponse(template.render(context, request)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_requiered |
|
|
|
def home(request): |
|
|
|
def home(request): |
|
|
|
context = list_of_choices(request) |
|
|
|
context = list_of_choices(request) |
|
|
|
template = loader.get_template('vote.html') |
|
|
|
template = loader.get_template('vote.html') |
|
|
|
@ -118,3 +122,19 @@ def confirm_vote(request): |
|
|
|
return JsonResponse({'response': 'ok'}) |
|
|
|
return JsonResponse({'response': 'ok'}) |
|
|
|
else: |
|
|
|
else: |
|
|
|
return JsonResponse({'response': 'unauthenticated'}) |
|
|
|
return JsonResponse({'response': 'unauthenticated'}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def confirm_cancel_vote(request): |
|
|
|
|
|
|
|
if request.user.is_authenticated: |
|
|
|
|
|
|
|
vote = json.loads(request.body.decode(encoding='UTF-8')) |
|
|
|
|
|
|
|
choice = Choice.objects.filter(id=vote['vote']).first() |
|
|
|
|
|
|
|
if choice is None: |
|
|
|
|
|
|
|
return JsonResponse({'response': 'not found'}) |
|
|
|
|
|
|
|
my_choice = Choice.objects.filter(users__in=[request.user]).first() |
|
|
|
|
|
|
|
if my_choice is not None: |
|
|
|
|
|
|
|
if my_choice.id == choice.id: |
|
|
|
|
|
|
|
return JsonResponse({'response': 'WAT'}) |
|
|
|
|
|
|
|
choice.votes.remove(request.user) |
|
|
|
|
|
|
|
return JsonResponse({'response': 'ok'}) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
return JsonResponse({'response': 'unauthenticated'}) |
|
|
|
|