You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.3 KiB
Python
188 lines
5.3 KiB
Python
"""
|
|
用户扩展属性 API 路由
|
|
"""
|
|
from apiflask import APIBlueprint
|
|
from flask_jwt_extended import jwt_required, current_user
|
|
from iti.applications.common.utils import success
|
|
from iti.applications.extensions import sys_log, db
|
|
from iti.applications.common.enums import LogType
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from iti.applications.service.sys import sys_user_attribute as attr_service
|
|
from .schemas.user_attribute import (
|
|
UserAttributeQuery,
|
|
UserAttributeSetRequest,
|
|
UserAttributeItemRequest,
|
|
UserAttributeBatchRequest,
|
|
)
|
|
from marshmallow import Schema, fields
|
|
|
|
bp = APIBlueprint(
|
|
"sys_user_attribute",
|
|
__name__,
|
|
url_prefix="/user/attributes",
|
|
tag="系统.用户扩展属性",
|
|
)
|
|
|
|
|
|
@bp.get("/<string:user_id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeQuery, location="query")
|
|
@bp.output(Schema.from_dict({"attributes": fields.Dict()}))
|
|
def get_user_attributes(user_id: str, query_data: dict):
|
|
"""
|
|
获取用户扩展属性
|
|
"""
|
|
group = query_data.get("group")
|
|
attributes = attr_service.get_user_attributes(user_id, group)
|
|
return success({"attributes": attributes})
|
|
|
|
|
|
@bp.put("/<string:user_id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeSetRequest, location="json")
|
|
@sys_log(
|
|
name="设置用户扩展属性",
|
|
desc="批量设置用户扩展属性",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def set_user_attributes(user_id: str, json_data: dict):
|
|
"""
|
|
批量设置用户扩展属性
|
|
"""
|
|
attributes = json_data.get("attributes")
|
|
group = json_data.get("group")
|
|
|
|
attr_service.set_user_attributes(user_id, attributes, group)
|
|
return success(message="设置成功")
|
|
|
|
|
|
@bp.delete("/<string:user_id>/<string:group>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@sys_log(
|
|
name="删除用户扩展属性",
|
|
desc="删除用户扩展属性分组或单个属性",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def delete_user_attributes(user_id: str, group: str):
|
|
"""
|
|
删除用户扩展属性(整个分组)
|
|
"""
|
|
attr_service.delete_user_attribute(user_id, group)
|
|
return success(message="删除成功")
|
|
|
|
|
|
@bp.delete("/<string:user_id>/<string:group>/<string:key>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@sys_log(
|
|
name="删除用户扩展属性",
|
|
desc="删除用户单个扩展属性",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def delete_user_attribute_key(user_id: str, group: str, key: str):
|
|
"""
|
|
删除用户单个扩展属性
|
|
"""
|
|
attr_service.delete_user_attribute(user_id, group, key)
|
|
return success(message="删除成功")
|
|
|
|
|
|
@bp.get("/<string:user_id>/<string:group>/<string:key>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.output(Schema.from_dict({"value": fields.Raw()}))
|
|
def get_user_attribute_value(user_id: str, group: str, key: str):
|
|
"""
|
|
获取单个属性值
|
|
"""
|
|
value = attr_service.get_user_attribute_value(user_id, group, key)
|
|
return success({"value": value})
|
|
|
|
|
|
@bp.put("/<string:user_id>/<string:group>/<string:key>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeItemRequest, location="json")
|
|
@sys_log(
|
|
name="设置用户扩展属性",
|
|
desc="设置用户单个扩展属性",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def set_user_attribute_value(user_id: str, group: str, key: str, json_data: dict):
|
|
"""
|
|
设置单个属性值
|
|
"""
|
|
value = json_data.get("attr_value")
|
|
attr_type = json_data.get("attr_type", "string")
|
|
|
|
attr_service.set_user_attribute_value(user_id, group, key, value, attr_type)
|
|
return success(message="设置成功")
|
|
|
|
|
|
@bp.post("/<string:user_id>/batch")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeBatchRequest, location="json")
|
|
@sys_log(
|
|
name="批量设置用户扩展属性",
|
|
desc="批量设置用户扩展属性(支持类型)",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def batch_set_user_attributes(user_id: str, json_data: dict):
|
|
"""
|
|
批量设置用户扩展属性(支持指定类型、描述、排序)
|
|
"""
|
|
attributes = json_data.get("attributes", [])
|
|
attr_service.batch_set_user_attributes_with_type(user_id, attributes)
|
|
return success(message="批量设置成功")
|
|
|
|
|
|
@bp.get("/current")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeQuery, location="query")
|
|
@bp.output(Schema.from_dict({"attributes": fields.Dict()}))
|
|
def get_current_user_attributes(query_data: dict):
|
|
"""
|
|
获取当前用户的扩展属性
|
|
"""
|
|
group = query_data.get("group")
|
|
attributes = attr_service.get_user_attributes(current_user.id, group)
|
|
return success({"attributes": attributes})
|
|
|
|
|
|
@bp.put("/current")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@bp.input(UserAttributeSetRequest, location="json")
|
|
@sys_log(
|
|
name="设置当前用户扩展属性",
|
|
desc="设置当前用户扩展属性",
|
|
type=LogType.OPERATION,
|
|
save_db=True,
|
|
execute_time=True,
|
|
)
|
|
def set_current_user_attributes(json_data: dict):
|
|
"""
|
|
设置当前用户的扩展属性
|
|
"""
|
|
attributes = json_data.get("attributes")
|
|
group = json_data.get("group")
|
|
|
|
attr_service.set_user_attributes(current_user.id, attributes, group)
|
|
return success(message="设置成功")
|
|
|