コード例 #1
0
ファイル: views.py プロジェクト: llf-970310/expression-api
def upload_success_v2(question_num):
    # get test
    test_id = ExamSession.get(current_user.id, "test_id",
                              default=DefaultValue.test_id)  # for production
    current_test = CurrentTestModel.objects(id=test_id).first()
    if current_test is None:
        current_app.logger.error("[TestNotFound][upload_success_v2]test_id: %s" % test_id)
        return jsonify(errors.Exam_not_exist)
    try:
        question = current_test.questions[question_num]
    except Exception as e:
        current_app.logger.error("[GetEmbeddedQuestionException][get_upload_url_v2]question_num: "
                                 "%s, user_name: %s. exception:\n%s"
                                 % (question_num, current_user.name, e))
        return jsonify(errors.Get_question_failed)
    q_type = question['q_type']  # EmbeddedDocument不是dict,没有get方法.  --  已手动支持...

    # todo: 任务队列应放更多信息,让评分节点直接取用,避免让评分节点查url
    task_id, err = MyCelery.put_task(q_type, current_test.id, question_num)
    if err:
        current_app.logger.error('[PutTaskException][upload_success]q_type:%s, test_id:%s,'
                                 'exception:\n%s' % (q_type, current_test.id, err))
        return jsonify(errors.exception({'Exception': str(err)}))
    current_app.logger.info("[PutTaskSuccess][upload_success]dataID: %s" % str(current_test.id))

    # 设置状态为handling
    d = {'set__questions__%s__status' % question_num: 'handling'}
    current_test.update(**d)

    # 最后一题上传完成,去除正在测试状态,设置last_test_id
    if int(question_num) >= len(current_test.questions):
        try:
            ExamSession.delete(current_user.id, 'testing')
            ExamSession.rename(current_user.id, 'test_id', 'last_test_id')
            ExamSession.expire(current_user.id, 'last_test_id', 3600)
        except Exception as e:
            current_app.logger.error('[ExamSessionRenameError]%s' % e)
            return jsonify(errors.exception({'Exception': str(e)}))

    resp = {
        "status": "Success",
        "desc": "添加任务成功,等待服务器处理",
        "dataID": str(current_test.id),
        "taskID": task_id
    }
    return jsonify(errors.success(resp))
コード例 #2
0
def wxbt_upload_success():
    """
    请求参数:questionNum, testID (json)
    上传音频完成,告知后端可以进行处理
    """
    q_num = str(request.json.get("questionNum"))
    test_id = str(request.json.get("testID"))
    if q_num is None or test_id is None:
        return jsonify(errors.Params_error)
    current_app.logger.info(
        "wx:upload_success:question_num: %s, current_id: %s" %
        (str(q_num), test_id))

    current_test = BtCurrentTestModel.objects(id=test_id).first()
    if current_test is None:
        current_app.logger.error(
            "upload_success: ERROR: Test Not Exists!! - test_id: %s" % test_id)
        return jsonify(errors.Exam_not_exist)

    q = current_test.questions.get(q_num)
    if q is None:
        return jsonify(errors.error({'msg': '题号无效'}))
    upload_url = q['wav_upload_url']
    current_app.logger.info("upload_success: upload_url: " + upload_url)

    # change question status to handling
    q.status = 'handling'
    q['analysis_start_time'] = datetime.datetime.utcnow()
    current_test.save()

    task_id, err = MyCelery.put_task(q.q_type,
                                     current_test.id,
                                     q_num,
                                     use_lock=False)
    if err:
        current_app.logger.error(
            '[PutTaskException][wxbt_upload_success]q_type:%s, test_id:%s,'
            'exception:\n%s' %
            (q.q_type, current_test.id, traceback.format_exc()))
        return jsonify(errors.exception({'Exception': str(err)}))
    current_app.logger.info("[PutTaskSuccess][wxbt_upload_success]dataID: %s" %
                            str(current_test.id))
    resp = {
        "status": "Success",
        "desc": "添加任务成功,等待服务器处理",
        "dataID": str(current_test.id),
        "taskID": task_id
    }
    return jsonify(errors.success(resp))
コード例 #3
0
ファイル: views.py プロジェクト: dylanchu/sspanel-flask
def update_user():
    user_id = request.json.get('id')  # str
    if user_id is not None:
        user = User.query.filter_by(id=user_id).first()
        if user:
            for k, v in request.json.items():
                if k == 'password':
                    user.password = current_app.md5_hash(v)
                elif hasattr(user, k):
                    setattr(user, k, v)
            try:
                db.session.commit()
            except Exception as e:
                return jsonify(errors.exception({'data': str(e)}))
            return jsonify(errors.success({'msg': '更新成功'}))
        else:
            return jsonify(errors.User_not_exist)
    return jsonify(errors.Bad_request)
コード例 #4
0
ファイル: views.py プロジェクト: dylanchu/sspanel-flask
def delete_user():
    user_id = request.args.get('id')
    if user_id is not None:
        user = User.query.filter_by(id=user_id).first()
        if user:
            try:
                db.session.delete(user)
                db.session.commit()
            except Exception as e:
                return jsonify(errors.exception({'data': str(e)}))
            return jsonify(
                errors.success({
                    'msg': '删除用户完成',
                    'data': {
                        'id': user.id,
                        'email': user.email
                    }
                }))
        else:
            return jsonify(errors.User_not_exist)
    return jsonify(errors.Bad_request)