Example #1
0
    def setUp(self) -> None:
        """
        Add objects to temporary test database:
        - groups 'lecturer' and 'student'
        - 'lecturer' user and 'student' user
        - study subject 'Subject'
        - Subject 'Subject' test 'Hard test'
        - 2 questions for 'Hard test'
        """
        self.lecturer = User.objects.create_user(username='******',
                                                 password='')
        Group.objects.create(id=1, name="lecturer")
        self.lecturer.groups.add(1)

        self.student = User.objects.create_user(username='******', password='')
        Group.objects.create(id=2, name="student")
        self.student.groups.add(2)

        self.subject = Subject.objects.create(
            name='Subject', description='Description of subject')
        self.another_subject = Subject.objects.create(
            name='Another subject',
            description='Description of another subject')
        self.test = Test.objects.create(
            subject=self.subject,
            author=self.lecturer,
            name='Hard test',
            description='Description of hard test for Subject',
            tasks_num=2,
            duration=60)

        self.question = Question.objects.create(
            formulation='First question with multiselect',
            multiselect=True,
            tasks_num=3,
            type=Question.Type.REGULAR,
            test=Test.objects.get(id=self.test.id),
            options=Question.parse_options([{
                'option': 'First true option',
                'is_true': True
            }, {
                'option': 'Second false option',
                'is_true': False
            }, {
                'option': 'Third true option',
                'is_true': True
            }]))
        self.another_question = Question.objects.create(
            formulation='Second question with single answer',
            multiselect=True,
            tasks_num=2,
            type=Question.Type.REGULAR,
            test=Test.objects.get(id=self.test.id),
            options=Question.parse_options([{
                'option': 'False option',
                'is_true': False
            }, {
                'option': 'True option',
                'is_true': True
            }]))
Example #2
0
 def create_from_request(cls, request):
     test = Test.objects.get(id=request.data.get('test_id'))
     questions_id = ObjectId()
     if request.data.get('with_images'):
         questions_type = Question.Type.WITH_IMAGES
         options = []
         for i, file_name in enumerate(request.FILES):
             path = pathlib.Path(
                 f'{test.subject.name}/{test.name}/{questions_id}/{i}.{file_name.split(".")[-1]}'
             )
             default_storage.save(
                 path, ContentFile(request.FILES[file_name].read()))
             options.append({
                 'option': str(path),
                 'is_true': request.data.get(file_name) == 'true'
             })
     else:
         questions_type = Question.Type.REGULAR
         options = request.data.get('options')
     return Question.objects.create(
         _id=questions_id,
         formulation=request.data.get('formulation'),
         multiselect=json.loads(request.data.get('multiselect')),
         tasks_num=request.data.get('tasks_num'),
         type=questions_type,
         options=Question.parse_options(options),
         test=test)
Example #3
0
 def create(self, validated_data):
     test = Test.objects.get(id=validated_data.get('test_id'))
     return Question.objects.create(
         formulation=validated_data.get('formulation'),
         multiselect=validated_data.get('multiselect'),
         tasks_num=validated_data.get('tasks_num'),
         type=validated_data.get('type'),
         options=Question.parse_options(validated_data.get('options')),
         test=test)
Example #4
0
 def update(self, instance, validated_data):
     instance.formulation = validated_data.get('formulation',
                                               instance.formulation)
     options = validated_data.get('options')
     if options:
         for option in options:
             option['is_true'] = ('true' == option['is_true']) or (
                 option['is_true'] == True)
         instance.options = Question.parse_options(options)
     instance.save()
     return instance