import json from collections import OrderedDict from django.contrib.auth import authenticate, login 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) return {'categories': dic, 'ms': [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]} def list_of_choices(request): arr = [] my_choice = Choice.objects.filter(users__in=[request.user]).first() if my_choice is not None: 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()], '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()], '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} @login_requiered def choose(request): context = list_of_teas() template = loader.get_template('choose.html') return HttpResponse(template.render(context, request)) 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'})