factory/apps/enm/serializers.py

89 lines
3.5 KiB
Python

from apps.utils.serializers import CustomModelSerializer
from apps.enm.models import Mpoint, MpLog, MpointStat, EnStat
from apps.utils.constants import EXCLUDE_FIELDS
from rest_framework import serializers
from apps.mtm.models import Mgroup
from rest_framework.exceptions import ParseError
class MpointSerializer(CustomModelSerializer):
mgroup = serializers.PrimaryKeyRelatedField(label="测点集", queryset=Mgroup.objects.all(), required=False)
mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
ep_monitored_name = serializers.CharField(source='ep_monitored.name', read_only=True)
ep_belong_name = serializers.CharField(source='ep_belong.name', read_only=True)
material_name = serializers.CharField(source='material.name', read_only=True)
class Meta:
model = Mpoint
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + ['belong_dept', 'cate']
def validate(self, attrs):
if 'material' in attrs and attrs['material']:
attrs['cate'] = 'material'
if 'mgroup' in attrs and attrs['mgroup']:
attrs['belong_dept'] = attrs['mgroup'].belong_dept
attrs['mgroups_allocate'] = [{'mgroup': attrs['mgroup'].id, 'mgroup_name': attrs['mgroup'].name, 'ratio': 1}]
ratio_ = 0
mgroupIds = []
for i in attrs['mgroups_allocate']:
ratio_ = ratio_ + i['ratio']
if i['mgroup'] in mgroupIds:
raise ParseError('分配集错误')
mgroupIds.append(i['mgroup'])
i['mgroup_name'] = Mgroup.objects.get(id=i['mgroup']).name
if round(ratio_, 3) != 1.0:
raise ParseError('比例合计错误')
return attrs
class MpLogSerializer(CustomModelSerializer):
mpoint_name = serializers.CharField(source='mpoint.name', read_only=True)
class Meta:
model = MpLog
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + ['mpoint_name']
class MpointStatSerializer(CustomModelSerializer):
mpoint_name = serializers.CharField(source='mpoint.name', read_only=True)
class Meta:
model = MpointStat
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + ['mpoint_name', 'type', 'year', 'month', 'day']
def check_required_keys(dictionary, keys):
for key in keys:
if key not in dictionary or not dictionary[key]:
return False
return True
def validate(self, attrs):
mpoint = attrs['mpoint']
if mpoint.material and mpoint.is_auto is False and 'sflog' in attrs and attrs['sflog']:
attrs['type'] = 'sflog'
sflog = attrs['sflog']
attrs['year_s'], attrs['month_s'], attrs['day_s'] = sflog.get_ymd
attrs['mgroup'] = sflog.mgroup
else:
raise ParseError('该数据不支持手工录入')
# if 'sflog' in attrs and attrs['sflog']:
# else:
# keys = ['hour', 'day_s', 'month_s', 'year_s']
# for ind, key in enumerate(keys):
# if key in attrs and attrs['key']:
# if not self.check_required_keys(attrs, keys[ind+1:]):
# raise ParseError('缺少数据')
# attrs['type'] = key
return super().validate(attrs)
class EnStatSerializer(CustomModelSerializer):
mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
class Meta:
model = EnStat
fields = '__all__'