Category model update

This commit is contained in:
Piotr Kopycki 2022-01-08 22:15:30 +01:00
parent 2c444c31d0
commit b837addd84
9 changed files with 81 additions and 20 deletions

View File

@ -1,8 +1,13 @@
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from django.urls import path
from categories.views import CategoryModelViewSet from categories.views import CategoryModelViewSet, category
router = DefaultRouter(trailing_slash=False) router = DefaultRouter(trailing_slash=False)
router.register("items", CategoryModelViewSet) router.register("items", CategoryModelViewSet)
urlpatterns = [
path('<category_name>', category),
]
urlpatterns = router.urls urlpatterns = router.urls

View File

@ -3,7 +3,17 @@ from rest_framework import viewsets
from categories.models import Category from categories.models import Category
from categories.serializers import CategorySerializer from categories.serializers import CategorySerializer
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.template import loader
class CategoryModelViewSet(viewsets.ModelViewSet): class CategoryModelViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all() queryset = Category.objects.all()
serializer_class = CategorySerializer serializer_class = CategorySerializer
@login_required
def category(request, category_name):
context = {}
context['name'] = category_name
context['tests'] = Test.objects.all(category=category_name)
return render(request, 'category.html', context)

View File

@ -25,5 +25,5 @@ urlpatterns = [
path('questions/', include("questions.urls")), path('questions/', include("questions.urls")),
path('answers/', include("answers.urls")), path('answers/', include("answers.urls")),
path('tests/', include("trials.urls")), path('tests/', include("trials.urls")),
path('categories/', include("categories.urls")), path('category/', include("categories.urls")),
] ]

View File

@ -46,16 +46,16 @@
<a href="/test/create">Create test</a> <a href="/test/create">Create test</a>
<a href="/users/tests">Your tests</a> <a href="/users/tests">Your tests</a>
<p>Categories</p> <p>Categories</p>
<a href="/tests/category/1">Język polski</a> <a href="/category/JezykPolski">Język polski</a>
<a href="/tests/category/2">Język angielski</a> <a href="/category/JezykAngielski">Język angielski</a>
<a href="/tests/category/3">Język niemiecki</a> <a href="/category/JezykNiemiecki">Język niemiecki</a>
<a href="/tests/category/4">Matematyka</a> <a href="/category/Matematyka">Matematyka</a>
<a href="/tests/category/5">Informatyka</a> <a href="/category/Informatyka">Informatyka</a>
<a href="/tests/category/6">Fizyka</a> <a href="/category/Fizyka">Fizyka</a>
<a href="/tests/category/7">Chemia</a> <a href="/category/Chemia">Chemia</a>
<a href="/tests/category/8">Biologia</a> <a href="/category/Biologia">Biologia</a>
<a href="/tests/category/9">Geografia</a> <a href="/category/Geografia">Geografia</a>
<a href="/tests/category/10">Historia</a> <a href="/category/Historia">Historia</a>
<p>Account</p> <p>Account</p>
<a href="/users/account">Settings</a> <a href="/users/account">Settings</a>
<a href="/users/logout">Logout</a> <a href="/users/logout">Logout</a>

26
templates/category.html Normal file
View File

@ -0,0 +1,26 @@
{% extends "base.html" %}
{% block title %}Category - {{name}}{% endblock %}
{% block content %}
<h1>Tests in {{name}} category</h1>
{% for test in tests %}
<div class="mainTestContainer">
<div class="mainTestName">
{{test.name}}
</div>
<div class="mainTestMeta">
Category: {{test.category}}
</div>
<div class="mainTestDesc">
<!-- TODO 250 words limit-->
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus volutpat scelerisque tortor, id sodales leo finibus id. Vivamus id viverra nunc, ac faucibus metus. Nulla a mauris imperdiet sapien lobortis dapibus. Quisque ornare posuere pulvinar.
</div>
<div class="mainTestMeta">
Passing score: {{test.passing_score}}
</div>
<button><a href="/tests/{{test.id}}/show">Start</a></button>
</div>
<br>
{% endfor %}
{% endblock %}

View File

@ -10,7 +10,7 @@
{{test.name}} {{test.name}}
</div> </div>
<div class="mainTestMeta"> <div class="mainTestMeta">
Category: {{test.category.name}} Category: {{test.category}}
</div> </div>
<div class="mainTestDesc"> <div class="mainTestDesc">
<!-- TODO 250 words limit--> <!-- TODO 250 words limit-->

View File

@ -0,0 +1,19 @@
# Generated by Django 3.2.9 on 2022-01-08 18:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('trials', '0005_test_category'),
]
operations = [
migrations.AlterField(
model_name='test',
name='category',
field=models.CharField(default='Matematyka', max_length=100),
preserve_default=False,
),
]

View File

@ -5,12 +5,13 @@ from .managers import TestManager
class Test(models.Model): class Test(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
passing_score = models.PositiveSmallIntegerField(default=0) passing_score = models.PositiveSmallIntegerField(default=0)
category = models.ForeignKey( # category = models.ForeignKey(
"categories.Category", # "categories.Category",
on_delete=models.SET_NULL, # on_delete=models.SET_NULL,
null=True, # null=True,
related_name="tests" # related_name="tests"
) # )
category = models.CharField(max_length=100)
objects = TestManager() objects = TestManager()

View File

@ -8,7 +8,7 @@ from categories.serializers import CategorySerializer
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
questions = QuestionSerializer(many=True, required=False) questions = QuestionSerializer(many=True, required=False)
category = CategorySerializer(required=False) #category = CategorySerializer(required=False)
class Meta: class Meta:
model = Test model = Test