factory/apps/hrm/serializers.py

164 lines
6.0 KiB
Python
Executable File

from datetime import datetime, timedelta
from apps.hrm.errors import PHONE_F_WRONG
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from apps.hrm.services import HrmService
from apps.utils.fields import MyFilePathField
from apps.utils.serializers import CustomModelSerializer
from apps.utils.constants import EXCLUDE_FIELDS
from apps.hrm.models import Certificate, ClockRecord, Employee, NotWorkRemark
from apps.system.serializers import DeptSimpleSerializer, UserSimpleSerializer
from django.db import transaction
from apps.third.clients import dhClient
import re
from django.conf import settings
class EmployeeSimpleSerializer(CustomModelSerializer):
belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
post_name = serializers.CharField(source='post.name', read_only=True)
class Meta:
model = Employee
fields = ['id', 'type', 'name', 'belong_dept', 'belong_dept_name', 'post', 'post_name', 'photo', 'third_info']
# class EmployeeBaseSerializer(CustomModelSerializer):
# def save(self, **kwargs):
# if self.validated_data.get('user', None):
# user = self.validated_data['user']
# self.validated_data['name'] = user.name
# self.validated_data['belong_dept'] = user.belong_dept
# return super().save(**kwargs)
def phone_check(phone):
re_phone = r'^1[358]\d{9}$|^147\d{8}$|^176\d{8}$'
if not re.match(re_phone, phone):
raise serializers.ValidationError(**PHONE_F_WRONG)
return phone
class EmployeeCreateUpdateSerializer(CustomModelSerializer):
class Meta:
model = Employee
exclude = EXCLUDE_FIELDS + ['is_atwork', 'last_check_time',
'not_work_remark',
'third_info',
'type', 'name', 'phone', 'belong_dept', 'post', 'user']
extra_kwargs = {
'number': {'required': True},
'photo': {'required': True},
'id_number': {'required': True},
}
@transaction.atomic
def create(self, validated_data):
instance = super().create(validated_data)
if settings.DAHUA_ENABLED and dhClient and instance.type == 'employee':
# 如果是内部员工
HrmService.sync_dahua_employee(ep=instance)
return instance
def update(self, instance, validated_data):
old_photo = instance.photo
old_job_state = instance.job_state
instance = super().update(instance, validated_data)
if instance.type == 'remployee':
from apps.rpm.services import sync_to_rep
sync_to_rep(instance)
if instance.job_state == 20 and instance.user: # 如果离职了关闭账户
instance.user.is_active = False
instance.user.save()
# 同时去除门禁授权
if settings.DAHUA_ENABLED and dhClient:
if instance.type == 'employee':
start_time = None
end_time = None
if instance.job_state == 20 and old_job_state == 10: # 离职
now = datetime.now()
start_time = now
end_time = now + timedelta(minutes=60)
elif instance.job_state == 10 and old_job_state == 20: # 重新在职
now = datetime.now()
start_time = now
end_time = now + timedelta(days=7300)
HrmService.sync_dahua_employee(ep=instance, old_photo=old_photo,
start_time=start_time, end_time=end_time)
return instance
class EmployeeImproveSerializer(CustomModelSerializer):
photo_f = MyFilePathField(source='photo', read_only=True)
signature_f = MyFilePathField(source='signature', read_only=True)
class Meta:
model = Employee
fields = ['photo', 'id_number', 'email', 'gender', 'signature', 'photo_f', 'signature_f', 'phone']
class ChannelAuthoritySerializer(serializers.Serializer):
pks = serializers.ListField(child=serializers.CharField(max_length=20), label="员工ID列表")
channels = serializers.ListField(child=serializers.CharField(max_length=20), label="门通道ID列表")
class EmployeeSerializer(CustomModelSerializer):
belong_dept_ = DeptSimpleSerializer(source='belong_dept', read_only=True)
user_ = UserSimpleSerializer(source='user', read_only=True)
belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
post_name = serializers.CharField(source='post.name', read_only=True)
blt_ = serializers.SerializerMethodField()
photo_f = MyFilePathField(source='photo', read_only=True)
signature_f = MyFilePathField(source='signature', read_only=True)
class Meta:
model = Employee
fields = '__all__'
read_only_fields = ['is_atwork', 'last_check_time', 'not_work_remark']
def get_blt_(self, obj):
if hasattr(obj, 'tdevice'):
from apps.third.serializers import TDeviceSimpleSerializer
return TDeviceSimpleSerializer(instance=obj.tdevice).data
return
class EmployeeNotWorkRemarkSerializer(ModelSerializer):
class Meta:
model = Employee
fields = ['not_work_remark']
class ClockRecordListSerializer(serializers.ModelSerializer):
employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)
class Meta:
model = ClockRecord
fields = '__all__'
class NotWorkRemarkListSerializer(serializers.ModelSerializer):
class Meta:
model = NotWorkRemark
fields = '__all__'
class CertificateCreateUpdateSerializer(CustomModelSerializer):
file_f = MyFilePathField(source='file', read_only=True)
class Meta:
model = Certificate
exclude = EXCLUDE_FIELDS
class CertificateSerializer(CustomModelSerializer):
employee_name = serializers.CharField(source='employee.name', read_only=True)
file_f = MyFilePathField(source='file', read_only=True)
class Meta:
model = Certificate
fields = '__all__'