SOITA/questions/managers.py
Hubert Jankowski ed31555583 edit tests
2022-01-30 20:04:27 +01:00

85 lines
2.2 KiB
Python

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=[],
name=None,
test=None,
description=None,
**kwargs
):
Answer = apps.get_model("answers", "Answer")
instance = super().create(
name=name,
test=test,
description=name,
)
for answer in answers:
Answer.objects.create(
question=instance,
description=answer["description"],
is_correct=answer["is_correct"]
)
return instance
def addQuestionToExistingTest(
self, *,
answers=[],
name=None,
test=None,
description=None,
**kwargs
):
Answer = apps.get_model("answers", "Answer")
instance = super().create(
name=name,
test=test,
description=name,
)
for answer in answers:
for a in answer["answers"]:
Answer.objects.create(
question=instance,
description=a["description"],
is_correct=a["is_correct"]
)
return instance
def EditQuestion(
self, *,
question_id=None,
answers=[],
name=None,
description=None,
**kwargs
):
Answer = apps.get_model("answers", "Answer")
instance = super().get(
id=question_id
)
instance.name = name
instance.description = name
actual_answers = instance.answers.all()
for answer in actual_answers:
answer.is_correct = False
answer.save()
for i in range(0, len(answers[0]["answers"])):
actual_answer = actual_answers[i]
new_answer = answers[0]["answers"][i]
actual_answer.description = new_answer["description"]
actual_answer.is_correct = new_answer["is_correct"]
actual_answer.save()
instance.save()
return instance