新增离职登记接口
This commit is contained in:
103
User/views.py
103
User/views.py
@@ -39,8 +39,40 @@ class CreateUserView(APIView):
|
||||
contract = request.FILES.getlist('contract') # 合同
|
||||
ApplicationForm =request.FILES.getlist('ApplicationForm') # 入职申请表
|
||||
salary = request.data.get('salary') # 工资标准
|
||||
if not all([username, account, password, IdCard, department, position,nation,mobilePhone,team,Dateofjoining,academic,contract]):
|
||||
return Response({'status': 'error', 'message': '缺少参数', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 详细的参数验证,提供更明确的错误信息
|
||||
missing_params = []
|
||||
if not username:
|
||||
missing_params.append('username(姓名)')
|
||||
if not account:
|
||||
missing_params.append('account(账号)')
|
||||
if not password:
|
||||
missing_params.append('password(密码)')
|
||||
if not IdCard:
|
||||
missing_params.append('IdCard(身份证)')
|
||||
if not department:
|
||||
missing_params.append('department(归属部门)')
|
||||
if not position:
|
||||
missing_params.append('position(岗位)')
|
||||
if not nation:
|
||||
missing_params.append('nation(民族)')
|
||||
if not mobilePhone:
|
||||
missing_params.append('mobilePhone(手机号)')
|
||||
if not team:
|
||||
missing_params.append('team(所属团队)')
|
||||
if not Dateofjoining:
|
||||
missing_params.append('Dateofjoining(入职时间)')
|
||||
if not academic:
|
||||
missing_params.append('academic(学历)')
|
||||
if not contract or (isinstance(contract, list) and len(contract) == 0):
|
||||
missing_params.append('contract(合同文件)')
|
||||
|
||||
if missing_params:
|
||||
return Response({
|
||||
'status': 'error',
|
||||
'message': f'缺少参数: {", ".join(missing_params)}',
|
||||
'code': 1
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 检查用户名是否已存在(username字段有唯一性约束)
|
||||
if User.objects.filter(username=username).exists():
|
||||
@@ -50,18 +82,52 @@ class CreateUserView(APIView):
|
||||
ApplicationForm_url = flies(ApplicationForm )
|
||||
contract_url = flies(contract)
|
||||
|
||||
Dateofjoining = datetime.datetime.strptime(Dateofjoining, "%Y-%m-%d")
|
||||
# 日期格式验证和解析
|
||||
try:
|
||||
Dateofjoining = datetime.datetime.strptime(Dateofjoining, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return Response({'status': 'error', 'message': '入职时间格式错误,应为YYYY-MM-DD格式', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
if Confirmationtime:
|
||||
Confirmationtime = datetime.datetime.strptime(Confirmationtime, "%Y-%m-%d")
|
||||
try:
|
||||
Confirmationtime = datetime.datetime.strptime(Confirmationtime, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return Response({'status': 'error', 'message': '转正时间格式错误,应为YYYY-MM-DD格式', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
Confirmationtime = None
|
||||
|
||||
if Practicingcertificatetime:
|
||||
Practicingcertificatetime = datetime.datetime.strptime(Practicingcertificatetime, "%Y-%m-%d")
|
||||
try:
|
||||
Practicingcertificatetime = datetime.datetime.strptime(Practicingcertificatetime, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return Response({'status': 'error', 'message': '执业证时间格式错误,应为YYYY-MM-DD格式', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
Practicingcertificatetime = None
|
||||
role_list = ast.literal_eval(role)
|
||||
department_id = ast.literal_eval(department)
|
||||
user = User.objects.create(
|
||||
|
||||
# 解析角色和部门ID列表
|
||||
try:
|
||||
if role:
|
||||
role_list = ast.literal_eval(role) if isinstance(role, str) else role
|
||||
if not isinstance(role_list, list):
|
||||
role_list = [role_list] if role_list else []
|
||||
else:
|
||||
role_list = []
|
||||
except (ValueError, SyntaxError) as e:
|
||||
return Response({'status': 'error', 'message': f'角色参数格式错误: {str(e)}', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
if department:
|
||||
department_id = ast.literal_eval(department) if isinstance(department, str) else department
|
||||
if not isinstance(department_id, list):
|
||||
department_id = [department_id] if department_id else []
|
||||
else:
|
||||
department_id = []
|
||||
except (ValueError, SyntaxError) as e:
|
||||
return Response({'status': 'error', 'message': f'部门参数格式错误: {str(e)}', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# 创建用户,添加异常处理
|
||||
try:
|
||||
user = User.objects.create(
|
||||
username=username,
|
||||
account=account,
|
||||
password=password,
|
||||
@@ -79,10 +145,23 @@ class CreateUserView(APIView):
|
||||
ApplicationForm=json.dumps(ApplicationForm_url),
|
||||
state="待登记",
|
||||
salary=salary
|
||||
)
|
||||
user.role.add(*role_list)
|
||||
user.department.add(*department_id)
|
||||
return Response({'message': '添加人员成功', 'code': 0}, status=status.HTTP_200_OK)
|
||||
)
|
||||
# 添加角色和部门关联
|
||||
if role_list:
|
||||
user.role.add(*role_list)
|
||||
if department_id:
|
||||
user.department.add(*department_id)
|
||||
return Response({'message': '添加人员成功', 'code': 0}, status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
# 捕获数据库操作异常
|
||||
error_msg = str(e)
|
||||
if 'Duplicate entry' in error_msg or 'UNIQUE constraint' in error_msg:
|
||||
if 'username' in error_msg:
|
||||
return Response({'status': 'error', 'message': '用户名已存在,不能重复', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return Response({'status': 'error', 'message': '数据已存在,请检查唯一性约束', 'code': 1}, status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return Response({'status': 'error', 'message': f'创建用户失败: {error_msg}', 'code': 1}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
class EditorialStaffView(APIView):
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user