145 lines
4.9 KiB
Python
Executable File
145 lines
4.9 KiB
Python
Executable File
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:
|
|
HrmService.sync_dahua_employee(ep=instance)
|
|
return instance
|
|
|
|
def update(self, instance, validated_data):
|
|
old_photo = instance.photo
|
|
instance = super().update(instance, validated_data)
|
|
if settings.DAHUA_ENABLED and dhClient:
|
|
HrmService.sync_dahua_employee(ep=instance, old_photo=old_photo)
|
|
if instance.job_state == 20 and instance.user: # 如果离职了关闭账户
|
|
instance.user.is_active = False
|
|
instance.user.save()
|
|
return instance
|
|
|
|
|
|
class EmployeeImproveSerializer(CustomModelSerializer):
|
|
photo = MyFilePathField()
|
|
signature = MyFilePathField()
|
|
|
|
class Meta:
|
|
model = Employee
|
|
fields = ['photo', 'id_number', 'email', 'gender', 'signature']
|
|
|
|
|
|
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 = MyFilePathField()
|
|
signature = MyFilePathField()
|
|
|
|
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):
|
|
create_by_ = UserSimpleSerializer(source='create_by', read_only=True)
|
|
|
|
class Meta:
|
|
model = ClockRecord
|
|
fields = '__all__'
|
|
|
|
|
|
class NotWorkRemarkListSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = NotWorkRemark
|
|
fields = '__all__'
|
|
|
|
|
|
class CertificateCreateUpdateSerializer(CustomModelSerializer):
|
|
file = MyFilePathField()
|
|
|
|
class Meta:
|
|
model = Certificate
|
|
exclude = EXCLUDE_FIELDS
|
|
|
|
|
|
class CertificateSerializer(CustomModelSerializer):
|
|
employee_name = serializers.CharField(source='employee.name', read_only=True)
|
|
file = MyFilePathField()
|
|
|
|
class Meta:
|
|
model = Certificate
|
|
fields = '__all__'
|