SOITA/trials/managers.py

62 lines
1.7 KiB
Python
Raw Normal View History

2021-12-12 11:35:35 +01:00
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
2021-12-12 11:35:35 +01:00
instance = super().create(
name=kwargs.get("name"),
passing_score=kwargs.get("passing_score"),
2022-01-24 22:17:08 +01:00
category=category,
2022-04-09 13:02:21 +02:00
created_by=kwargs.get("user"),
2022-04-10 15:05:07 +02:00
visible=kwargs.get("visible"),
password=kwargs.get("password")
2021-12-12 11:35:35 +01:00
)
for question in questions:
question_instance = Question.objects.create(
name=question["name"],
2022-01-24 22:17:08 +01:00
description=question.get("description", ""),
2021-12-12 23:34:54 +01:00
test=instance
2021-12-12 11:35:35 +01:00
)
for answer in question["answers"]:
Answer.objects.create(
description=answer["description"],
2022-01-24 22:17:08 +01:00
is_correct=answer.get("is_correct", False),
2021-12-12 11:35:35 +01:00
question=question_instance
)
return instance
2022-05-25 00:06:44 +02:00
class TournamentManager(Manager):
def create(
self, *,
name="",
questions=[],
passing_score=0,
**kwargs
):
Question = apps.get_model("questions", "Question")
instance = super().create(
name=name,
passing_score=passing_score
)
for q in questions:
qq = Question.objects.get(id=q)
qq.tournament.add(instance.id)
return instance