def create_resources(datasetID):
    try:
        new_res = []

        # 上传的是id
        if request.json['type'] == 'text':
            for instance in request.json['data']:
                new_res.append(
                    Resource(dataSetID=datasetID,
                             src=instance['src'],
                             environment=instance['environment'],
                             fileMsg=instance['fileMsg'],
                             filename=instance['filename'],
                             ifPositive=instance['ifPositive'],
                             nameExtension=instance['nameExtension'],
                             type=instance['type'],
                             createTime=datetime.datetime.now(tz=pytz.utc),
                             modifyTime=datetime.datetime.now(tz=pytz.utc)))
            sess = create_session()
            sess.add_all(new_res)
            sess.commit()
            return jsonify({'code': 0})

        # 上传的是文件,将由服务器上传给远端
        if request.json['type'] == 'file':
            # print(request.json)
            for instance in request.json['data']:
                src = tools.upload_file(instance['content'])
                new_res.append(
                    Resource(dataSetID=datasetID,
                             src=src,
                             environment=instance['environment'],
                             fileMsg=instance['fileMsg'],
                             filename=instance['filename'],
                             ifPositive=instance['ifPositive'],
                             nameExtension=instance['nameExtension'],
                             type=instance['type'],
                             createTime=datetime.datetime.now(tz=pytz.utc),
                             modifyTime=datetime.datetime.now(tz=pytz.utc)))
            sess = create_session()
            sess.add_all(new_res)
            sess.commit()
            return jsonify({'code': 0})

        return jsonify({'code': 1}, 400)

    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def update_dataset(datasetID):
    try:
        # print(request.json)
        if not request.json or 'data' not in request.json:
            make_response(jsonify({'code': 1}), 400)
        print(request.json['data'])

        sess = create_session()
        ans = sess.query(DataSet).filter(DataSet.id == datasetID).first()

        if ans:
            modify_flag = False
            if request.json['data']['name']:
                ans.name = request.json['data']['name']
                modify_flag = True
            if modify_flag:
                ans.modifyTime = datetime.datetime.now(tz=pytz.utc)
            sess.commit()
            return jsonify({'code': 0})
        else:
            return make_response(jsonify({'code': 1}), 400)

    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
示例#3
0
def create_user():
    try:
        if g.user.role != 'admin':
            return make_response(
                jsonify({
                    'code': 1,
                    'error': 'permission denied'
                }), 401)
        new_user = []
        print(request.json['data'])

        for instance in request.json['data']:
            if len(instance['username']) > 255:
                return make_response(
                    jsonify({
                        'code': 1,
                        'error': 'username too long'
                    }), 400)
            new_user.append(
                User(username=instance['username'],
                     password=User.hash_password(instance['password']),
                     role=instance['role'] if instance['role'] else 'user'))
        sess = create_session()
        sess.add_all(new_user)
        sess.commit()
        return jsonify({'code': 0})
    except Exception as e:
        print(e)
        return make_response(
            jsonify({
                'code': 1,
                'error': 'repeated username'
            }), 400)
示例#4
0
def modify_user_info():
    try:
        print(request.json)

        password = request.json['data']['password']
        new_password = request.json['data']['newPassword']
        if g.user.verify_password(password):
            sess = create_session()
            sess.query(User).filter(User.id == g.user.id).first(
            ).password = User.hash_password(new_password)
            # g.user.password = User.hash_password(new_password)
            sess.commit()
            return jsonify({'code': 0})
        else:
            return make_response(
                jsonify({
                    'code': 1,
                    'error': 'permission denied'
                }), 401)
    except Exception as e:
        print(e)
        return make_response(
            jsonify({
                'code': 1,
                'error': 'repeated username'
            }), 400)
def delete_dataset(datasetID):
    try:
        sess = create_session()
        sess.query(DataSet).filter(DataSet.id == datasetID).delete()
        sess.commit()
        return jsonify({'code': 0})
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def get_dataset(datasetID):
    try:
        sess = create_session()
        ans = sess.query(DataSet).filter(DataSet.id == datasetID).first()
        print(ans)
        if ans:
            return jsonify({'code': 0, 'data': ans.serialize})
        else:
            return make_response(jsonify({'code': 1}), 404)
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def get_resource_info(datasetID, srcID):
    try:
        sess = create_session()
        res = sess.query(Resource).filter(
            and_(Resource.dataSetID == datasetID,
                 Resource.id == srcID)).first()
        print(res)
        if res:
            return jsonify({'code': 0, 'data': res.serialize})
        else:
            return make_response(jsonify({'code': 1}), 404)
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def get_all_datasets():
    try:
        sess = create_session()
        offset = request.args.get('offset', default=0, type=int)
        limit = request.args.get('limit', default=10, type=int)
        ans_all = sess.query(DataSet).order_by(DataSet.id).all()
        ans = ans_all[offset:offset + limit]
        return jsonify({
            'code': 0,
            'maxLen': len(ans_all),
            'data': [x.serialize for x in ans]
        })
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 406)
def delete_resource(datasetID, srcID):
    try:
        sess = create_session()
        res = sess.query(Resource).filter(
            and_(Resource.dataSetID == datasetID,
                 Resource.id == srcID)).first()
        print(res)
        if res:
            sess.delete(res)
            sess.commit()
            return jsonify({'code': 0})
        else:
            return make_response(jsonify({'code': 1}), 406)
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def update_resource(datasetID, srcID):
    try:
        # print(request.json)
        print(request.json['data'])

        sess = create_session()
        res = sess.query(Resource).filter(
            and_(Resource.dataSetID == datasetID,
                 Resource.id == srcID)).first()

        if res:
            modify_flag = False
            if request.json['data']['environment']:
                res.environment = request.json['data']['environment']
                modify_flag = True
            if request.json['data']['fileMsg']:
                res.fileMsg = request.json['data']['fileMsg']
                modify_flag = True
            if request.json['data']['filename']:
                res.filename = request.json['data']['filename']
                modify_flag = True
            if request.json['data']['ifPositive']:
                res.ifPositive = request.json['data']['ifPositive']
                modify_flag = True
            if request.json['data']['nameExtension']:
                res.nameExtension = request.json['data']['nameExtension']
                modify_flag = True
            if request.json['data']['src']:
                res.src = request.json['data']['src']
                modify_flag = True
            if request.json['data']['type']:
                res.type = request.json['data']['type']
                modify_flag = True

            if modify_flag:
                res.modifyTime = datetime.datetime.now(tz=pytz.utc)
            sess.commit()
            return jsonify({'code': 0})
        else:
            return make_response(jsonify({'code': 1}), 400)

    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def get_all_resources(datasetID):
    try:
        sess = create_session()
        data_set = sess.query(DataSet).filter(DataSet.id == datasetID).first()
        print(data_set)
        if data_set:
            offset = request.args.get('offset', default=0, type=int)
            limit = request.args.get('limit', default=10, type=int)
            ans_all = data_set.resources
            print(len(ans_all))
            ans = ans_all[offset:offset + limit]
            return jsonify({
                'code': 0,
                'maxLen': len(ans_all),
                'data': [x.serialize for x in ans]
            })
        else:
            return make_response(jsonify({'code': 1}), 404)
    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
def create_datasets():
    try:
        # print(request.json)
        if not request.json or 'data' not in request.json:
            make_response(jsonify({'code': 1}), 400)
        new_data_sets = []
        print(request.json['data'])

        for instance in request.json['data']:
            new_data_sets.append(
                DataSet(name=instance['name'],
                        type=instance['type'],
                        creator=g.user.username,
                        createTime=datetime.datetime.now(tz=pytz.utc),
                        modifyTime=datetime.datetime.now(tz=pytz.utc)))
        sess = create_session()
        sess.add_all(new_data_sets)
        sess.commit()
        return jsonify({'code': 0})

    except Exception as e:
        print(e)
        return make_response(jsonify({'code': 1}), 400)
示例#13
0
def verify_password(username_or_token, password):
    try:
        # first try to authenticate by token
        print('verify_password\nuser_token = ', username_or_token,
              '\npassword = '******'verify_password\n', data)
        user = None
        if data:
            user = sess.query(User).filter(User.id == data['id']).first()
        if not user:
            # try to authenticate with username/password
            user = sess.query(User).filter(
                User.username == username_or_token).first()
            print('verify_password\n', user)
            if not user or not user.verify_password(password):
                return False
        g.user = user
        return True
    except Exception as e:
        print(e)
        return False