diff --git a/choose/templates/choose.html b/choose/templates/choose.html index 229473f..060789e 100644 --- a/choose/templates/choose.html +++ b/choose/templates/choose.html @@ -126,7 +126,8 @@ ); fetch(request).then((response) => response.json()) .then((data) => { - console.log(data); + if (data.response == 'ok'){window.location.replace("/");} + else {window.alert(data.response)} }) } diff --git a/choose/templates/vote.html b/choose/templates/vote.html index bea3f7d..e5aaf31 100644 --- a/choose/templates/vote.html +++ b/choose/templates/vote.html @@ -5,7 +5,49 @@ Title - + + + + + + + + + {% for choice in choices %} + + + + + + + {% endfor %} + +
Список чаевВыбралиПроголосовали
+
    + {% for tea in choice.teas %} +
  • {{ tea }}
  • + {% endfor %} +
+
+
    + {% for choice in choice.chosed %} +
  • {{ choice }}
  • + {% endfor %} +
+
+
    + {% for vote in choice.voted %} +
  • {{ vote }}
  • + {% endfor %} +
+
+ {% if choice.class == 'choice' %} + + {% endif %} + {% if choice.class == 'my_choice' %} + + {% endif %} +
+ \ No newline at end of file diff --git a/choose/views.py b/choose/views.py index 08c13d2..81a2c16 100644 --- a/choose/views.py +++ b/choose/views.py @@ -42,17 +42,42 @@ def list_of_teas(): return {'categories': dic, 'ms': [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]} -def list_of_choices(): - pass +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 home(request): +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')) @@ -77,3 +102,19 @@ def confirm_choose(request): 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'}) diff --git a/tea/urls.py b/tea/urls.py index 95a4d86..abb491b 100644 --- a/tea/urls.py +++ b/tea/urls.py @@ -23,8 +23,10 @@ import choose.views urlpatterns = [ path('admin/', admin.site.urls), path('auth/', include('social_django.urls', namespace='social')), - path('', choose.views.home, name='home'), + path('choose', choose.views.choose, name='choose'), + path('vote', choose.views.home, name='home'), path('confirm-choice', choose.views.confirm_choose), + path('confirm-vote', choose.views.confirm_choose), path('login', choose.views.login_view, name='login_view') ] if settings.DEBUG: