Compare commits

..

2 Commits

Author SHA1 Message Date
paov 46ee7ad545 14 4 years ago
paov 6dd97cf7b5 13 4 years ago
  1. 3
      choose/templates/choose.html
  2. 96
      choose/templates/vote.html
  3. 47
      choose/views.py
  4. 3
      sync.py
  5. 4
      tea/urls.py

@ -126,7 +126,8 @@
); );
fetch(request).then((response) => response.json()) fetch(request).then((response) => response.json())
.then((data) => { .then((data) => {
console.log(data); if (data.response == 'ok'){window.location.replace("/");}
else {window.alert(data.response)}
}) })
} }
</script> </script>

@ -5,7 +5,49 @@
<title>Title</title> <title>Title</title>
</head> </head>
<body> <body>
<table>
<tbody>
<tr>
<th>Список чаев</th>
<th>Выбрали</th>
<th>Проголосовали</th>
<th></th>
</tr>
{% for choice in choices %}
<tr class="{{ choice.class }}">
<td>
<ul>
{% for tea in choice.teas %}
<li>{{ tea }}</li>
{% endfor %}
</ul>
</td>
<td>
<ul>
{% for choice in choice.chosed %}
<li>{{ choice }}</li>
{% endfor %}
</ul>
</td>
<td>
<ul>
{% for vote in choice.voted %}
<li>{{ vote }}</li>
{% endfor %}
</ul>
</td>
<td>
{% if choice.class == 'choice' %}
<button onclick="vote({{ choice.id }})"{% if request.user.username in choice.voted %}disabled{% endif %}>+</button>
{% endif %}
{% if choice.class == 'my_choice' %}
<button onclick="location.href='/choose'">Изменить выбор</button>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body> </body>
<style> <style>
table { table {
@ -51,14 +93,54 @@
border-radius: 0 8px 8px 0; border-radius: 0 8px 8px 0;
} }
table tr.category_tr th { table tbody tr.my_choice {
border-radius: 8px 8px 8px 8px; background: #fffdcd;
background: #ececec;
} }
table h3 { ul {
margin-top: 0px; display: block;
margin-bottom: 0px; list-style-type: disc;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0px;
margin-inline-end: 0px;
padding-inline-start: 10px;
} }
</style> </style>
<script>
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
function vote(id) {
const request = new Request(
'confirm-vote',
{
method: 'POST',
headers: {'X-CSRFToken': csrftoken},
mode: 'same-origin',
body: JSON.stringify({vote:id})
}
);
fetch(request).then((response) => response.json())
.then((data) => {
if (data.response == 'ok' || data.response == 'not found'){window.location.reload();}
else {window.alert(data.response)}
})
}
</script>
</html> </html>

@ -42,17 +42,42 @@ def list_of_teas():
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]}
def list_of_choices(): def list_of_choices(request):
pass 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 @login_requiered
def home(request): def choose(request):
context = list_of_teas() context = list_of_teas()
template = loader.get_template('choose.html') template = loader.get_template('choose.html')
return HttpResponse(template.render(context, request)) 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): def confirm_choose(request):
if request.user.is_authenticated: if request.user.is_authenticated:
choice = json.loads(request.body.decode(encoding='UTF-8')) choice = json.loads(request.body.decode(encoding='UTF-8'))
@ -77,3 +102,19 @@ def confirm_choose(request):
return JsonResponse({'response': 'ok'}) return JsonResponse({'response': 'ok'})
else: else:
return JsonResponse({'response': 'unauthenticated'}) 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'})

@ -8,7 +8,10 @@ from bs4 import BeautifulSoup
from pathlib import Path from pathlib import Path
from choose.utils import get_extension from choose.utils import get_extension
import environ
env = environ.Env(DEBUG=(bool, False))
environ.Env.read_env('tea/.env')
BASE_DIR = Path(__file__).resolve().parent BASE_DIR = Path(__file__).resolve().parent
settings.configure( settings.configure(
DATABASES={ DATABASES={

@ -23,8 +23,10 @@ import choose.views
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('auth/', include('social_django.urls', namespace='social')), 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-choice', choose.views.confirm_choose),
path('confirm-vote', choose.views.confirm_choose),
path('login', choose.views.login_view, name='login_view') path('login', choose.views.login_view, name='login_view')
] ]
if settings.DEBUG: if settings.DEBUG:

Loading…
Cancel
Save