forked from erp-dev/erp
feat: added /api/v2/roles/
This commit is contained in:
@@ -3,7 +3,7 @@ api_v2 视图包。
|
||||
"""
|
||||
|
||||
from .healthy import HealthCheckView
|
||||
from .users import QuickCreateEmployeeUserView
|
||||
from .users import QuickCreateEmployeeUserView, RoleListView
|
||||
from .printing import (
|
||||
PrintingJobByCustomerView,
|
||||
PrintingJobV2Serializer,
|
||||
@@ -19,6 +19,7 @@ from .stateflow import BusinessObjectCloneView
|
||||
__all__ = [
|
||||
'HealthCheckView',
|
||||
'QuickCreateEmployeeUserView',
|
||||
'RoleListView',
|
||||
'PrintingJobByCustomerView',
|
||||
'PrintingJobV2Serializer',
|
||||
'PrintingJobBatchAdvancePreviewView',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Group
|
||||
from django.db import transaction
|
||||
from rest_framework import serializers, status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
@@ -10,6 +11,21 @@ from basic_info import models as basic_models
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class RoleListView(APIView):
|
||||
"""
|
||||
获取所有角色列表(基于 Django Group)。
|
||||
|
||||
GET /api/v2/roles/
|
||||
返回: [{"id": 1, "name": "管理员"}, ...]
|
||||
"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
roles = Group.objects.all().values('id', 'name').order_by('id')
|
||||
return Response(list(roles))
|
||||
|
||||
|
||||
class QuickEmployeeUserCreateSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=150)
|
||||
password = serializers.CharField(write_only=True, min_length=6)
|
||||
@@ -24,6 +40,7 @@ class QuickEmployeeUserCreateSerializer(serializers.Serializer):
|
||||
required=False,
|
||||
default=basic_models.EmployeeStatusEnum.ACTIVE,
|
||||
)
|
||||
role_id = serializers.IntegerField(required=False, allow_null=True, help_text='角色ID(可选)')
|
||||
|
||||
def validate_username(self, value):
|
||||
if User.objects.filter(username=value).exists():
|
||||
@@ -35,6 +52,11 @@ class QuickEmployeeUserCreateSerializer(serializers.Serializer):
|
||||
raise serializers.ValidationError('商户不存在')
|
||||
return value
|
||||
|
||||
def validate_role_id(self, value):
|
||||
if value is not None and not Group.objects.filter(id=value).exists():
|
||||
raise serializers.ValidationError('角色不存在')
|
||||
return value
|
||||
|
||||
|
||||
class QuickCreateEmployeeUserView(APIView):
|
||||
"""
|
||||
@@ -53,6 +75,7 @@ class QuickCreateEmployeeUserView(APIView):
|
||||
merchant = basic_models.Merchant.objects.get(id=data['merchant_id'])
|
||||
display_name = data['display_name']
|
||||
status_value = data.get('status') or basic_models.EmployeeStatusEnum.ACTIVE
|
||||
role_id = data.get('role_id')
|
||||
|
||||
with transaction.atomic():
|
||||
user = User.objects.create_user(
|
||||
@@ -63,6 +86,11 @@ class QuickCreateEmployeeUserView(APIView):
|
||||
user.first_name = display_name
|
||||
user.save(update_fields=['first_name'])
|
||||
|
||||
# 如果指定了角色,将用户添加到对应的 Group
|
||||
if role_id:
|
||||
group = Group.objects.get(id=role_id)
|
||||
user.groups.add(group)
|
||||
|
||||
employee = basic_models.Employee.objects.create(
|
||||
merchant=merchant,
|
||||
sys_user=user,
|
||||
@@ -79,6 +107,7 @@ class QuickCreateEmployeeUserView(APIView):
|
||||
'id': user.id,
|
||||
'username': user.username,
|
||||
'display_name': display_name,
|
||||
'role_id': role_id,
|
||||
},
|
||||
'employee': {
|
||||
'id': employee.id,
|
||||
|
||||
Reference in New Issue
Block a user