superset 后端增加注册接口

好烦啊-- :<

1.先定义modes:

superset\superset\models\user.py

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.from flask_appbuilder.security.sqla.models import User
from sqlalchemy import String, Column, Boolean
from typing import Union
from superset import dbdef id_or_slug_filter(models_name, id_or_slug):if isinstance(id_or_slug, int):return models_name.id == id_or_slugif id_or_slug.isdigit():return models_name.id == int(id_or_slug)return models_name.slug == id_or_slugclass UserV2(User):__tablename__ = "ab_user"@classmethoddef get(cls, id_or_slug: Union[str, int]):query = db.session.query(UserV2).filter(id_or_slug_filter(UserV2, id_or_slug))return query.one_or_none()@classmethoddef get_user_by_cn_name(cls, cn_name: Union[str]):query = db.session.query(UserV2).filter(UserV2.username == cn_name)return query.one_or_none()@classmethoddef get_model_by_username(cls, username: Union[str]):query = db.session.query(UserV2).filter(UserV2.username == username)return query.one_or_none()def as_dict(self):return {c.name: getattr(self, c.name) for c in self.__table__.columns}def __repr__(self):return self.username

2.新增注册接口接口

superset\superset\views\users\api.py

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT     OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from flask import g, Response, request
from flask_appbuilder.api import expose, safe, BaseApi
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.security.sqla.models import User
from flask_jwt_extended.exceptions import NoAuthorizationErrorfrom superset import appbuilder
from superset.models.user import UserV2
from superset.views.base_api import BaseSupersetApi
from superset.views.users.schemas import UserResponseSchema
from superset.views.utils import bootstrap_user_datauser_response_schema = UserResponseSchema()class CurrentUserRestApi(BaseSupersetApi):"""An API to get information about the current user"""resource_name = "me"openapi_spec_tag = "Current User"openapi_spec_component_schemas = (UserResponseSchema,)@expose("/", methods=("GET",))@safedef get_me(self) -> Response:"""Get the user object corresponding to the agent making the request.---get:summary: Get the user objectdescription: >-Gets the user object corresponding to the agent making the request,or returns a 401 error if the user is unauthenticated.responses:200:description: The current usercontent:application/json:schema:type: objectproperties:result:$ref: '#/components/schemas/UserResponseSchema'401:$ref: '#/components/responses/401'"""try:if g.user is None or g.user.is_anonymous:return self.response_401()except NoAuthorizationError:return self.response_401()return self.response(200, result=user_response_schema.dump(g.user))@expose("/roles/", methods=("GET",))@safedef get_my_roles(self) -> Response:"""Get the user roles corresponding to the agent making the request.---get:summary: Get the user rolesdescription: >-Gets the user roles corresponding to the agent making the request,or returns a 401 error if the user is unauthenticated.responses:200:description: The current usercontent:application/json:schema:type: objectproperties:result:$ref: '#/components/schemas/UserResponseSchema'401:$ref: '#/components/responses/401'"""try:if g.user is None or g.user.is_anonymous:return self.response_401()except NoAuthorizationError:return self.response_401()user = bootstrap_user_data(g.user, include_perms=True)return self.response(200, result=user)class UserRestApi(BaseApi):"""继承Flask-appbuilder原生用户视图类, 扩展用户操作的接口类"""route_base = "/api/v1/users"datamodel = SQLAInterface(UserV2)include_route_methods = {"register"}@expose("/register/", methods=("POST",))@safedef register(self) -> Response:"""Get the user roles corresponding to the agent making the request.---get:summary: Get the user rolesdescription: >-Gets the user roles corresponding to the agent making the request,or returns a 401 error if the user is unauthenticated.responses:200:description: The current usercontent:application/json:schema:type: objectproperties:result:$ref: '#/components/schemas/UserResponseSchema'401:$ref: '#/components/responses/401'"""try:data = request.get_json()username = data.get("username")user_models = UserV2.get_model_by_username(username)if username and not user_models:result = appbuilder.sm.add_user(username,data.get("first_name"),data.get("last_name"),data.get("email"),appbuilder.sm.find_role(data.get("role")),data.get("password"),)if result:return self.response(200, result={"status": "Success","message": "ok",})else:return self.response_401()else:return self.response_401()except NoAuthorizationError:return self.response_401()

3. 增加api导入

superset\superset\initialization_init_.py

		...from superset.views.users.api import UserRestApi...appbuilder.add_api(UserRestApi)

在这里插入图片描述

4. postman 测试:

参数:

{"username": "1213","first_name": "122","last_name":"last_name","email":"email@qq.com","role":"admin","password":"sasadasd121324rd"
}

在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/215902.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[PyTorch][chapter 66][强化学习-值函数近似]

前言 现实强化学习任务面临的状态空间往往是连续的,无穷多个。 这里主要针对这种连续的状态空间处理。后面DQN 也是这种处理思路。 目录&#xff1a; 1&#xff1a; 原理 2&#xff1a; 梯度更新 3&#xff1a; target 和 预测值 4 流程 一 原理 强化学习最重要的是得到 …

二阶线性微分算子

求导算子:&#xff0c;相当于 二阶线性微分算子 符号&#xff1a; 性质&#xff1a;设函数yy(x),,,二阶可导&#xff0c;C、C1,C2为常数&#xff0c;则有 叠加原理 设函数 ,都是L[y]的解&#xff0c; C1,C2是常数&#xff0c;则c1y1c2y2&#xff0c;也是L[y]的解 函数线性…

每日一题(LeetCode)----链表--链表最大孪生和

每日一题(LeetCode)----链表–链表最大孪生和 1.题目&#xff08;2130. 链表最大孪生和&#xff09; 在一个大小为 n 且 n 为 偶数 的链表中&#xff0c;对于 0 < i < (n / 2) - 1 的 i &#xff0c;第 i 个节点&#xff08;下标从 0 开始&#xff09;的孪生节点为第 (n…

相机设置参数:黑电平(Black Level)详解和示例

本文通过原理和示例对相机设置参数“黑电平”进行讲解&#xff0c;以帮助大家理解和使用。 原理 相机中黑电平原理是将电平增大&#xff0c;可以显示更多暗区细节&#xff0c;可能会损失一些亮区&#xff0c;但图像更多的关注暗区&#xff0c;获取完图像信息再减掉。只是为了…

VMware安装windows操作系统

一、下载镜像包 地址&#xff1a;镜像包地址。 找到需要的版本下载镜像包。 二、安装 打开VMware新建虚拟机&#xff0c;选择用镜像文件。将下载的镜像包加载进去即可。

Java枚举详解

一、什么是枚举类型 枚举类型是一种特殊的数据类型&#xff0c;用于定义一组固定的命名常量。枚举类型提供了一种更强大、更安全和更易读的方式来表示一组相关的常量。 在Java中&#xff0c;枚举类型是通过使用enum关键字来定义的。枚举类型可以包含一个或多个枚举常量&#xf…

DELL MD3600F存储重置管理软件密码

注意&#xff1a;密码清除可能会导致业务秒断&#xff0c;建议非业务时间操作 针对一台控制器操作即可&#xff0c;另一控制器会同步操作 重置后密码为空&#xff01; 需求&#xff1a;重置存储管理软件密码 管理软件中分配物理磁盘时提示输入密码(类似是否了解风险确认操作的提…

【网易云商】构建高效 SaaS 系统的技术要点与最佳实践

SaaS 是什么 定义 相信大家都对云服务中的 IaaS、PaaS、SaaS 早就有所耳闻&#xff0c;现在更是衍生出了 aPaaS、iPaaS、DaaS 等等的类似概念。对于 SaaS 也有各种各样的定义&#xff0c;本文给出的定义是&#xff1a; SaaS 是一种基于互联网提供服务和软件的交付模式&#xf…

Wireshark的捕获过滤器

Wireshark的过滤器&#xff0c;顾名思义&#xff0c;作用是对数据包进行过滤处理。具体过滤器包括捕获过滤器和显示过滤器。本文对捕获过滤器进行分析。 捕获过滤器&#xff1a;当进行数据包捕获时&#xff0c;只有那些满足给定的包含/排除表达式的数据包会被捕获。 捕获过滤器…

什么是轻量应用服务器?可以从亚马逊云科技的优势入手了解

什么是轻量应用服务器&#xff1f; 随着如今各行各业对云计算的需求越来越多&#xff0c;云服务器也被越来越多的企业所广泛采用。其中&#xff0c;轻量应用服务器是一种简单、高效、可靠的云计算服务&#xff0c;能够为开发人员、企业和个人提供轻量级的虚拟专用服务器&#x…

MeterSphere | 接口测试请求体中,int类型的入参实现动态化变量

项目场景&#xff1a; 在接口自动化的时候&#xff0c;要把上一个接口的 Int 变量传入到 下一个接口中进行使用&#xff0c;但编译器会出现 红色的 X 符号 问题描述 如何实现 int 类型的入参实现动态化变量&#xff1f; 解决方案&#xff1a; 忽视掉这个红色 X 号&#xff0…

使用 JavaScript 进行 API 测试的综合教程

说明 API 测试是软件测试的一种形式&#xff0c;涉及直接测试 API 并作为集成测试的一部分&#xff0c;以确定它们是否满足功能、可靠性、性能和安全性的预期。 先决条件&#xff1a; JavaScript 基础知识。Node.js 安装在您的计算机上。如果没有&#xff0c;请在此处下载。npm…