65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
| from apps.mtm.models import Step
 | |
| from rest_framework import serializers
 | |
| from rest_framework.serializers import ModelSerializer
 | |
| from rest_framework import exceptions
 | |
| 
 | |
| from utils.mixins import DynamicFieldsSerializerMixin
 | |
| from .models import Equipment, ECheckRecord
 | |
| from apps.system.serializers import OrganizationSimpleSerializer, UserSimpleSerializer
 | |
| 
 | |
| 
 | |
| class EquipmentListSerializer(DynamicFieldsSerializerMixin, ModelSerializer):
 | |
|     keeper_ = UserSimpleSerializer(source='keeper', read_only=True)
 | |
|     step_ = serializers.SerializerMethodField()
 | |
|     class Meta:
 | |
|         model = Equipment
 | |
|         fields = '__all__'
 | |
| 
 | |
|     @staticmethod
 | |
|     def setup_eager_loading(queryset):
 | |
|         """ Perform necessary eager loading of data. """
 | |
|         queryset = queryset.select_related('keeper')
 | |
|         return queryset
 | |
| 
 | |
|     def get_step_(self, obj):
 | |
|         return Step.objects.filter(equipments=obj).values('id', 'name', 'number')
 | |
| 
 | |
| 
 | |
| class EquipmentCreateUpdateSerializer(ModelSerializer):
 | |
|     class Meta:
 | |
|         model = Equipment
 | |
|         exclude = ['create_by', 'update_by', 'create_time', 'update_time', 'check_date', 'next_check_date']
 | |
|     
 | |
|     def validate(self, attrs):
 | |
|         if attrs['type'] == Equipment.EQUIP_TYPE_TEST:
 | |
|             if attrs['state'] not in [Equipment.EQUIP_STATE_OK, Equipment.EQUIP_STATE_DISABLE]:
 | |
|                 raise exceptions.APIException('设备状态错误')
 | |
|         return super().validate(attrs)
 | |
| 
 | |
| class EquipmentSimpleSerializer(ModelSerializer):
 | |
|     class Meta:
 | |
|         model = Equipment
 | |
|         fields = ['id', 'number', 'name', 'state', 'model', 'type']
 | |
| 
 | |
| 
 | |
| 
 | |
| class ECheckRecordListSerializer(ModelSerializer):
 | |
|     equipment_ = EquipmentSimpleSerializer(source='equipment', read_only=True)
 | |
|     class Meta:
 | |
|         model = ECheckRecord
 | |
|         fields = '__all__'
 | |
| 
 | |
|     @staticmethod
 | |
|     def setup_eager_loading(queryset):
 | |
|         """ Perform necessary eager loading of data. """
 | |
|         queryset = queryset.select_related('equipment')
 | |
|         return queryset
 | |
| 
 | |
| class EChcekRecordCreateSerializer(ModelSerializer):
 | |
|     class Meta:
 | |
|         model = ECheckRecord
 | |
|         fields = ['equipment', 'check_date', 'description']
 | |
| 
 | |
| class DaqCreateSerializer(serializers.Serializer):
 | |
|     number = serializers.CharField()
 | |
|     file = serializers.FileField() |