2023-09-15 09:40:35 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: qabot
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: user.py
|
|
|
|
|
|
@date:2023/9/4 10:57
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
from django.core import cache
|
|
|
|
|
|
from drf_yasg import openapi
|
|
|
|
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
|
from rest_framework.decorators import permission_classes
|
|
|
|
|
|
from rest_framework.permissions import AllowAny
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
from rest_framework.views import Request
|
|
|
|
|
|
|
|
|
|
|
|
from common.auth.authenticate import TokenAuth
|
|
|
|
|
|
from common.auth.authentication import has_permissions
|
2024-03-18 07:34:02 +00:00
|
|
|
|
from common.constants.permission_constants import PermissionConstants, CompareConstants, ViewPermission, RoleConstants
|
2025-03-19 07:45:10 +00:00
|
|
|
|
from common.log.log import log
|
2023-09-15 09:40:35 +00:00
|
|
|
|
from common.response import result
|
2025-03-19 07:45:10 +00:00
|
|
|
|
from common.util.common import encryption
|
2023-09-15 09:40:35 +00:00
|
|
|
|
from smartdoc.settings import JWT_AUTH
|
2023-10-24 12:24:32 +00:00
|
|
|
|
from users.serializers.user_serializers import RegisterSerializer, LoginSerializer, CheckCodeSerializer, \
|
2023-09-15 09:40:35 +00:00
|
|
|
|
RePasswordSerializer, \
|
2025-01-20 06:41:26 +00:00
|
|
|
|
SendEmailSerializer, UserProfile, UserSerializer, UserManageSerializer, UserInstanceSerializer, SystemSerializer, \
|
|
|
|
|
|
SwitchLanguageSerializer
|
2025-01-13 03:15:51 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2025-01-20 06:41:26 +00:00
|
|
|
|
|
2023-09-15 09:40:35 +00:00
|
|
|
|
user_cache = cache.caches['user_cache']
|
|
|
|
|
|
token_cache = cache.caches['token_cache']
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-21 08:25:09 +00:00
|
|
|
|
class Profile(APIView):
|
|
|
|
|
|
@action(methods=['GET'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get MaxKB related information"),
|
|
|
|
|
|
operation_id=_("Get MaxKB related information"),
|
2024-03-21 08:25:09 +00:00
|
|
|
|
responses=result.get_api_response(SystemSerializer.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_('System parameters')])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='System parameters', operate='Get MaxKB related information')
|
2024-03-21 08:25:09 +00:00
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(SystemSerializer.get_profile())
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-15 09:40:35 +00:00
|
|
|
|
class User(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get current user information"),
|
|
|
|
|
|
operation_id=_("Get current user information"),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=result.get_api_response(UserProfile.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[])
|
2023-11-20 10:53:18 +00:00
|
|
|
|
@has_permissions(PermissionConstants.USER_READ)
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Get current user information')
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def get(self, request: Request):
|
2023-10-09 11:03:41 +00:00
|
|
|
|
return result.success(UserProfile.get_user_profile(request.user))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
|
2023-11-17 09:43:35 +00:00
|
|
|
|
class Query(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get user list"),
|
|
|
|
|
|
operation_id=_("Get user list"),
|
2023-11-17 09:43:35 +00:00
|
|
|
|
manual_parameters=UserSerializer.Query.get_request_params_api(),
|
|
|
|
|
|
responses=result.get_api_array_response(UserSerializer.Query.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2023-11-20 10:53:18 +00:00
|
|
|
|
@has_permissions(PermissionConstants.USER_READ)
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Get user list')
|
2023-11-17 09:43:35 +00:00
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
UserSerializer.Query(data={'email_or_username': request.query_params.get('email_or_username')}).list())
|
|
|
|
|
|
|
2023-09-15 09:40:35 +00:00
|
|
|
|
|
2025-01-20 06:41:26 +00:00
|
|
|
|
class SwitchUserLanguageView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@swagger_auto_schema(operation_summary=_("Switch Language"),
|
|
|
|
|
|
operation_id=_("Switch Language"),
|
|
|
|
|
|
request_body=openapi.Schema(
|
|
|
|
|
|
type=openapi.TYPE_OBJECT,
|
|
|
|
|
|
required=['language'],
|
|
|
|
|
|
properties={
|
|
|
|
|
|
'language': openapi.Schema(type=openapi.TYPE_STRING, title=_("language"),
|
|
|
|
|
|
description=_("language")),
|
|
|
|
|
|
}
|
|
|
|
|
|
),
|
|
|
|
|
|
responses=RePasswordSerializer().get_response_body_api(),
|
|
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Switch Language')
|
2025-01-20 06:41:26 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
data = {**request.data, 'user_id': request.user.id}
|
|
|
|
|
|
return result.success(SwitchLanguageSerializer(data=data).switch())
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-15 09:40:35 +00:00
|
|
|
|
class ResetCurrentUserPasswordView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Modify current user password"),
|
|
|
|
|
|
operation_id=_("Modify current user password"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=openapi.Schema(
|
|
|
|
|
|
type=openapi.TYPE_OBJECT,
|
|
|
|
|
|
required=['email', 'code', "password", 're_password'],
|
|
|
|
|
|
properties={
|
2025-01-20 06:41:26 +00:00
|
|
|
|
'code': openapi.Schema(type=openapi.TYPE_STRING, title=_("Verification code"),
|
|
|
|
|
|
description=_("Verification code")),
|
|
|
|
|
|
'password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"),
|
|
|
|
|
|
description=_("Password")),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
're_password': openapi.Schema(type=openapi.TYPE_STRING, title=_("Password"),
|
|
|
|
|
|
description=_("Password"))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=RePasswordSerializer().get_response_body_api(),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Modify current user password')
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
data = {'email': request.user.email}
|
|
|
|
|
|
data.update(request.data)
|
|
|
|
|
|
serializer_obj = RePasswordSerializer(data=data)
|
|
|
|
|
|
if serializer_obj.reset_password():
|
2024-04-16 14:55:34 +00:00
|
|
|
|
token_cache.delete(request.META.get('HTTP_AUTHORIZATION'))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
return result.success(True)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
return result.error(_("Failed to change password"))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SendEmailToCurrentUserView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@permission_classes((AllowAny,))
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Send email to current user"),
|
|
|
|
|
|
operation_id=_("Send email to current user"),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=SendEmailSerializer().get_response_body_api(),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Send email to current user')
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
serializer_obj = SendEmailSerializer(data={'email': request.user.email, 'type': "reset_password"})
|
|
|
|
|
|
if serializer_obj.is_valid(raise_exception=True):
|
|
|
|
|
|
return result.success(serializer_obj.send())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Logout(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@permission_classes((AllowAny,))
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Sign out"),
|
|
|
|
|
|
operation_id=_("Sign out"),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=SendEmailSerializer().get_response_body_api(),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Sign out')
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
2024-04-16 14:55:34 +00:00
|
|
|
|
token_cache.delete(request.META.get('HTTP_AUTHORIZATION'))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
return result.success(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-19 07:45:10 +00:00
|
|
|
|
def _get_details(request):
|
|
|
|
|
|
path = request.path
|
|
|
|
|
|
body = request.data
|
|
|
|
|
|
query = request.query_params
|
|
|
|
|
|
return {
|
|
|
|
|
|
'path': path,
|
|
|
|
|
|
'body': {**body, 'password': encryption(body.get('password', ''))},
|
|
|
|
|
|
'query': query
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-15 09:40:35 +00:00
|
|
|
|
class Login(APIView):
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Log in"),
|
|
|
|
|
|
operation_id=_("Log in"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=LoginSerializer().get_request_body_api(),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=LoginSerializer().get_response_body_api(),
|
|
|
|
|
|
security=[],
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Log in', get_user=lambda r: {'user_name': r.data.get('username', None)},
|
|
|
|
|
|
get_details=_get_details)
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
login_request = LoginSerializer(data=request.data)
|
|
|
|
|
|
# 校验请求参数
|
|
|
|
|
|
user = login_request.is_valid(raise_exception=True)
|
|
|
|
|
|
token = login_request.get_user_token()
|
|
|
|
|
|
token_cache.set(token, user, timeout=JWT_AUTH['JWT_EXPIRATION_DELTA'])
|
|
|
|
|
|
return result.success(token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Register(APIView):
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@permission_classes((AllowAny,))
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("User registration"),
|
|
|
|
|
|
operation_id=_("User registration"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=RegisterSerializer().get_request_body_api(),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=RegisterSerializer().get_response_body_api(),
|
|
|
|
|
|
security=[],
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='User registration', get_user=lambda r: {'user_name': r.data.get('username', None)})
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
serializer_obj = RegisterSerializer(data=request.data)
|
|
|
|
|
|
if serializer_obj.is_valid(raise_exception=True):
|
|
|
|
|
|
serializer_obj.save()
|
2025-01-13 03:15:51 +00:00
|
|
|
|
return result.success(_("Registration successful"))
|
2023-09-15 09:40:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RePasswordView(APIView):
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@permission_classes((AllowAny,))
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Change password"),
|
|
|
|
|
|
operation_id=_("Change password"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=RePasswordSerializer().get_request_body_api(),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=RePasswordSerializer().get_response_body_api(),
|
|
|
|
|
|
security=[],
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Change password',
|
|
|
|
|
|
get_user=lambda r: {'user_name': None, 'email': r.data.get('email', None)})
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
serializer_obj = RePasswordSerializer(data=request.data)
|
|
|
|
|
|
return result.success(serializer_obj.reset_password())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CheckCode(APIView):
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
|
|
|
|
|
@permission_classes((AllowAny,))
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Check whether the verification code is correct"),
|
|
|
|
|
|
operation_id=_("Check whether the verification code is correct"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=CheckCodeSerializer().get_request_body_api(),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=CheckCodeSerializer().get_response_body_api(),
|
|
|
|
|
|
security=[],
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Check whether the verification code is correct',
|
|
|
|
|
|
get_user=lambda r: {'user_name': None, 'email': r.data.get('email', None)})
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
return result.success(CheckCodeSerializer(data=request.data).is_valid(raise_exception=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SendEmail(APIView):
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Send email"),
|
|
|
|
|
|
operation_id=_("Send email"),
|
2023-09-15 09:40:35 +00:00
|
|
|
|
request_body=SendEmailSerializer().get_request_body_api(),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
responses=SendEmailSerializer().get_response_body_api(),
|
|
|
|
|
|
security=[],
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Send email',
|
|
|
|
|
|
get_user=lambda r: {'user_name': None, 'email': r.data.get('email', None)})
|
2023-09-15 09:40:35 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
serializer_obj = SendEmailSerializer(data=request.data)
|
|
|
|
|
|
if serializer_obj.is_valid(raise_exception=True):
|
|
|
|
|
|
return result.success(serializer_obj.send())
|
2024-03-18 07:34:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserManage(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['POST'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Add user"),
|
|
|
|
|
|
operation_id=_("Add user"),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
request_body=UserManageSerializer.UserInstance.get_request_body_api(),
|
|
|
|
|
|
responses=result.get_api_response(UserInstanceSerializer.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User management")]
|
2024-03-18 07:34:02 +00:00
|
|
|
|
)
|
|
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Add user')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
return result.success(UserManageSerializer().save(request.data))
|
|
|
|
|
|
|
|
|
|
|
|
class Page(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get user paginated list"),
|
|
|
|
|
|
operation_id=_("Get user paginated list"),
|
|
|
|
|
|
tags=[_("User management")],
|
2024-03-18 07:34:02 +00:00
|
|
|
|
manual_parameters=UserManageSerializer.Query.get_request_params_api(),
|
|
|
|
|
|
responses=result.get_page_api_response(UserInstanceSerializer.get_response_body_api()),
|
|
|
|
|
|
)
|
|
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Get user paginated list')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def get(self, request: Request, current_page, page_size):
|
|
|
|
|
|
d = UserManageSerializer.Query(
|
2024-03-20 07:56:01 +00:00
|
|
|
|
data={'email_or_username': request.query_params.get('email_or_username', None),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
'user_id': str(request.user.id)})
|
|
|
|
|
|
return result.success(d.page(current_page, page_size))
|
|
|
|
|
|
|
|
|
|
|
|
class RePassword(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['PUT'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Change password"),
|
|
|
|
|
|
operation_id=_("Change password"),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
manual_parameters=UserInstanceSerializer.get_request_params_api(),
|
|
|
|
|
|
request_body=UserManageSerializer.RePasswordInstance.get_request_body_api(),
|
|
|
|
|
|
responses=result.get_default_response(),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User management")])
|
2024-03-18 07:34:02 +00:00
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Change password')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def put(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
UserManageSerializer.Operate(data={'id': user_id}).re_password(request.data, with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
class Operate(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['DELETE'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Delete user"),
|
|
|
|
|
|
operation_id=_("Delete user"),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
manual_parameters=UserInstanceSerializer.get_request_params_api(),
|
|
|
|
|
|
responses=result.get_default_response(),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User management")])
|
2024-03-18 07:34:02 +00:00
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Delete user')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def delete(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(UserManageSerializer.Operate(data={'id': user_id}).delete(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get user information"),
|
|
|
|
|
|
operation_id=_("Get user information"),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
manual_parameters=UserInstanceSerializer.get_request_params_api(),
|
|
|
|
|
|
responses=result.get_api_response(UserInstanceSerializer.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User management")]
|
2024-03-18 07:34:02 +00:00
|
|
|
|
)
|
|
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Get user information')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def get(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(UserManageSerializer.Operate(data={'id': user_id}).one(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
@action(methods=['PUT'], detail=False)
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Update user information"),
|
|
|
|
|
|
operation_id=_("Update user information"),
|
2024-03-18 07:34:02 +00:00
|
|
|
|
manual_parameters=UserInstanceSerializer.get_request_params_api(),
|
|
|
|
|
|
request_body=UserManageSerializer.UserEditInstance.get_request_body_api(),
|
|
|
|
|
|
responses=result.get_api_response(UserInstanceSerializer.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User management")]
|
2024-03-18 07:34:02 +00:00
|
|
|
|
)
|
|
|
|
|
|
@has_permissions(ViewPermission(
|
|
|
|
|
|
[RoleConstants.ADMIN],
|
|
|
|
|
|
[PermissionConstants.USER_READ],
|
|
|
|
|
|
compare=CompareConstants.AND))
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Update user information')
|
2024-03-18 07:34:02 +00:00
|
|
|
|
def put(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
UserManageSerializer.Operate(data={'id': user_id}).edit(request.data, with_valid=True))
|
2024-11-05 03:52:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserListView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
2025-01-13 03:15:51 +00:00
|
|
|
|
@swagger_auto_schema(operation_summary=_("Get user list by type"),
|
|
|
|
|
|
operation_id=_("Get user list by type"),
|
2024-11-05 03:52:56 +00:00
|
|
|
|
manual_parameters=UserSerializer.Query.get_request_params_api(),
|
|
|
|
|
|
responses=result.get_api_array_response(UserSerializer.Query.get_response_body_api()),
|
2025-01-13 03:15:51 +00:00
|
|
|
|
tags=[_("User")])
|
2024-11-05 03:52:56 +00:00
|
|
|
|
@has_permissions(PermissionConstants.USER_READ)
|
2025-03-19 07:45:10 +00:00
|
|
|
|
@log(menu='User', operate='Get user list by type')
|
2024-11-05 03:52:56 +00:00
|
|
|
|
def get(self, request: Request, type):
|
|
|
|
|
|
return result.success(UserSerializer().listByType(type, request.user.id))
|