168 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Python
		
	
	
	
| from apps.hrm.models import Certificate
 | |
| from apps.opm.models import GasCheck, Operation, Opl, OplCate, OplCert, OplWorker
 | |
| from apps.system.serializers import DictSerializer, DictSimpleSerializer, UserSimpleSerializer
 | |
| from apps.utils.serializers import CustomModelSerializer
 | |
| from apps.utils.constants import EXCLUDE_FIELDS
 | |
| from rest_framework import serializers
 | |
| from django.db import transaction
 | |
| from rest_framework.exceptions import ParseError
 | |
| 
 | |
| 
 | |
| class OplCateCreateUpdateSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = OplCate
 | |
|         exclude = EXCLUDE_FIELDS
 | |
| 
 | |
| 
 | |
| class OplCateSerializer(CustomModelSerializer):
 | |
| 
 | |
|     class Meta:
 | |
|         model = OplCate
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| class OplCateDetailSerializer(CustomModelSerializer):
 | |
|     measure_options_ = DictSimpleSerializer(source='measure_options', read_only=True, many=True)
 | |
|     risk_options_ = DictSimpleSerializer(source='risk_options', read_only=True, many=True)
 | |
|     close_options_ = DictSimpleSerializer(source='close_options', read_only=True, many=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = OplCate
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| class OperationCreateUpdateSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = Operation
 | |
|         exclude = EXCLUDE_FIELDS + ['number']
 | |
| 
 | |
| 
 | |
| class OperationSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = Operation
 | |
|         fields = "__all__"
 | |
| 
 | |
| 
 | |
| class OplWorkerCreateSerializer(CustomModelSerializer):
 | |
|     certificates = serializers.PrimaryKeyRelatedField(label='证书ID', many=True, queryset=Certificate.objects.all())
 | |
| 
 | |
|     class Meta:
 | |
|         model = OplWorker
 | |
|         fields = ['worker', 'duty', 'certificates', 'opl']
 | |
| 
 | |
|     def create(self, validated_data):
 | |
|         opl = validated_data['opl']
 | |
|         if opl.ticket:
 | |
|             raise ParseError('许可证已处理不可新增')
 | |
|         certificates = validated_data.pop('certificates')
 | |
|         if OplWorker.objects.filter(worker=validated_data['worker'], opl=validated_data['opl']).exists():
 | |
|             raise ParseError('该成员已加入')
 | |
|         with transaction.atomic():
 | |
|             oplw = super().create(validated_data)
 | |
|             for x in certificates:
 | |
|                 oplc = OplCert.objects.filter(opl_worker=oplw, certificate=x).first()
 | |
|                 if oplc:
 | |
|                     pass
 | |
|                 else:
 | |
|                     oplc = OplCert()
 | |
|                     oplc.opl_worker = oplw
 | |
|                     oplc.certificate = x
 | |
|                 for f in Certificate._meta.fields:
 | |
|                     if f.name not in ['id']:
 | |
|                         setattr(oplc, f.name, getattr(x, f.name, None))
 | |
|                 oplc.save()
 | |
|         return oplw
 | |
| 
 | |
| 
 | |
| class OplWorkerUpdateSerializer(CustomModelSerializer):
 | |
|     certificates = serializers.PrimaryKeyRelatedField(label='证书ID', many=True, queryset=Certificate.objects.all())
 | |
| 
 | |
|     class Meta:
 | |
|         model = OplWorker
 | |
|         fields = ['duty', 'certificates']
 | |
| 
 | |
|     def update(self, instance, validated_data):
 | |
|         certificates = validated_data.pop('certificates')
 | |
|         with transaction.atomic():
 | |
|             oplw = super().update(instance, validated_data)
 | |
|             for x in certificates:
 | |
|                 oplc = OplCert.objects.filter(opl_worker=oplw, certificate=x).first()
 | |
|                 if oplc:
 | |
|                     pass
 | |
|                 else:
 | |
|                     oplc = OplCert()
 | |
|                     oplc.opl_worker = oplw
 | |
|                     oplc.certificate = x
 | |
|                 for f in Certificate._meta.fields:
 | |
|                     if f.name not in ['id']:
 | |
|                         setattr(oplc, f.name, getattr(x, f.name, None))
 | |
|                 oplc.save()
 | |
|         return oplw
 | |
| 
 | |
| 
 | |
| class OplWorkerSerializer(CustomModelSerializer):
 | |
|     worker_ = UserSimpleSerializer(source='worker', read_only=True)
 | |
|     certificates_ = serializers.SerializerMethodField()
 | |
| 
 | |
|     class Meta:
 | |
|         model = OplWorker
 | |
|         fields = '__all__'
 | |
| 
 | |
|     def get_certificates_(self, obj):
 | |
|         cs = OplCert.objects.filter(opl_worker=obj)
 | |
|         return cs.values('certificate', 'name', 'type', 'number', 'issue_date', 'expiration_date', 'review_date', 'file')
 | |
| 
 | |
| 
 | |
| class GasCheckCreateUpdateSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = GasCheck
 | |
|         exclude = EXCLUDE_FIELDS
 | |
| 
 | |
|     def create(self, validated_data):
 | |
|         opl = validated_data['opl']
 | |
|         if opl.ticket:
 | |
|             raise ParseError('许可证已处理不可新增')
 | |
|         return super().create(validated_data)
 | |
| 
 | |
| 
 | |
| class GasCheckSerializer(CustomModelSerializer):
 | |
|     checker_ = UserSimpleSerializer(source='checker', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = GasCheck
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| class OplCreateUpdateSerializer(CustomModelSerializer):
 | |
|     id = serializers.CharField(read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = Opl
 | |
|         fields = ['id', 'operation', 'level', 'cate', 'start_time', 'end_time',
 | |
|                   'dept_do', 'charger', 'monitor', 'work_time', 'work_type',
 | |
|                   'accept_time', 'power', 'power_to', 'power_from',
 | |
|                   'power_end_time', 'power_start_time', 'power_days',
 | |
|                   'other_risk', 'other_emr', 'escape_route',
 | |
|                   'risks_checked', 'measures_checked', 'create_imgs']
 | |
| 
 | |
|     def create(self, validated_data):
 | |
|         operation = validated_data['operation']
 | |
|         if operation.state == Operation.OP_CLOSE:
 | |
|             raise ParseError('作业已关闭不可创建许可')
 | |
|         return super().create(validated_data)
 | |
| 
 | |
| 
 | |
| class OplSerializer(CustomModelSerializer):
 | |
|     cate_name = serializers.CharField(source='cate.name', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = Opl
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| 
 | |
| class OplCloseSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = Opl
 | |
|         fields = ['close_imgs', 'close_note', 'close_dos', 'close_desc']
 |