Example #1
0
def register():
    if request.json and request.method == 'POST':
        data = request.get_json()
        email = check_key('email', data)
        if data['username'] is None or data['password'] is None:
            abort(400)
        if User.query.filter_by(username=data['username']).first() is not None:
            abort(400)
        v1 = client.CoreV1Api()
        body = client.V1Namespace()
        body.kind = 'Namespace'
        body.metadata = dict(name=data['username'], lables={
            'name': data['username']
        })
        pretty = 'true'
        try:
            api_response = v1.create_namespace(body, pretty=pretty)
            print(api_response)
        except ApiException as e:
            print("Exception when calling CoreV1Api->create_namespace: %s\n" % e)
            return jsonify({'code': 500, 'msg': '用户已存在'})
        user_data = User(username=data['username'])
        user_data.email = email
        user_data.hash_password(data['password'])
        db.session.add(user_data)
        db.session.commit()
        return jsonify({'code': 200, 'msg': '帐号注册成功'})
    else:
        return jsonify({'code': 500, 'msg': '请求方式错误'})