Ejemplo n.º 1
0
def create_prob(data):
    # expected a dict contain prob info
    order = data['order']
    prob_type = data['type']
    content = data['content']
    max_score = data['max_score']
    answer = data['answer']
    answer_detail = data['answer_detail']
    medias = request.files.getlist('problem'+str(order), None)
    media_uuid_list = Media.save_medias(medias, 'problem')
    new_prob = Problem(order, prob_type, content, media_uuid_list, max_score, answer, answer_detail)
    return new_prob
Ejemplo n.º 2
0
    def post(self, tid):
        user = g.current_user
        task = g.current_task
        if user.is_teacher(task.course):
            return api_abort(403, "you are the teacher")
        time_now = time()
        if not task.time_begin <= time_now <= task.time_end:
            return api_abort(403, "not in the time")

        # delete existed answer
        exist_task_answer = set(task.answers).intersection(set(user.answers))
        if exist_task_answer:
            exist_task_answer = exist_task_answer.pop()
            for answer in exist_task_answer.answers:
                if answer.media is not None:
                    medias = Media.load_medias_from_uuid_list(
                        pickle.loads(answer.media), return_model=True)
                    for media in medias:
                        media.delete()
            db.session.delete(exist_task_answer)

        new_task_answer = TaskAnswer()
        answers = answer_submit_reqparser.parse_args()['answers']
        if not isinstance(answers, list):
            answers = eval(answers)
        problems = dict()
        prob_order_set = set()
        for prob in task.problems:
            problems[prob.order] = prob
            prob_order_set.add(prob.order)

        answer_order_set = set()
        for answer in answers:
            if not isinstance(answer, dict):
                answer = eval(answer)
            content = answer.get('content', None)
            if content == 'undefined':
                content = None
            order = answer.get("order", None)
            if order is None:
                return api_abort(400, "order is needed")
            answer_order_set.add(order)
            medias = request.files.getlist('answer' + str(order))
            media_uuid_list = Media.save_medias(
                medias, 'answer') if len(medias) is not 0 else None
            new_answer = Answer(order, content, media_uuid_list)
            new_answer.student = user
            new_answer.problem = problems[order]
            if new_answer.problem.type is not "subjective":
                new_answer.judge_score()
            new_task_answer.answers.append(new_answer)

        if answer_order_set.difference(prob_order_set):
            return api_abort(400, "answer order over the max order")
        for order in prob_order_set.difference(answer_order_set):
            medias = request.files.getlist('answer' + str(order), None)
            media_uuid_list = Media.save_medias(
                medias, 'answer') if len(medias) is not 0 else None
            new_answer = Answer(order, medias=media_uuid_list)
            new_answer.student = user
            new_answer.problem = problems[order]
            new_task_answer.answers.append(new_answer)

        new_task_answer.student = user
        new_task_answer.task = task
        new_task_answer.judge_score()
        r.sadd("task_finished:" + task.id, user.id)
        db.session.add(new_task_answer)
        db.session.commit()
        data = new_task_answer.to_json(detail=True)
        return make_resp(data)