Fixed one user opinion to one test and tournament

This commit is contained in:
Hubert Jankowski 2022-06-04 14:14:59 +02:00
parent 3e22c9925c
commit 544303c145
4 changed files with 71 additions and 8 deletions

View File

@ -36,9 +36,14 @@
<option value="4">Dobry</option>
<option value="5">Genialny</option>
</select>
<span><button class="defaultButton">
<a href='' onclick="this.href='rateTest?rate='+document.getElementById('rate').value">Oceń</a>
</button></span>
{% if request.GET.message != "" %}
<br>
{{ request.GET.message }}
{% endif %}
<div class="resultContainerSapce"><br></div>
{% if request.session.password == "" %}

View File

@ -37,8 +37,12 @@
<option value="5">Genialny</option>
</select>
<span><button class="defaultButton">
<a href='' onclick="this.href='rateTest?rate='+document.getElementById('rate').value">Oceń</a>
<a href='' onclick="this.href='rateTournament?rate='+document.getElementById('rate').value">Oceń</a>
</button></span>
{% if request.GET.message != "" %}
<br>
{{ request.GET.message }}
{% endif %}
<div class="resultContainerSapce"><br></div>
{% if request.session.password == "" %}

View File

@ -181,3 +181,33 @@ class Tournament(models.Model):
"name": self.name,
"passing_score": self.passing_score
}
class RateTest(models.Model):
user = models.ForeignKey(
"users.User",
null=True,
related_name="rate_tests",
on_delete=models.CASCADE
)
test = models.ForeignKey(
"Test",
null=True,
related_name="rate_tests",
on_delete=models.CASCADE
)
class RateTournament(models.Model):
user = models.ForeignKey(
"users.User",
null=True,
related_name="rate_tournaments",
on_delete=models.CASCADE
)
tournament = models.ForeignKey(
"Tournament",
null=True,
related_name="rate_tournaments",
on_delete=models.CASCADE
)

View File

@ -8,7 +8,7 @@ from rest_framework.response import Response
from rest_framework.views import APIView
from questions.models import Question
from trials.models import Test, SolvedTest, Tournament
from trials.models import Test, SolvedTest, Tournament, RateTournament, RateTest
from trials.serializers import TestSerializer
from django.conf import settings
from django.shortcuts import render, redirect
@ -324,18 +324,42 @@ def editPassword(request, test_id):
test.save()
return redirect(f'/tests/{test_id}/edit')
@login_required
def rateTest(request, test_id):
user = request.user
test = Test.objects.get(id=test_id)
if RateTest.objects.filter(user=user, test=test).exists():
return redirect(f'/tests/{test_id}/result?message=Nie mozesz ocenić ponownie tego testu!')
else:
rate = request.GET["rate"]
test.rates_amount += 1
test.total_rating += int(rate)
avg_rating = test.total_rating / test.rates_amount
test.avg_rating = int(avg_rating)
test.save()
RateTest.objects.create(user=user, test=test)
return redirect(f'/tests/{test_id}/result')
@login_required
def rateTournament(request, tournament_id):
user = request.user
tournament = Test.objects.get(id=tournament_id)
if RateTournament.objects.filter(user=user, test=test).exists():
redirect(f'/tests/{tournament_id}/tournament/result?message=Nie mozesz ocenić ponownie tego testu!')
else:
rate = request.GET["rate"]
tournament.rates_amount += 1
tournament.total_rating += int(rate)
avg_rating = tournament.total_rating / tournament.rates_amount
tournament.avg_rating = int(avg_rating)
tournament.save()
RateTournament.objects.create(user=user, test=test)
return redirect(f'/tests/{tournament_id}/tournament/result')
class TestModelViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all()
serializer_class = TestSerializer