|
|
|
@ -1,15 +1,37 @@ |
|
|
|
import json |
|
|
|
import json |
|
|
|
from collections import OrderedDict |
|
|
|
from collections import OrderedDict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from django.contrib.auth import authenticate, login |
|
|
|
from django.http import JsonResponse, HttpResponse |
|
|
|
from django.http import JsonResponse, HttpResponse |
|
|
|
from django.shortcuts import render |
|
|
|
from django.shortcuts import render, redirect |
|
|
|
from django.template import loader |
|
|
|
from django.template import loader |
|
|
|
|
|
|
|
|
|
|
|
from .models import Tea, TeaType, TeaCategory, Pic |
|
|
|
from .models import Tea, TeaType, TeaCategory, Pic, Choice |
|
|
|
# Create your views here. |
|
|
|
# Create your views here. |
|
|
|
from .utils import get_extension |
|
|
|
from .utils import get_extension |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def login_view(request): |
|
|
|
|
|
|
|
if request.method == 'POST': |
|
|
|
|
|
|
|
username = request.POST['username'] |
|
|
|
|
|
|
|
password = request.POST['password'] |
|
|
|
|
|
|
|
user = authenticate(username=username, password=password) |
|
|
|
|
|
|
|
if user is not None and user.is_active and user.is_authenticated: |
|
|
|
|
|
|
|
login(request, user) |
|
|
|
|
|
|
|
return redirect(home) |
|
|
|
|
|
|
|
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(): |
|
|
|
def list_of_teas(): |
|
|
|
dic = OrderedDict() |
|
|
|
dic = OrderedDict() |
|
|
|
for cat in TeaCategory.objects.all(): |
|
|
|
for cat in TeaCategory.objects.all(): |
|
|
|
@ -20,12 +42,13 @@ def list_of_teas(): |
|
|
|
dic[tt.category.name][tt.name] = OrderedDict() |
|
|
|
dic[tt.category.name][tt.name] = OrderedDict() |
|
|
|
dic[tt.category.name][tt.name]['preferred'] = tt.preferred.name |
|
|
|
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]['price'] = int(tt.preferred.price / 100) |
|
|
|
dic[tt.category.name][tt.name]['pic'] = str(tt.preferred.pic.id) + '.' + get_extension( |
|
|
|
dic[tt.category.name][tt.name]['pic'] = '/media/' + str(tt.preferred.pic.id) + '.' + get_extension( |
|
|
|
tt.preferred.pic.href) |
|
|
|
tt.preferred.pic.href) |
|
|
|
|
|
|
|
|
|
|
|
return {'categories': dic, 'ms': [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]} |
|
|
|
return {'categories': dic, 'ms': [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_requiered |
|
|
|
def home(request): |
|
|
|
def home(request): |
|
|
|
context = list_of_teas() |
|
|
|
context = list_of_teas() |
|
|
|
template = loader.get_template('choose.html') |
|
|
|
template = loader.get_template('choose.html') |
|
|
|
@ -33,5 +56,25 @@ def home(request): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def confirm_choose(request): |
|
|
|
def confirm_choose(request): |
|
|
|
choice = json.loads(request.body.decode(encoding='UTF-8')) |
|
|
|
if request.user.is_authenticated: |
|
|
|
return JsonResponse({'response': 'ok'}) |
|
|
|
choice = json.loads(request.body.decode(encoding='UTF-8')) |
|
|
|
|
|
|
|
d ={} |
|
|
|
|
|
|
|
for key in choice: |
|
|
|
|
|
|
|
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'}) |
|
|
|
|