UnisKB/apps/users/views/user.py

73 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# coding=utf-8
"""
@project: MaxKB
@Author虎虎
@file user.py
@date2025/4/14 19:25
@desc:
"""
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from rest_framework.request import Request
from rest_framework.views import APIView
from common.auth.authenticate import TokenAuth
from common.auth.authentication import has_permissions
from common.constants.permission_constants import PermissionConstants, Permission, Group, Operate
from common.result import result
from users.api.user import UserProfileAPI, TestWorkspacePermissionUserApi
from users.serializers.user import UserProfileSerializer, UserManageSerializer
class UserProfileView(APIView):
authentication_classes = [TokenAuth]
@extend_schema(methods=['GET'],
description=_("Get current user information"),
operation_id=_("Get current user information"),
tags=[_("User management")],
responses=UserProfileAPI.get_response())
def get(self, request: Request):
return result.success(UserProfileSerializer().profile(request.user))
class TestPermissionsUserView(APIView):
authentication_classes = [TokenAuth]
@extend_schema(methods=['GET'],
description=_("Get current user information"),
operation_id="测试",
tags=[_("User management")],
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_EDIT)
def get(self, request: Request):
return result.success(UserProfileSerializer().profile(request.user))
class TestWorkspacePermissionUserView(APIView):
authentication_classes = [TokenAuth]
@extend_schema(methods=['GET'],
description="针对工作空间下权限校验",
operation_id="针对工作空间下权限校验",
tags=[_("User management")],
responses=UserProfileAPI.get_response(),
parameters=TestWorkspacePermissionUserApi.get_parameters())
@has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission())
def get(self, request: Request, workspace_id):
return result.success(UserProfileSerializer().profile(request.user))
class UserManage(APIView):
authentication_classes = [TokenAuth]
@extend_schema(methods=['POST'],
description=_("Create user"),
operation_id=_("Create user"),
tags=[_("User management")],
request=UserProfileAPI.get_request(),
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_CREATE)
def post(self, request: Request):
return result.success(UserManageSerializer().save(request.data))