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.
154 lines
5.8 KiB
154 lines
5.8 KiB
import json
|
|
from collections import OrderedDict
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
from django.http import JsonResponse, HttpResponse
|
|
from django.shortcuts import render, redirect
|
|
from django.template import loader
|
|
|
|
from .models import Tea, TeaType, TeaCategory, Pic, Choice
|
|
# Create your views here.
|
|
from .utils import get_extension
|
|
|
|
|
|
def login_view(request):
|
|
return render(request, 'login.html')
|
|
|
|
|
|
def login_requiered(func):
|
|
def wrapper(*args, **kwargs):
|
|
request = args[0]
|
|
if request.user.is_authenticated:
|
|
return func(*args, **kwargs)
|
|
else:
|
|
return redirect('login_view')
|
|
|
|
return wrapper
|
|
|
|
|
|
def list_of_teas():
|
|
dic = OrderedDict()
|
|
for cat in TeaCategory.objects.all():
|
|
dic[cat.name] = OrderedDict()
|
|
|
|
for tt in TeaType.objects.all():
|
|
if tt.preferred is not None:
|
|
dic[tt.category.name][tt.name] = OrderedDict()
|
|
dic[tt.category.name][tt.name]['preferred'] = tt.preferred.name
|
|
dic[tt.category.name][tt.name]['price'] = int(tt.preferred.price / 100)
|
|
dic[tt.category.name][tt.name]['pic'] = '/media/' + str(tt.preferred.pic.id) + '.' + get_extension(
|
|
tt.preferred.pic.href)
|
|
dic[tt.category.name][tt.name]['ms'] = [tt.preferred.m * x for x in range(0, 11)]
|
|
|
|
return dic
|
|
|
|
|
|
def list_of_choices(request):
|
|
arr = []
|
|
voted = False
|
|
my_choice = Choice.objects.filter(users__in=[request.user]).first()
|
|
if my_choice is not None:
|
|
voted = True
|
|
json.loads(my_choice.choice)
|
|
arr.append({'voted': [user.username for user in my_choice.votes.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
|
|
json.loads(my_choice.choice).items()]),
|
|
'class': 'my_choice', 'id': my_choice.id})
|
|
choices = list(Choice.objects.all().exclude(id=my_choice.id))
|
|
else:
|
|
choices = list(Choice.objects.all())
|
|
for choice in choices:
|
|
arr.append({'voted': [user.username for user in choice.votes.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
|
|
json.loads(choice.choice).items()]),
|
|
'class': 'choice', 'id': choice.id})
|
|
return {'choices': arr, 'voted': voted}
|
|
|
|
|
|
def get_my_choice(request):
|
|
my_choice = Choice.objects.filter(users__in=[request.user]).first()
|
|
if my_choice is None:
|
|
return {}
|
|
else:
|
|
return {tea_id: str(mass) for tea_id, mass in json.loads(my_choice.choice).items()}
|
|
|
|
|
|
@login_requiered
|
|
def choose(request):
|
|
context = {'categories': list_of_teas(), 'teas': get_my_choice(request)}
|
|
template = loader.get_template('choose.html')
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
@login_requiered
|
|
def home(request):
|
|
context = list_of_choices(request)
|
|
template = loader.get_template('vote.html')
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
def confirm_choose(request):
|
|
if request.user.is_authenticated:
|
|
choice = json.loads(request.body.decode(encoding='UTF-8'))
|
|
d = {}
|
|
for key in choice:
|
|
if choice[key] != 0:
|
|
tea = Tea.objects.get(name=key)
|
|
d[tea.id] = choice[key]
|
|
choice_string = json.dumps(d, sort_keys=True)
|
|
previous = Choice.objects.filter(users__in=[request.user]).first()
|
|
if previous is not None:
|
|
if previous.choice == choice_string:
|
|
return JsonResponse({'response': 'unchanged'})
|
|
previous.users.remove(request.user)
|
|
if not previous.users.exists():
|
|
previous.delete()
|
|
my_choice, _ = Choice.objects.get_or_create(choice=choice_string)
|
|
if request.user not in list(my_choice.users.all()):
|
|
my_choice.users.add(request.user)
|
|
if request.user in list(my_choice.votes.all()):
|
|
my_choice.votes.remove(request.user)
|
|
return JsonResponse({'response': 'ok'})
|
|
else:
|
|
return JsonResponse({'response': 'unauthenticated'})
|
|
|
|
|
|
def confirm_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': 'FUK U'})
|
|
choice.votes.add(request.user)
|
|
return JsonResponse({'response': 'ok'})
|
|
else:
|
|
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'})
|
|
|
|
|
|
def logout_view(request):
|
|
logout(request)
|
|
return redirect(login_view)
|
|
|