Merge remote-tracking branch 'origin/сja' into Alex

This commit is contained in:
Alex committed 2025-01-29 15:24:21 +03:00
commit 8b16c64324
2 files changed
+47 -2

No files matched your search

+13 -1
View File
@@ -68,4 +68,16 @@ class TestResultSerializer(serializers.ModelSerializer):
comment = serializers.CharField(allow_null=True)
class Meta:
model = TestResult
fields = '__all__'
fields = '__all__'
class TestsSerializer(serializers.ModelSerializer):
class Meta:
model = Test
fields = '__all__'
def create(self, validated_data):
user = self.context['request'].user
if user.is_authenticated:
validated_data['author'] = user
return super().create(validated_data)
+34 -1
View File
@@ -22,7 +22,40 @@ from django.db.models import F
class TestListCreate(generics.ListCreateAPIView):
queryset = Test.objects.all()
serializer_class = TestSerializer
serializer_class = TestsSerializer
def post(self, request, *args, **kwargs):
# user = request.user
# user = User.objects.get(username="9966-00-125")
test = self.create(request, *args, **kwargs)
test_obj = Test.objects.get(id=test.data['id'])
# test = Test.objects.create(data=request.data, author=user)
questions_files = {file.name: file for file in request.FILES.getlist('questionsfiles')}
answers_files = {file.name: file for file in request.FILES.getlist('answersfiles')}
questions = json.loads(request.data['questions'])
for count, question_data in enumerate(questions, start=1):
question_image = questions_files.get(question_data['file']['name']) if 'file' in question_data and question_data['file'] is not None else None
question = TestQuestion.objects.create(
name=question_data['text'],
test=test_obj,
file=question_image,
numb=count,
settings=question_data.get('settings', {}),
)
for answer in question_data['answers']:
answer_image = answers_files.get(answer['file']['name']) if 'file' in answer and answer['file'] is not None else None
TestAnswer.objects.create(
name=answer['text'],
file=answer_image,
question=question
)
return test_obj
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Test.objects.all()