diff --git a/templates/result.html b/templates/result.html
index 80b072c..1abf9c3 100644
--- a/templates/result.html
+++ b/templates/result.html
@@ -36,9 +36,14 @@
+
+ {% if request.GET.message != "" %}
+
+ {{ request.GET.message }}
+ {% endif %}
{% if request.session.password == "" %}
diff --git a/templates/tournament_result.html b/templates/tournament_result.html
index 80b072c..d4f6d9f 100644
--- a/templates/tournament_result.html
+++ b/templates/tournament_result.html
@@ -37,8 +37,12 @@
+ {% if request.GET.message != "" %}
+
+ {{ request.GET.message }}
+ {% endif %}
{% if request.session.password == "" %}
diff --git a/trials/migrations/0028_ratetest_ratetournament.py b/trials/migrations/0028_ratetest_ratetournament.py
new file mode 100644
index 0000000..fbd8c5f
--- /dev/null
+++ b/trials/migrations/0028_ratetest_ratetournament.py
@@ -0,0 +1,32 @@
+# Generated by Django 3.2.9 on 2022-06-04 09:39
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ('trials', '0027_auto_20220524_2354'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='RateTournament',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('tournament', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='rate_tournaments', to='trials.tournament')),
+ ('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='rate_tournaments', to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ migrations.CreateModel(
+ name='RateTest',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('test', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='rate_tests', to='trials.test')),
+ ('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='rate_tests', to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ ]
diff --git a/trials/models.py b/trials/models.py
index 5349b02..e5b213e 100644
--- a/trials/models.py
+++ b/trials/models.py
@@ -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
+ )
\ No newline at end of file
diff --git a/trials/views.py b/trials/views.py
index 6362535..8719421 100644
--- a/trials/views.py
+++ b/trials/views.py
@@ -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)
- 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()
+
+ 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