forked from erp-dev/erp
feat: api_v2
This commit is contained in:
8
api_v2/views/__init__.py
Normal file
8
api_v2/views/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
api_v2 视图包。
|
||||
"""
|
||||
|
||||
from .users import QuickCreateEmployeeUserView
|
||||
|
||||
__all__ = ['QuickCreateEmployeeUserView']
|
||||
|
||||
92
api_v2/views/users.py
Normal file
92
api_v2/views/users.py
Normal file
@@ -0,0 +1,92 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import transaction
|
||||
from rest_framework import serializers, status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from basic_info import models as basic_models
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class QuickEmployeeUserCreateSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=150)
|
||||
password = serializers.CharField(write_only=True, min_length=6)
|
||||
display_name = serializers.CharField(max_length=100)
|
||||
merchant_id = serializers.IntegerField()
|
||||
email = serializers.EmailField(required=False, allow_blank=True, allow_null=True)
|
||||
mobile = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=20)
|
||||
area = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=100)
|
||||
description = serializers.CharField(required=False, allow_blank=True, allow_null=True)
|
||||
status = serializers.ChoiceField(
|
||||
choices=basic_models.EmployeeStatusEnum.choices,
|
||||
required=False,
|
||||
default=basic_models.EmployeeStatusEnum.ACTIVE,
|
||||
)
|
||||
|
||||
def validate_username(self, value):
|
||||
if User.objects.filter(username=value).exists():
|
||||
raise serializers.ValidationError('用户名已存在')
|
||||
return value
|
||||
|
||||
def validate_merchant_id(self, value):
|
||||
if not basic_models.Merchant.objects.filter(id=value).exists():
|
||||
raise serializers.ValidationError('商户不存在')
|
||||
return value
|
||||
|
||||
|
||||
class QuickCreateEmployeeUserView(APIView):
|
||||
"""
|
||||
快速创建系统用户 + 关联员工。
|
||||
|
||||
POST /api/v2/users/quick-create/
|
||||
"""
|
||||
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
serializer = QuickEmployeeUserCreateSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
data = serializer.validated_data
|
||||
|
||||
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
|
||||
|
||||
with transaction.atomic():
|
||||
user = User.objects.create_user(
|
||||
username=data['username'],
|
||||
password=data['password'],
|
||||
email=data.get('email') or '',
|
||||
)
|
||||
user.first_name = display_name
|
||||
user.save(update_fields=['first_name'])
|
||||
|
||||
employee = basic_models.Employee.objects.create(
|
||||
merchant=merchant,
|
||||
sys_user=user,
|
||||
name=display_name,
|
||||
mobile=data.get('mobile') or '',
|
||||
area=data.get('area') or '',
|
||||
description=data.get('description') or '',
|
||||
status=status_value,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{
|
||||
'user': {
|
||||
'id': user.id,
|
||||
'username': user.username,
|
||||
'display_name': display_name,
|
||||
},
|
||||
'employee': {
|
||||
'id': employee.id,
|
||||
'name': employee.name,
|
||||
'status': employee.status,
|
||||
'merchant': merchant.id,
|
||||
},
|
||||
},
|
||||
status=status.HTTP_201_CREATED,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user