fix/one-user-one-test-opinion #40

Merged
s470631 merged 2 commits from fix/one-user-one-test-opinion into master 2022-06-04 14:17:20 +02:00
5 changed files with 103 additions and 8 deletions

View File

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

View File

@ -37,8 +37,12 @@
<option value="5">Genialny</option> <option value="5">Genialny</option>
</select> </select>
<span><button class="defaultButton"> <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> </button></span>
{% if request.GET.message != "" %}
<br>
{{ request.GET.message }}
{% endif %}
<div class="resultContainerSapce"><br></div> <div class="resultContainerSapce"><br></div>
{% if request.session.password == "" %} {% 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, "name": self.name,
"passing_score": self.passing_score "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 rest_framework.views import APIView
from questions.models import Question 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 trials.serializers import TestSerializer
from django.conf import settings from django.conf import settings
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
@ -324,18 +324,42 @@ def editPassword(request, test_id):
test.save() test.save()
return redirect(f'/tests/{test_id}/edit') return redirect(f'/tests/{test_id}/edit')
@login_required @login_required
def rateTest(request, test_id): def rateTest(request, test_id):
user = request.user
test = Test.objects.get(id=test_id) test = Test.objects.get(id=test_id)
rate = request.GET["rate"]
test.rates_amount += 1 if RateTest.objects.filter(user=user, test=test).exists():
test.total_rating += int(rate) return redirect(f'/tests/{test_id}/result?message=Nie mozesz ocenić ponownie tego testu!')
avg_rating = test.total_rating / test.rates_amount else:
test.avg_rating = int(avg_rating) rate = request.GET["rate"]
test.save() 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') 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): class TestModelViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all() queryset = Test.objects.all()
serializer_class = TestSerializer serializer_class = TestSerializer