feat: em增加einspect, equipment增加mgroup字段

This commit is contained in:
caoqianming 2023-11-21 18:55:49 +08:00
parent 784d1a79dc
commit 6263960a7c
5 changed files with 121 additions and 17 deletions

View File

@ -0,0 +1,42 @@
# Generated by Django 3.2.12 on 2023-11-21 10:52
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('mtm', '0025_auto_20231120_1139'),
('em', '0006_alter_equipment_number_factory'),
]
operations = [
migrations.AddField(
model_name='equipment',
name='mgroup',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='mtm.mgroup'),
),
migrations.CreateModel(
name='EInspect',
fields=[
('id', models.CharField(editable=False, help_text='主键ID', max_length=20, primary_key=True, serialize=False, verbose_name='主键ID')),
('create_time', models.DateTimeField(default=django.utils.timezone.now, help_text='创建时间', verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, help_text='修改时间', verbose_name='修改时间')),
('is_deleted', models.BooleanField(default=False, help_text='删除标记', verbose_name='删除标记')),
('inspect_time', models.DateTimeField(verbose_name='巡检时间')),
('result', models.CharField(choices=[('pass', '合格'), ('fail', '不合格')], help_text="(('pass', '合格'), ('fail', '不合格'))", max_length=20, verbose_name='巡检结果')),
('note', models.TextField(blank=True, null=True, verbose_name='备注')),
('create_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='einspect_create_by', to=settings.AUTH_USER_MODEL, verbose_name='创建人')),
('equipment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='em.equipment', verbose_name='关联设备')),
('inspect_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, verbose_name='巡检人')),
('update_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='einspect_update_by', to=settings.AUTH_USER_MODEL, verbose_name='最后编辑人')),
],
options={
'abstract': False,
},
),
]

View File

@ -1,5 +1,5 @@
from django.db import models
from apps.utils.models import CommonBModel, CommonADModel, CommonAModel
from apps.utils.models import CommonBModel, CommonADModel, CommonBDModel
from apps.system.models import User
# Create your models here.
@ -43,6 +43,8 @@ class Equipment(CommonBModel):
# (1, '专用'),
# (2, '公用'),
# )
mgroup = models.ForeignKey(
'mtm.mgroup', on_delete=models.SET_NULL, null=True, blank=True)
type = models.PositiveSmallIntegerField(
'类型', choices=type_choices, default=10)
name = models.CharField('设备名称', max_length=50)
@ -102,3 +104,26 @@ class EcheckRecord(CommonADModel):
note = models.TextField('备注', null=True, blank=True)
result = models.PositiveSmallIntegerField(
'结果', default=10, choices=CHECK_CHOICES, help_text=str(CHECK_CHOICES))
class EInspect(CommonADModel):
"""
巡检记录
"""
INSPECT_RESULTS = (
('pass', '合格'),
('fail', '不合格'),
# ('repair', '需要维修'),
# ('urgent_repair', '紧急维修'),
# ('partial_pass', '部分合格'),
# ('observation', '观测中'),
# ('repaired', '已修复'),
)
equipment = models.ForeignKey(
Equipment, verbose_name='关联设备', on_delete=models.CASCADE)
inspect_user = models.ForeignKey(
'system.user', verbose_name='巡检人', on_delete=models.CASCADE)
inspect_time = models.DateTimeField('巡检时间')
result = models.CharField(max_length=20, choices=INSPECT_RESULTS,
verbose_name='巡检结果', help_text=str(INSPECT_RESULTS))
note = models.TextField('备注', null=True, blank=True)

View File

@ -1,18 +1,30 @@
from apps.utils.serializers import CustomModelSerializer
from apps.em.models import Equipment, EcheckRecord
from apps.em.models import Equipment, EcheckRecord, EInspect
from apps.system.models import Dept
from apps.utils.constants import EXCLUDE_FIELDS, EXCLUDE_FIELDS_BASE
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
class EquipmentSerializer(CustomModelSerializer):
belong_dept = serializers.PrimaryKeyRelatedField(label='责任部门', queryset = Dept.objects.all())
belong_dept = serializers.PrimaryKeyRelatedField(
label='责任部门', queryset=Dept.objects.all())
keeper_name = serializers.CharField(source='keeper.name', read_only=True)
belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
belong_dept_name = serializers.CharField(
source='belong_dept.name', read_only=True)
mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
def validate(self, attrs):
mgroup = attrs.get('mgroup', None)
if mgroup:
attrs['belong_dept'] = mgroup.belong_dept
return super().validate(attrs)
class Meta:
model = Equipment
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS + ['check_date', 'next_check_date', 'keeper_name', 'belong_dept_name']
read_only_fields = EXCLUDE_FIELDS + \
['check_date', 'next_check_date', 'keeper_name', 'belong_dept_name']
class EcheckRecordSerializer(CustomModelSerializer):
@ -27,3 +39,9 @@ class EcheckRecordSerializer(CustomModelSerializer):
raise ValidationError('检定日期小于历史记录')
return attrs
class EInspectSerializer(CustomModelSerializer):
class Meta:
model = EInspect
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS

View File

@ -1,6 +1,6 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from apps.em.views import EquipmentViewSet, EcheckRecordViewSet
from apps.em.views import EquipmentViewSet, EcheckRecordViewSet, EInspectViewSet
API_BASE_URL = 'api/em/'
HTML_BASE_URL = 'em/'
@ -8,6 +8,7 @@ HTML_BASE_URL = 'em/'
router = DefaultRouter()
router.register('equipment', EquipmentViewSet, basename='equipment')
router.register('echeckrecord', EcheckRecordViewSet, basename='echeckrecord')
router.register('einspect', EInspectViewSet, basename='einspect')
urlpatterns = [
path(API_BASE_URL, include(router.urls)),
]
]

View File

@ -1,17 +1,19 @@
from django.shortcuts import render
from apps.em.models import Equipment, EcheckRecord
from apps.em.models import Equipment, EcheckRecord, EInspect
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from apps.em.serializers import EquipmentSerializer, EcheckRecordSerializer
from apps.em.serializers import EquipmentSerializer, EcheckRecordSerializer, EInspectSerializer
from apps.em.filters import EquipFilterSet
from rest_framework.exceptions import ParseError
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin
from dateutil.relativedelta import relativedelta
# Create your views here.
class EquipmentViewSet(CustomModelViewSet):
queryset = Equipment.objects.all()
serializer_class = EquipmentSerializer
select_related_fields = ['create_by', 'belong_dept', 'keeper']
select_related_fields = ['create_by', 'belong_dept', 'keeper', 'mgroup']
search_fields = ['number', 'name']
filterset_class = EquipFilterSet
@ -19,7 +21,7 @@ class EquipmentViewSet(CustomModelViewSet):
if not self.detail and not self.request.query_params.get('type', None):
raise ParseError('请指定设备类型')
return super().filter_queryset(queryset)
class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, CustomGenericViewSet):
"""
@ -27,7 +29,8 @@ class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, C
校准/检定记录
"""
perms_map = {'get': '*', 'post': 'echeckrecord.create', 'put': 'echeckrecord.update', 'delete': 'echeckrecord.delete'}
perms_map = {'get': '*', 'post': 'echeckrecord.create',
'put': 'echeckrecord.update', 'delete': 'echeckrecord.delete'}
queryset = EcheckRecord.objects.all()
serializer_class = EcheckRecordSerializer
select_related_fields = ['equipment', 'create_by']
@ -38,18 +41,33 @@ class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, C
equipment = instance.equipment
if equipment.cycle:
equipment.check_date = instance.check_date
equipment.next_check_date = instance.check_date + relativedelta(months=equipment.cycle)
equipment.next_check_date = instance.check_date + \
relativedelta(months=equipment.cycle)
equipment.save()
def perform_destroy(self, instance):
instance.delete()
equipment = instance.equipment
er = EcheckRecord.objects.filter(equipment=equipment).order_by('check_date').last()
er = EcheckRecord.objects.filter(
equipment=equipment).order_by('check_date').last()
if er:
equipment.check_date = instance.check_date
equipment.next_check_date = instance.check_date + relativedelta(months=equipment.cycle)
equipment.next_check_date = instance.check_date + \
relativedelta(months=equipment.cycle)
equipment.save()
else:
equipment.check_date = None
equipment.next_check_date = None
equipment.save()
equipment.save()
class EInspectViewSet(CustomModelViewSet):
"""
list:巡检记录
巡检记录
"""
queryset = EInspect.objects.all()
serializer_class = EInspectSerializer
select_related_fields = ['equipment']
filterset_fields = ['equipment']