Ejemplo n.º 1
0
 async def post(self, request, id):
     test = Test.objects(id=id).first()
     if not test or test.status != Test.STATUS_PUBLISHED:
         raise NotFound('test_not_found')
     question_id = request.json['question_id']
     options = request.json['options']
     question = Question.objects(test_id=id, id=question_id).first()
     if not question:
         raise NotFound('question_not_found')
     answer = Answer.objects(account_id=current_account.id,
                             test_id=test.id).first()
     if not answer:
         Answer(id=generation_objectid(),
                account_id=current_account.id,
                test_id=test.id,
                answers={
                    question_id: options
                },
                updated_time=datetime.utcnow()).save()
     else:
         answers = answer.answers
         answers[question_id] = options
         answer.update(answers=answers, updated_time=datetime.utcnow())
         answer.save()
     if question.number == test.question_count - 1:
         last = True
         score = self.calculate_score(test, answer.answers)
         answer.update(score=score, status=Answer.STATUS_FINISHED)
         # 统计分数
     else:
         last = False
     return {'ok': True, 'last': last}, 201
Ejemplo n.º 2
0
 async def get(self, request):
     if not current_account.id:
         raise NotFound('account_not_found')
     account = Account.get(current_account.id)
     if not account:
         raise NotFound('account_not_found')
     return account, 200
Ejemplo n.º 3
0
 async def get(self, request):
     if not current_account.id:
         raise NotFound('account_not_found')
     account = Account.objects(id=current_account.id).first()
     if not account:
         raise NotFound('account_not_found')
     return account, 200
Ejemplo n.º 4
0
 async def put(self, request, test_id, id):
     test = Test.objects(id=test_id).first()
     if not test or test.creator_id != current_account.id:
         raise NotFound('test_not_found')
     question = Question.objects(id=id, test_id=test_id).first()
     if not question:
         raise NotFound('question_not_found')
     question.udpate(**request.json)
     question.save()
     return question, 200
Ejemplo n.º 5
0
 async def post(self, request, test_id, id):
     test = Test.get(_id=test_id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     question = Question.get(_id=id)
     if not question or question.get('test_id') != test_id:
         raise NotFound('question_not_found')
     result = Question.find_one_and_update(filter={'_id': ObjectId(id)},
                                           update={'$set': request.json})
     return result, 200, None
Ejemplo n.º 6
0
 async def post(self, request, id):
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('account_not_found')
     request.json['test_id'] = id
     question = Question.insert(**request.json)
     return question, 201, None
Ejemplo n.º 7
0
    async def get(self, request, id):
        print(request.headers)
        test = Test.get(_id=id)
        if not test or test.get('status') != 'published':
            raise NotFound('test_not_found')

        return test, 200
Ejemplo n.º 8
0
 async def get(self, request, id):
     print(request.headers)
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('account_not_found')
     questions = Question.find(filter={'test_id': id}, skip=0, limit=20)
     return questions, 200
Ejemplo n.º 9
0
 async def delete(self, request, id):
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     if test.get('status') == 'published':
         raise BadRequest('invalid_status')
     Test.delete_one(_id=id)
     return {}, 204
Ejemplo n.º 10
0
 async def put(self, request, id):
     print(request.json)
     print(request.headers)
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     result = Test.find_one_and_update(filter={'_id': ObjectId(id)},
                                       update={'$set': request.json})
     return result, 200
Ejemplo n.º 11
0
 async def get(self, request, id):
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     _start_time = test.get('start_time')
     if _start_time:
         test['date_start'], test['time_start'] = split_datetime(_start_time)
     _end_time = test.get('end_time')
     if _end_time:
         test['date_end'], test['time_end'] = split_datetime(_end_time)
     return test, 200
Ejemplo n.º 12
0
 async def put(self, request, id):
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     start_time = request.json.get('start_time')
     if start_time:
         request.json['start_time'] = str_to_time(start_time)
     end_time = request.json.get('end_time')
     if end_time:
         request.json['end_time'] = str_to_time(end_time)
     result = Test.find_one_and_update(filter={'_id': ObjectId(id)},
                                       update={'$set': request.json})
     return result, 200
Ejemplo n.º 13
0
 async def get(self, request, id):
     test = Test.objects(id=id).first()
     if not test or test.status != Test.STATUS_PUBLISHED:
         raise NotFound('test_not_found')
     return test, 200
Ejemplo n.º 14
0
 def _get_test(self, id):
     test = Test.objects(id=id).first()
     if not test or test.status == 'draft':
         raise NotFound('account_not_found')
     return test
Ejemplo n.º 15
0
 async def get(self, request, id):
     answer = Answer.objects(test_id=id).first()
     if not answer:
         raise NotFound('answer_not_found')
     return answer, 200
Ejemplo n.º 16
0
 def _get_test(self, id):
     test = Test.objects(id=id).first()
     if not test:
         raise NotFound('account_not_found')
     return test
Ejemplo n.º 17
0
 async def get(self, request, id):
     test = Test.get(_id=id)
     if not test or test.get('creator_id') != current_account.id:
         raise NotFound('test_not_found')
     return test, 200
Ejemplo n.º 18
0
 def _get_test(self, id):
     test = Test.objects(id=id).first()
     if not test or test.creator_id != current_account.id:
         raise NotFound('test_not_found')
     return test