예제 #1
0
def finish_exam_pre_process():
    id = request.json.get("id")
    exam = Exam.query.get(id)
    review_memo = request.json.get('review_memo', '')
    user_id = g.user.id
    if not exam:
        raise JsonOutputException('未找到该试卷')
    if exam.state != EXAM_STATUS['预处理']:
        raise JsonOutputException('试卷状态错误')

    questions = Question.query.\
        filter_by(exam_id=exam.id).\
        filter(Question.state != QUEST_STATUS['已删除']).\
        all()

    if not len(questions):
        raise JsonOutputException('未录入题目,无法完成')

    preprocess_data = Preprocess.query.filter_by(exam_id=exam.id).\
        filter_by(operator_id=g.user.id).\
        filter_by(state=EXAM_STATUS['预处理']).\
        first()
    if not preprocess_data:
        raise JsonOutputException('预处理记录不存在')

    preprocess_data.state = EXAM_STATUS['预处理完成']
    preprocess_data.save()

    ExamLog.log(exam.id, g.user.id, EXAM_STATUS['预处理完成'], 'DEAL')

    exam.state = EXAM_STATUS['预处理完成']
    exam.save()
    exam = Exam.query.get(id)
    return render_api(exam.to_dict())
예제 #2
0
def view_exam_file_pre_process(id):
    exam = Exam.query.get(int(id))
    if not exam:
        raise JsonOutputException('试卷不存在')
    if not exam.state in (EXAM_STATUS['已采纳'], EXAM_STATUS['预处理']):
        raise JsonOutputException('试卷状态错误')
    process_data = Preprocess.query.\
        filter_by(exam_id=id, state=EXAM_STATUS['预处理']).\
        order_by(Preprocess.created_at.desc()).\
        first()
    if process_data and not process_data.operator_id == g.user.id:
        raise JsonOutputException('任务已被领取')
    if not process_data:
        process_data = Preprocess(exam_id=id,
                                  operator_id=g.user.id,
                                  state=EXAM_STATUS['预处理'],
                                  memo='')
        process_data.save()
        ExamLog.log(exam.id, g.user.id, EXAM_STATUS['预处理'], 'DEAL')
    exam.state = EXAM_STATUS['预处理']
    exam.save()
    data = exam.get_dtl()
    questList = Question.query.\
        filter_by(exam_id=exam.id).\
        filter(Question.state != QUEST_STATUS['已删除']).\
        all()
    data['quest_list'] = [item.to_dict() for item in questList]
    return render_api(data)
예제 #3
0
def user_info_update():
    password = request.json.get('password')
    repassword = request.json.get('rePassword')
    valid_code = request.json.get('validCode')
    phone = request.json.get('phone')
    email = request.json.get('email')
    province_id = request.json.get('province_id', 0)
    city_id = request.json.get('city_id', 0)
    area_id = request.json.get('area_id', 0)
    school_id = request.json.get('school_id', 0)
    grade_id = request.json.get('grade_id', 0)

    user = User.query.filter_by(phone=phone).first()
    if user is not None and user.id != g.user.id:
        raise JsonOutputException('该手机号已被使用')
    if not valid_code:
        raise JsonOutputException('请输入验证码')
    sms = SmsServer()
    # 验证码验证
    if not sms.check_code(valid_code, phone):
        raise JsonOutputException('验证码错误')
    if password:
        if password != repassword:
            raise JsonOutputException('两次输入密码不一致')
        g.user.password = password
    g.user.phone = phone
    g.user.email = email
    g.user.province_id = province_id
    g.user.city_id = city_id
    g.user.area_id = area_id
    g.user.school_id = school_id
    g.user.grade_id = grade_id
    g.user.save()
    return render_api({})
예제 #4
0
def update_question():
    id = request.json.get('id')
    quest_type_id = request.json.get('quest_type_id')
    quest_image = request.json.get('quest_image')
    answer_image = request.json.get('answer_image')
    quest_no = request.json.get('quest_no')
    option_count = request.json.get('option_count')

    quest = Question.query.get(id)
    if quest is None:
        raise JsonOutputException('未找到题目')
    #非未处数据不能进行修改
    if quest.state != QUEST_STATUS['未处理']:
        raise JsonOutputException('该题不能修改')
    elif quest.insert_user_id != g.user.id:  #非本人操作无法修改
        raise JsonOutputException("您无权修改该题目")

    if quest_type_id is not None:
        quest.quest_type_id = quest_type_id
    if quest_image is not None:
        quest.quest_image = quest_image
    if answer_image is not None:
        quest.answer_image = answer_image
    if quest_no is not None:
        quest.quest_no = quest_no
    if option_count is not None:
        quest.option_count = option_count
    if quest.quest_type_id == '3':
        quest.has_sub = 1
    quest.save()
    quest = Question.query.get(id)
    return render_api(quest.to_dict())
예제 #5
0
def review_exam_update(id):
    state = request.json.get('state')
    memo = request.json.get('memo')
    if not state or state not in [-1, 2, 5]:
        raise JsonOutputException('状态错误')

    #查询是否已审核
    exam = Exam.query.get(id)
    if not exam:
        raise JsonOutputException('试卷不存在')
    review_data = Review.query.filter_by(exam_id=exam.id).\
        filter_by(review_state=EXAM_STATUS['正在审核']).\
        filter_by(reviewer_id=g.user.id).\
        first()
    if not review_data:
        raise JsonOutputException('审核记录不存在')
    countdown = 1800 - (datetime.datetime.now() -
                        review_data.review_date).seconds
    if countdown <= 0:
        review_data.review_state = EXAM_STATUS['审核超时']
        exam.state = EXAM_STATUS['未审核']
        review_data.save()
        exam.save()
        raise JsonOutputException('本次审核已过期,请重新审核')
    review_data.review_state = state
    review_data.review_memo = memo
    review_data.save()
    exam.state = state
    exam.save()
    ExamLog.log(exam.id, g.user.id, state, 'CONFIRM')
    return render_api({})
예제 #6
0
def get_check_task(id):
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS[
            '完成解答'] and question.state != QUEST_STATUS['正在检查']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_check_data = QuestCheck.query.\
        filter_by(state=QUEST_STATUS['正在检查']).\
        filter_by(quest_id=id).\
        order_by(QuestCheck.created_at.desc()).\
        first()
    if not quest_check_data:
        quest_check_data = QuestCheck(
            quest_id=id,
            exam_id=question.exam_id,
            quest_no=question.quest_no,
            state=QUEST_STATUS['正在检查'],
            operator_id=g.user.id,
        )
        quest_check_data.save()
    if quest_check_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')
    question.state = QUEST_STATUS['正在检查']
    question.save()
    res = question.get_answer_dtl()
    return render_api(res)
예제 #7
0
def get_user_by_openid(code):
    # 获取openid
    url = "https://api.weixin.qq.com/sns/jscode2session"
    querystring = {
        "appid": current_app.config['WECHAT_APPID'],
        "secret": current_app.config['WECHAT_SECRET'],
        "js_code": code,
        "grant_type": "authorization_code"
    }
    response = requests.request("GET", url, params=querystring)
    if not response.ok:
        raise JsonOutputException('openid获取失败')
    res = response.json()
    if res.get('errcode'):
        raise JsonOutputException(res['errmsg'])
    openid = res['openid']

    user = User.query.filter_by(openid=openid).first()
    session['openid'] = openid
    if not user:
        user = User(openid=openid)
        user.save()
        return render_api({'status': 'unregister'})
    if not user.phone:
        return render_api({'status': 'unregister'})
    login_user(user)
    data = user.to_dict()
    return render_api({'status': 'success', 'user': data})
예제 #8
0
def check_right(id):
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS['正在检查']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_check_data = QuestCheck.query.\
        filter_by(state=QUEST_STATUS['正在检查']).\
        filter_by(quest_id=id).\
        order_by(QuestCheck.created_at.desc()).\
        first()
    if quest_check_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')
    state = QUEST_STATUS['待校对']
    # 大小题
    if question.has_sub:
        for item in question.sub_items1:
            item_quest_type = QType.query.filter_by(
                id=item.get('quest_type_id', 0)).first()
            if not item_quest_type:
                raise JsonOutputException('子题题型不存在')
            sub_quest = SubQuestion(
                parent_id=question.id,
                quest_content=item.get('quest_content', ''),
                quest_content_html=item.get('quest_content_html', ''),
                correct_answer=item.get('correct_answer', ''),
                quest_no=item.get('sort', 0),
                qtype_id=item.get('quest_type_id', 0),
                operator_id=item.get('operator_id', 0),
                finish_state=item.get('finish_state', ''))
            if item_quest_type.is_selector():
                options = item.get('options', [])
                option_count = len(options)
                # 插入选项
                sub_quest.qoptjson = json.dumps(options)
                sub_quest.option_count = option_count
            db.session.add(sub_quest)
    else:
        # 选择题
        quest_type = QType.query.filter_by(id=question.quest_type_id).first()
        if not quest_type:
            raise JsonOutputException('题型不存在')
        if quest_type.is_selector():
            question.correct_answer = question.correct_answer1
            for option in question.options1:
                option = QOption(qid=question.id,
                                 qok=option.get('_selected', False),
                                 qsn=option.get('sort', ''),
                                 qopt=option.get('content', ''))
                db.session.add(option)
        else:
            question.correct_answer = question.correct_answer1
    quest_check_data.state = state
    question.state = state
    db.session.add(quest_check_data)
    db.session.add(question)
    db.session.commit()
    return render_api({})
예제 #9
0
def upload_paper_attachment(id):
    paper = Exam.query.get_or_404(id)
    if paper.is_fast != 1:
        raise JsonOutputException('操作失败')
    success = paper.push_attachments()
    if success:
        paper.is_fast = 2
        paper.save()
        return render_api({})
    raise JsonOutputException('上传失败')
예제 #10
0
def login():
    if g.user is not None and g.user.is_authenticated():
        return g.user.to_dict()
    user_name = request.json.get('user_name')
    password = request.json.get('password')
    user = User.query.filter_by(name=user_name).first()
    if user is None:
        user = User.query.filter_by(phone=user_name).first()
    if user is None:
        raise JsonOutputException('用户不存在')
    if user.verify_password(password):
        login_user(user)
        return user.to_dict()
    raise JsonOutputException('密码错误')
예제 #11
0
def del_quest(id):
    quest = Question.query.get(id)
    if quest is None:
        raise JsonOutputException('未找到该试题')
    if quest.state != QUEST_STATUS['未处理']:
        raise JsonOutputException('该题目已进入其它处理流程,不能删除')
    if quest.insert_user_id != g.user.id:
        raise JsonOutputException('您无权删除该题目')
    exam = Exam.query.get(quest.exam_id)
    if exam is None:
        raise JsonOutputException('未找到该试卷')

    quest.state = QUEST_STATUS['已删除']
    quest.save()
    return render_api('')
예제 #12
0
def cancel_fast(id):
    exam = Exam.query.get(id)
    if not exam:
        raise JsonOutputException('没有数据')
    exam.is_fast = 0
    exam.save()
    return {'code': 0, 'data': {}}
예제 #13
0
def upload_attachment():
    file = request.files.get('file')
    thumb = bool(request.args.get('thumb', False))
    if not file:
        raise JsonOutputException('请选择要上传的文件')
    file_type = request.form.get('type', '')
    data = upload(file, thumb)
    if data.get('status') is True:
        attachment = Attachment(name=data.get('original', ''),
                                url=data.get('url', ''),
                                user_id=g.user.id,
                                file_type=file_type)
        attachment.save()
        #attachment.id
        return {'code': 0, 'data': [attachment.url]}
    raise JsonOutputException('上传失败')
예제 #14
0
def city():
    pro_id = request.args.get('pro_id')
    title = request.args.get('title', '')
    if not pro_id:
        raise JsonOutputException('need pro_id')
    cities = Region.get_city(pro_id, title)
    return {'code': 0, 'data': cities}
예제 #15
0
def school():
    ctid = request.args.get('ctid')
    title = request.args.get('title', '')
    if not ctid:
        raise JsonOutputException('need ctid')
    schools = School.get_schools_by_ctid(ctid, title)
    return {'code': 0, 'data': schools}
예제 #16
0
def area():
    city_id = request.args.get('city_id')
    title = request.args.get('title', '')
    if not city_id:
        raise JsonOutputException('need city_id')
    areas = Region.get_area(city_id, title)
    return {'code': 0, 'data': areas}
예제 #17
0
def cropper_image():
    file_url = request.json.get('file_url')
    box = request.json.get('box')
    if not file_url:
        raise JsonOutputException('请传入图片')
    if not box or len(box) != 5:
        raise JsonOutputException('切割参数错误')
    app_path = current_app.config['APP_PATH']
    file_path = '{}{}'.format(app_path, file_url)
    if not os.path.isfile(file_path):
        raise JsonOutputException('图片不存在')
    origin_image = Image.open(file_path)
    degree = -box[-1]
    box = box[0:4]
    dest_image = origin_image.rotate(degree, expand=1).crop(box)
    return image_save(dest_image)
예제 #18
0
def review_exam(id):
    exam = Exam.query.get(id)
    # 超时时间
    timeout = 1800
    if not exam:
        raise JsonOutputException('试卷不存在')
    if not exam.state in (EXAM_STATUS['正在审核'], EXAM_STATUS['未审核']):
        raise JsonOutputException('试卷状态错误')
    if exam.school_id != g.user.school_id and g.user.school_id != 3906:
        raise JsonOutputException('没有权限审核该试卷')
    review_data = Review.query.\
        filter_by(exam_id=exam.id).\
        filter_by(review_state=EXAM_STATUS['正在审核']).\
        filter_by(reviewer_id=g.user.id).\
        order_by(Review.created_at.desc()).\
        first()
    #新增
    if not review_data:
        review = Review(exam_id=exam.id,
                        reviewer_id=g.user.id,
                        review_state=EXAM_STATUS['正在审核'],
                        review_memo='')
        review.save()
        exam.state = EXAM_STATUS['正在审核']
        exam.save()
        res = exam.get_dtl()
        res['countdown'] = timeout - (datetime.datetime.now() -
                                      review.review_date).seconds
        # log
        ExamLog.log(exam.id, g.user.id, EXAM_STATUS['正在审核'], 'CONFIRM')
        return render_api(res)
    if not review_data.reviewer_id == g.user.id:
        raise JsonOutputException('该试卷已被人审核')
    countdown = timeout - (datetime.datetime.now() -
                           review_data.review_date).seconds
    if countdown <= 0:
        review_data.review_state = EXAM_STATUS['审核超时']
        exam.state = EXAM_STATUS['未审核']
        review_data.save()
        exam.save()
        raise JsonOutputException('本次审核已过期,请重新审核')
    res = exam.get_dtl()
    res['countdown'] = timeout - (datetime.datetime.now() -
                                  review_data.review_date).seconds
    return render_api(res)
예제 #19
0
def api_register():
    phone = request.json.get('phone')
    valid_code = str(request.json.get('valid_code', ''))
    visit_code = str(request.json.get('visit_code', ''))
    if not phone or not valid_code or not visit_code:
        raise JsonOutputException('参数错误')
    if len(phone) != 11:
        raise JsonOutputException('手机号格式错误')
    user = User.query.filter_by(phone=phone).first()
    if user is not None:
        raise JsonOutputException('该手机号已经注册过')
    sms = SmsServer()
    if not sms.check_code(valid_code, phone):
        raise JsonOutputException('验证码错误')
    if not (len(visit_code) == 4 and sum([int(i) for i in visit_code]) == 16):
        raise JsonOutputException('邀请码错误')
    session['phone'] = phone
    return render_api({})
예제 #20
0
def delexam(id):

    exam = Exam.query.get(int(id))
    if exam is None or exam.state > EXAM_STATUS['未审核']:
        raise JsonOutputException('删除失败')
    else:
        exam.state = EXAM_STATUS['已删除']
        exam.save()
        return {'code': 0, 'data': ''}
예제 #21
0
def api_register_info():
    if not session.get('phone'):
        raise JsonOutputException('请从注册页进入')
    data = MultiDict(mapping=request.json)
    form = RegisterInfoForm(data)
    if form.validate():
        user = User.query.filter_by(phone=form.phone.data).first()
        if user is not None:
            raise JsonOutputException('该手机号已经注册过')
        user = User.query.filter_by(name=form.user_name.data).first()
        if user is not None:
            raise JsonOutputException('该用户名已被使用')
        user = None
        if session.get('openid'):
            user = User.query.filter_by(openid=session.get('openid')).first()
        if user:
            user.name = form.user_name.data
            user.phone = form.phone.data
            email = form.email.data,
            user.password = form.password.data
            user.school_id = form.school_id.data
            user.city_id = form.city_id.data
            user.grade_id = form.grade_id.data
            user.province_id = form.province_id.data
            user.area_id = form.area_id.data
        else:
            user = User(name=form.user_name.data,
                        phone=form.phone.data,
                        email=form.email.data,
                        password=form.password.data,
                        school_id=form.school_id.data,
                        city_id=form.city_id.data,
                        grade_id=form.grade_id.data,
                        province_id=form.province_id.data,
                        area_id=form.area_id.data)
        user.save()
        login_user(user)
        return render_api({})
    raise FormValidateError(form.errors)
예제 #22
0
def add_struct(id):
    exam = Exam.query.get(int(id))
    if not exam:
        raise JsonOutputException('试卷不存在')
    if not exam.state == EXAM_STATUS['预处理']:
        raise JsonOutputException('试卷状态错误')
    process_data = Preprocess.query.\
        filter_by(exam_id=id, state=EXAM_STATUS['预处理']).\
        order_by(Preprocess.created_at.desc()).\
        first()
    if not process_data:
        raise JsonOutputException('无法处理')
    if not process_data.operator_id == g.user.id:
        raise JsonOutputException('该试卷已经被他人领取')
    struct = request.json.get('struct', [])
    attachments = request.json.get('attachments', [])
    is_word = request.json.get('is_word', False)
    if not struct:
        raise JsonOutputException('请输入试卷结构')
    exam.struct = struct
    # word文档直接生成题目
    if is_word:
        for tip in struct:
            quest_image = attachments
            for i in range(int(tip['start_no']), int(tip['end_no']) + 1):
                quest = Question(exam_id=id,
                                 quest_no=i,
                                 quest_image=quest_image,
                                 state=QUEST_STATUS['未处理'],
                                 insert_user_id=g.user.id)
                db.session.add(quest)
        db.session.commit()
        process_data.state = EXAM_STATUS['预处理完成']
        process_data.save()
        exam.state = EXAM_STATUS['预处理完成']
    exam.has_struct = True
    exam.save()
    return render_api({})
예제 #23
0
def verify_right(id):
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS['正在校对']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_verify_data = QuestVerify.query.\
        filter_by(state=QUEST_STATUS['正在校对']).\
        filter_by(quest_id=id).\
        order_by(QuestVerify.created_at.desc()).\
        first()
    if quest_verify_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')
    state = QUEST_STATUS['结束录题']

    quest_verify_data.state = state
    question.state = state
    db.session.add(quest_verify_data)
    db.session.add(question)
    db.session.commit()
    # 检查试卷是否录题完成
    exam = Exam.query.get(question.exam_id)
    exam.check_question_complete()
    return render_api({})
예제 #24
0
def q_search():
    import http.client, urllib.parse
    import json
    httpClient = None
    pageIndex = request.args.get("pageIndex", "1")
    q = request.args.get("q", "")
    subject_id = request.args.get('subject_id', '')
    qtype_id = request.args.get('qtype_id', '')
    pageIndex = request.args.get('pageIndex', 0)

    size = 15
    _from = int(pageIndex) * size

    connection = http.client.HTTPConnection('search.i3ke.com', 80, timeout=10)
    headers = {'Content-type': 'application/json'}
    param = {
        "subject_id": subject_id,
        "qtype_id": qtype_id,
        "mlt": {
            "fields": "qtxt",
            "like": "%" + q
        },
        "allFields": ["qtxt"],
        "highlightedFields": ["qtxt"],
        "from": _from,
        "size": size,
        "sort": {
            "_score": "desc"
        }
    }
    params = json.dumps(param)

    connection.request('POST', '/sq-apps/api/_search', params, headers)

    response = connection.getresponse()
    jsonStr = response.read().decode()
    jsonResult = json.loads(jsonStr)
    if jsonResult['code'] != 0:
        raise JsonOutputException('参数错误')

    res = {
        'items': jsonResult['datas'],
        'pageIndex': pageIndex,
        'pageSize': jsonResult['size'],
        'totalCount': jsonResult['total'],
        'totalPage': math.ceil(jsonResult['total'] / jsonResult['size'])
    }
    return render_api(res)
예제 #25
0
def exam_process():
    id = request.json.get('exam_id')
    if not id:
        raise JsonOutputException('请输入试卷id')
    exam = Exam.query.get(id)
    if not exam:
        raise JsonOutputException('试卷不存在')
    if not exam.state == EXAM_STATUS['预处理']:
        raise JsonOutputException('试卷状态错误')
    process_data = Preprocess.query.\
        filter_by(exam_id=id, state=EXAM_STATUS['预处理']).\
        order_by(Preprocess.created_at.desc()).\
        first()
    if not process_data:
        raise JsonOutputException('无法处理')
    if not process_data.operator_id == g.user.id:
        raise JsonOutputException('该试卷已经被他人领取')
    struct = request.json.get('struct', [])
    if not struct:
        raise JsonOutputException('请录入试卷结构')
    exam.struct = struct
    exam.has_struct = True
    tips = request.json.get('tips', [])
    if not tips:
        raise JsonOutputException('请输入试卷切割信息')
    for tip in tips:
        quest_image = tip.get('formData', {}).get('quest_image', [])
        answer_image = tip.get('formData', {}).get('answer_image', [])
        for i in range(int(tip['formData']['start_no']),
                       int(tip['formData']['end_no']) + 1):
            quest = Question(exam_id=id,
                             quest_no=i,
                             quest_image=quest_image,
                             answer_image=answer_image,
                             state=QUEST_STATUS['未处理'],
                             insert_user_id=g.user.id,
                             order=exam.order)
            db.session.add(quest)
    db.session.commit()
    process_data.state = EXAM_STATUS['预处理完成']
    process_data.save()
    exam.state = EXAM_STATUS['预处理完成']
    exam.save()
    return render_api({})
예제 #26
0
def paper_upload():
    data = MultiDict(mapping=request.json)
    form = PaperUploadForm(data)
    if not form.validate():
        raise FormValidateError(form.errors)
    # todo 插入数据库
    attachments = request.json.get('attachments', [])
    exam = Exam(name=form.name.data, section=form.section.data, subject=form.subject.data, paper_types=form.paper_types.data, \
                province_id=form.province_id.data, city_id=form.city_id.data, area_id=form.area_id.data,\
                school_id=form.school_id.data,
                exam_date=form.exam_date.data,
                year=form.year.data,
                grade=form.grade.data,
                is_fast=form.is_fast.data,
                state=0,
                attachments=attachments,
                upload_user=g.user.id,
                order=PAPER_TYPE_ORDER[form.paper_types.data])
    result = exam.save()
    ExamLog.log(exam.id, g.user.id, EXAM_STATUS['未审核'], 'UPLOAD')
    if result.id is not None:
        return render_api({})
    raise JsonOutputException('添加失败')
예제 #27
0
def input_quest(id):
    selected_id = request.json.get('selected_id', 0)
    quest_content_html = request.json.get('quest_content_html')
    quest_content = request.json.get('quest_content')
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS['正在录题']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_typing_data = QuestTyping.query.\
        filter_by(state=QUEST_STATUS['正在录题']).\
        filter_by(quest_id=id).\
        order_by(QuestTyping.created_at.desc()).\
        first()
    if quest_typing_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')
    if selected_id:
        question.refer_quest_id = selected_id
        quest_typing_data.state = QUEST_STATUS['结束录题']
        question.state = QUEST_STATUS['结束录题']
        db.session.add(quest_typing_data)
        db.session.add(question)
        db.session.commit()

        # 检查试卷是否录题完毕
        exam = Exam.query.get(question.exam_id)
        exam.check_question_complete()
        return render_api({})
    if not quest_content_html:
        raise JsonOutputException('请输入题目')
    # 题目类型
    quest_type_id = request.json.get('quest_type_id')
    jieda = request.json.get('jieda', '')
    fenxi = request.json.get('fenxi', '')
    dianpin = request.json.get('dianpin', '')
    kaodian = request.json.get('kaodian', '')
    has_sub = request.json.get('has_sub', 0)
    quest_type = QType.query.filter_by(id=quest_type_id).first()
    if not quest_type:
        raise JsonOutputException('题型不存在')
    question.quest_content = quest_content
    question.quest_content_html = quest_content_html
    question.quest_type_id = quest_type_id
    question.jieda = jieda
    question.fenxi = fenxi
    question.dianpin = dianpin
    question.kaodian = kaodian
    question.has_sub = has_sub
    state = QUEST_STATUS['完成解答']
    # 大小题
    if has_sub:
        sub_items = request.json.get('sub_items1', [])
        for item in sub_items:
            item_quest_type_id = item.get('quest_type_id', 0)
            item_quest_type = QType.query.filter_by(id=item_quest_type_id).first()
            if not item_quest_type:
                raise JsonOutputException('子题题型不存在')
            correct_answer = item.get('correct_answer', '')
            if correct_answer == '':
                state = QUEST_STATUS['完成录题']
                break
            if item_quest_type.is_selector():
                options = item.get('options', [])
                option_count = len(options)
                if option_count == 0:
                    raise JsonOutputException('请输入选择题选项')
        question.sub_items1 = []
        for item in sub_items:
            item['operator_id'] = g.user.id
            item['finish_state'] = 'input'
            question.sub_items1.append(item)
    else:
        # 选择题
        if quest_type.is_selector():
            options1 = request.json.get('options1', [])
            option_count = len(options1)
            correct_answer1 = request.json.get('correct_answer1', '')
            show_type = request.json.get('show_type', 'C')
            if show_type == 'C':
                qcols = 1
                qrows = option_count / qcols
            if show_type == 'B':
                qrows = 2
                qcols = math.ceil(option_count / qrows)
            if show_type == 'A':
                qrows = 1
                qcols = option_count / qrows
            question.option_count = option_count
            question.qrows = qrows
            question.qcols = qcols
            question.correct_answer1 = correct_answer1
            if not question.correct_answer1:
                state = QUEST_STATUS['完成录题']
            # 插入选项
            question.options1 = options1
        # 解答/填空
        else:
            correct_answer1 = request.json.get('quest_answer', '')
            question.correct_answer1 = correct_answer1
            if question.correct_answer1 == '':
                state = QUEST_STATUS['完成录题']
    quest_typing_data.state = state
    question.state = state
    db.session.add(quest_typing_data)
    db.session.add(question)
    db.session.commit()
    return render_api({})
예제 #28
0
def get_exam(id):
    data = Exam.get_exam(id)
    if data is not None:
        return {'code': 0, 'data': data}
    else:
        raise JsonOutputException('没有数据')
예제 #29
0
def judge_accepy(id):
    types = request.json.get('type')
    if not types in [1, 2]:
        raise JsonOutputException('请求参数错误')
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS['正在裁定']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_judge_data = QuestJudge.query.\
        filter_by(state=QUEST_STATUS['正在裁定']).\
        filter_by(quest_id=id).\
        order_by(QuestJudge.created_at.desc()).\
        first()
    if quest_judge_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')
    state = QUEST_STATUS['待校对']
    correct_answer_key = 'correct_answer{}'.format(types)
    option_key = 'options{}'.format(types)
    sub_item_key = 'sub_items{}'.format(types)
    # 大小题
    if question.has_sub:
        for item in getattr(question, sub_item_key):
            item_quest_type_id = item.get('quest_type_id', 0)
            item_quest_type = QType.query.filter_by(
                id=item_quest_type_id).first()
            if not item_quest_type:
                raise JsonOutputException('子题题型不存在')
            sub_quest = SubQuestion(
                parent_id=question.id,
                quest_content=item.get('quest_content', ''),
                quest_content_html=item.get('quest_content_html', ''),
                correct_answer=item.get('correct_answer', ''),
                quest_no=item.get('sort', 0),
                qtype_id=item_quest_type_id,
                operator_id=item.get('operator_id', 0),
                finish_state=item.get('finish_state', ''))
            if item_quest_type.is_selector():
                options = item.get('options', [])
                option_count = len(options)
                # 插入选项
                sub_quest.qoptjson = json.dumps(options)
                sub_quest.option_count = option_count
            db.session.add(sub_quest)
    else:
        # 选择题
        quest_type = QType.query.filter_by(id=question.quest_type_id).first()
        if not quest_type:
            raise JsonOutputException('题型不存在')
        if quest_type.is_selector():
            question.correct_answer = getattr(question, correct_answer_key)
            for option in getattr(question, option_key):
                option = QOption(qid=question.id,
                                 qok=option.get('_selected', False),
                                 qsn=option.get('sort', ''),
                                 qopt=option.get('content', ''))
                db.session.add(option)
        else:
            question.correct_answer = getattr(question, correct_answer_key)
    quest_judge_data.state = state
    question.state = state
    db.session.add(quest_judge_data)
    db.session.add(question)
    db.session.commit()
    return render_api({})
예제 #30
0
def check_quest(id):
    question = Question.query.get(id)
    if not question:
        raise JsonOutputException('题目不存在')
    if question.state != QUEST_STATUS['正在检查']:
        raise JsonOutputException('暂时无法处理该题目')
    quest_check_data = QuestCheck.query.\
        filter_by(state=QUEST_STATUS['正在检查']).\
        filter_by(quest_id=id).\
        order_by(QuestCheck.created_at.desc()).\
        first()
    if quest_check_data.operator_id != g.user.id:
        raise JsonOutputException('该题目已被他人领取')

    # 题目类型
    jieda = request.json.get('jieda', '')
    fenxi = request.json.get('fenxi', '')
    dianpin = request.json.get('dianpin', '')
    kaodian = request.json.get('kaodian', '')
    question.jieda = jieda
    question.fenxi = fenxi
    question.dianpin = dianpin
    question.kaodian = kaodian
    state = QUEST_STATUS['待裁定']
    quest_type_id = request.json.get('quest_type_id')
    quest_type = QType.query.filter_by(id=quest_type_id).first()
    if not quest_type:
        raise JsonOutputException('题型不存在')
    # 大小题
    if question.has_sub:
        sub_items = request.json.get('sub_items2', [])
        if len(sub_items) == 0:
            raise JsonOutputException('请输入子题信息')
        for item in sub_items:
            item_quest_type_id = item.get('quest_type_id', 0)
            item_quest_type = QType.query.filter_by(
                id=item_quest_type_id).first()
            if not item_quest_type:
                raise JsonOutputException('子题题型不存在')
            correct_answer = item.get('correct_answer', '')
            if correct_answer == '':
                raise JsonOutputException('子题({})请输入正确答案'.format(
                    item.get('sort', '')))
            if item_quest_type.is_selector():
                options = item.get('options', [])
                option_count = len(options)
                if option_count == 0:
                    raise JsonOutputException('子题({})请输入正确答案'.format(
                        item.get('sort', '')))
        question.sub_items2 = []
        for item in sub_items:
            item['operator_id'] = g.user.id
            item['finish_state'] = 'answer_check'
            question.sub_items2.append(item)
    else:
        # 选择
        if quest_type.is_selector():
            correct_answer2 = request.json.get('correct_answer2', '')
            if not correct_answer2:
                raise JsonOutputException('请输入正确答案')
            options2 = request.json.get('options2', [])
            question.correct_answer2 = correct_answer2
            question.options2 = options2
        # 解答/填空
        else:
            correct_answer2 = request.json.get('correct_answer2', '')
            question.correct_answer2 = correct_answer2
            if not question.correct_answer2:
                raise JsonOutputException('请输入正确答案')
    quest_check_data.state = state
    question.state = state
    db.session.add(quest_check_data)
    db.session.add(question)
    db.session.commit()
    return render_api({})