Merge pull request 'fix/one-user-one-test-opinion' (#40) from fix/one-user-one-test-opinion into master

Reviewed-on: #40
This commit is contained in:
s470631 2022-06-04 14:17:20 +02:00
commit 26ffc43918
5 changed files with 103 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

@ -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)),
],
),
]

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)
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