diff --git a/answers/serializers.py b/answers/serializers.py index 131ac22..0f3e66e 100644 --- a/answers/serializers.py +++ b/answers/serializers.py @@ -4,11 +4,13 @@ from answers.models import Answer class AnswerSerializer(serializers.ModelSerializer): + + class Meta: model = Answer fields = ( "id", "description", "is_correct", - "question" + # "question" ) diff --git a/categories/__init__.py b/categories/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/categories/admin.py b/categories/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/categories/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/categories/apps.py b/categories/apps.py new file mode 100644 index 0000000..fc45e88 --- /dev/null +++ b/categories/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CategoriesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'categories' diff --git a/categories/migrations/__init__.py b/categories/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/categories/models.py b/categories/models.py new file mode 100644 index 0000000..db91d85 --- /dev/null +++ b/categories/models.py @@ -0,0 +1,9 @@ +from django.db import models + +# Create your models here. + + +class Category(models.Model): + name = models.CharField(max_length=200, default="") + + diff --git a/categories/serializers.py b/categories/serializers.py new file mode 100644 index 0000000..938f873 --- /dev/null +++ b/categories/serializers.py @@ -0,0 +1,13 @@ +from rest_framework import serializers + +from categories.models import Category + + +class CategorySerializer(serializers.ModelSerializer): + + class Meta: + model = Category + fields = ( + "id", + "name", + ) diff --git a/categories/urls.py b/categories/urls.py new file mode 100644 index 0000000..a61f94c --- /dev/null +++ b/categories/urls.py @@ -0,0 +1,8 @@ +from rest_framework.routers import DefaultRouter + +from categories.views import CategoryModelViewSet + +router = DefaultRouter() +router.register("items", CategoryModelViewSet) + +urlpatterns = router.urls diff --git a/categories/views.py b/categories/views.py new file mode 100644 index 0000000..007b918 --- /dev/null +++ b/categories/views.py @@ -0,0 +1,9 @@ +from rest_framework import viewsets + +from categories.models import Category +from categories.serializers import CategorySerializer + + +class CategoryModelViewSet(viewsets.ModelViewSet): + queryset = Category.objects.all() + serializer_class = CategorySerializer diff --git a/config/settings.py b/config/settings.py index a30645b..5a4e9c3 100644 --- a/config/settings.py +++ b/config/settings.py @@ -1,5 +1,6 @@ import json import os +from datetime import timedelta """ Django settings for config project. @@ -56,7 +57,8 @@ INSTALLED_APPS = [ "users", "trials", "answers", - "questions" + "questions", + "categories" ] MIDDLEWARE = [ @@ -153,6 +155,12 @@ if not DEVELOPMENT: }, }) +SIMPLE_JWT = { + "AUTH_HEADER_TYPES": ("Bearer", ), + 'ACCESS_TOKEN_LIFETIME': timedelta(days=365), + 'REFRESH_TOKEN_LIFETIME': timedelta(days=1), +} + AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', diff --git a/config/urls.py b/config/urls.py index 212ac1d..efba82f 100644 --- a/config/urls.py +++ b/config/urls.py @@ -27,4 +27,5 @@ urlpatterns = [ path('questions/', include("questions.urls")), path('answers/', include("answers.urls")), path('tests/', include("trials.urls")), + path('categories/', include("categories.urls")), ] diff --git a/questions/managers.py b/questions/managers.py new file mode 100644 index 0000000..4384f71 --- /dev/null +++ b/questions/managers.py @@ -0,0 +1,26 @@ +from django.db.models import Manager +from django.apps import apps +from django.conf import settings +from categories.models import Category + + +class QuestionManager(Manager): + def create( + self, *, + answers=[], + test=None, + **kwargs + ): + Answer = apps.get_model("answers", "Answer") + instance = super().create( + test=test, + description=kwargs.get("description"), + ) + for answer in answers: + Answer.objects.create( + question=instance, + description=answer["description"], + is_correct=answer["is_correct"] + ) + return instance + diff --git a/questions/models.py b/questions/models.py index c839628..ed7e783 100644 --- a/questions/models.py +++ b/questions/models.py @@ -1,4 +1,5 @@ from django.db import models +from .managers import QuestionManager class Question(models.Model): @@ -17,3 +18,5 @@ class Question(models.Model): answer.get_secret_answer() for answer in self.answers.all() ] + + objects = QuestionManager() \ No newline at end of file diff --git a/questions/serializers.py b/questions/serializers.py index fa42a9a..9dba1ad 100644 --- a/questions/serializers.py +++ b/questions/serializers.py @@ -1,14 +1,17 @@ from rest_framework import serializers from questions.models import Question +from answers.serializers import AnswerSerializer class QuestionSerializer(serializers.ModelSerializer): - answers = serializers.SerializerMethodField() + # answers = serializers.SerializerMethodField() + # + # def get_answers(self, instance: Question): + # return instance.get_answers_secret() - def get_answers(self, instance: Question): - return instance.get_answers_secret() + answers = AnswerSerializer(many=True, required=False) class Meta: model = Question @@ -19,3 +22,6 @@ class QuestionSerializer(serializers.ModelSerializer): "answers", "test" ) + + def create(self, validated_data): + import pdb;pdb.set_trace() diff --git a/questions/urls.py b/questions/urls.py index 7e61cf7..6f3a3e7 100644 --- a/questions/urls.py +++ b/questions/urls.py @@ -2,7 +2,7 @@ from rest_framework.routers import DefaultRouter from questions.views import QuestionModelViewSet -router = DefaultRouter() +router = DefaultRouter(trailing_slash=False) router.register("items", QuestionModelViewSet) urlpatterns = router.urls diff --git a/test.json b/test.json new file mode 100644 index 0000000..bda22ee --- /dev/null +++ b/test.json @@ -0,0 +1,23 @@ +{ + "name": "asd", + "passing_score": 1, + "questions": [ + { + "name": "heja", + "description": "czesc", + "answers": [ + { + "description": "hejka_answer", + "is_correct": false + }, + { + "description": "hejka2_answer", + "is_correct": true + } + ] + } + ], + "category": { + "name": "Polski" + } +} \ No newline at end of file diff --git a/trials/managers.py b/trials/managers.py new file mode 100644 index 0000000..357b0fd --- /dev/null +++ b/trials/managers.py @@ -0,0 +1,38 @@ +from django.db.models import Manager +from django.apps import apps +from django.conf import settings +from categories.models import Category + + +class TestManager(Manager): + + def create( + self, *, + category=None, + questions=[], + **kwargs + ): + Question = apps.get_model("questions", "Question") + Answer = apps.get_model("answers", "Answer") + category = Category.objects.get_or_create( + name=category["name"] + ) + + instance = super().create( + name=kwargs.get("name"), + passing_score=kwargs.get("passing_score"), + category=category[0] + ) + for question in questions: + question_instance = Question.objects.create( + name=question["name"], + descripton=question["description"] + ) + for answer in question["answers"]: + Answer.objects.create( + description=answer["description"], + is_correct=answer["is_correct"], + question=question_instance + ) + + return instance diff --git a/trials/migrations/0004_remove_test_category.py b/trials/migrations/0004_remove_test_category.py new file mode 100644 index 0000000..e663dc1 --- /dev/null +++ b/trials/migrations/0004_remove_test_category.py @@ -0,0 +1,17 @@ +# Generated by Django 3.2.9 on 2021-12-11 22:17 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('trials', '0003_test_category'), + ] + + operations = [ + migrations.RemoveField( + model_name='test', + name='category', + ), + ] diff --git a/trials/models.py b/trials/models.py index 3292fb6..eea91b9 100644 --- a/trials/models.py +++ b/trials/models.py @@ -1,10 +1,18 @@ from django.db import models +from .managers import TestManager class Test(models.Model): name = models.CharField(max_length=100) - category = models.CharField(max_length=100, default="") passing_score = models.PositiveSmallIntegerField(default=0) + category = models.ForeignKey( + "categories.Category", + on_delete=models.SET_NULL, + null=True, + related_name="tests" + ) + + objects = TestManager() def get_score(self, answers): """ diff --git a/trials/serializers.py b/trials/serializers.py index 52fce74..b5496eb 100644 --- a/trials/serializers.py +++ b/trials/serializers.py @@ -2,11 +2,13 @@ from rest_framework import serializers from questions.serializers import QuestionSerializer from trials.models import Test +from categories.serializers import CategorySerializer class TestSerializer(serializers.ModelSerializer): questions = QuestionSerializer(many=True, required=False) + category = CategorySerializer(required=False) class Meta: model = Test @@ -14,5 +16,13 @@ class TestSerializer(serializers.ModelSerializer): "id", "name", "passing_score", - "questions" + "questions", + "category", ) + + def create(self, validated_data): + instance = Test.objects.create( + **validated_data + ) + return instance + diff --git a/trials/urls.py b/trials/urls.py index 3ebf009..b2139f5 100644 --- a/trials/urls.py +++ b/trials/urls.py @@ -5,12 +5,12 @@ from trials.views import TestModelViewSet from trials.views import TestTemplateView from trials.views import TestValidateAPIView -router = DefaultRouter() +router = DefaultRouter(trailing_slash=False) router.register("items", TestModelViewSet) urlpatterns = [ - path('/show/', TestTemplateView.as_view()), - path('/mark/', TestValidateAPIView.as_view()) + path('/show', TestTemplateView.as_view()), + path('/mark', TestValidateAPIView.as_view()) ] urlpatterns += router.urls