输入输出物料接口
This commit is contained in:
parent
03f88f89c1
commit
c66581adbb
|
@ -0,0 +1,68 @@
|
||||||
|
# Generated by Django 3.2.6 on 2021-08-30 03:29
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mtm', '0003_auto_20210827_1604'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='process',
|
||||||
|
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='mtm.process', verbose_name='关联工序'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='product',
|
||||||
|
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='inputmaterial_product', to='mtm.material', verbose_name='关联产品'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='sort',
|
||||||
|
field=models.IntegerField(default=1, verbose_name='排序号'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='process',
|
||||||
|
field=models.ForeignKey(default=2, on_delete=django.db.models.deletion.CASCADE, to='mtm.process', verbose_name='关联工序'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='product',
|
||||||
|
field=models.ForeignKey(default=3, on_delete=django.db.models.deletion.CASCADE, related_name='outputmaterial_product', to='mtm.material', verbose_name='关联产品'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='sort',
|
||||||
|
field=models.IntegerField(default=1, verbose_name='排序号'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='material',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='inputmaterial', to='mtm.material', verbose_name='输入物料'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='number',
|
||||||
|
field=models.FloatField(default=1, verbose_name='消耗量'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='material',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='outputmaterial', to='mtm.material', verbose_name='输出物料'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='number',
|
||||||
|
field=models.FloatField(default=1, verbose_name='产出量'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Generated by Django 3.2.6 on 2021-08-30 05:13
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('mtm', '0004_auto_20210830_1129'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='inputmaterial',
|
||||||
|
name='unit',
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='outputmaterial',
|
||||||
|
name='unit',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='material',
|
||||||
|
name='unit',
|
||||||
|
field=models.CharField(choices=[('块', '块'), ('套', '套')], default='块', max_length=10, verbose_name='基准计量单位'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -15,11 +15,16 @@ class Material(CommonAModel):
|
||||||
(2, '半成品'),
|
(2, '半成品'),
|
||||||
(3, '原材料')
|
(3, '原材料')
|
||||||
)
|
)
|
||||||
|
unit_choices =(
|
||||||
|
('块', '块'),
|
||||||
|
('套', '套')
|
||||||
|
)
|
||||||
name = models.CharField('物料名称', max_length=100, unique=True)
|
name = models.CharField('物料名称', max_length=100, unique=True)
|
||||||
number = models.CharField('编号', max_length=100, unique=True)
|
number = models.CharField('编号', max_length=100, unique=True)
|
||||||
type = models.CharField('物料类型', choices= type_choices, max_length=20, default=1)
|
type = models.CharField('物料类型', choices= type_choices, max_length=20, default=1)
|
||||||
sort_str = models.CharField('排序字符', max_length=100, null=True, blank=True)
|
sort_str = models.CharField('排序字符', max_length=100, null=True, blank=True)
|
||||||
processes = models.JSONField('工艺流程', default=list, blank=True)
|
processes = models.JSONField('工艺流程', default=list, blank=True)
|
||||||
|
unit = models.CharField('基准计量单位', choices=unit_choices, default='块', max_length=10)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = '物料表'
|
verbose_name = '物料表'
|
||||||
|
@ -110,9 +115,11 @@ class InputMaterial(CommonAModel):
|
||||||
"""
|
"""
|
||||||
输入物料
|
输入物料
|
||||||
"""
|
"""
|
||||||
material = models.ForeignKey(Material, verbose_name='输入物料', on_delete=models.CASCADE)
|
material = models.ForeignKey(Material, verbose_name='输入物料', on_delete=models.CASCADE, related_name='inputmaterial')
|
||||||
number = models.FloatField('消耗量', default=0)
|
number = models.FloatField('消耗量', default=1)
|
||||||
unit = models.CharField('单位', max_length=20)
|
product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='inputmaterial_product')
|
||||||
|
process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
|
||||||
|
sort = models.IntegerField('排序号', default=1)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = '输入物料'
|
verbose_name = '输入物料'
|
||||||
|
@ -123,9 +130,11 @@ class OutputMaterial(CommonAModel):
|
||||||
"""
|
"""
|
||||||
输出物料
|
输出物料
|
||||||
"""
|
"""
|
||||||
material = models.ForeignKey(Material, verbose_name='输出物料', on_delete=models.CASCADE)
|
material = models.ForeignKey(Material, verbose_name='输出物料', on_delete=models.CASCADE, related_name='outputmaterial')
|
||||||
number = models.FloatField('产出量', default=0)
|
number = models.FloatField('产出量', default=1)
|
||||||
unit = models.CharField('单位', max_length=20)
|
product = models.ForeignKey(Material, verbose_name='关联产品', on_delete=models.CASCADE, related_name='outputmaterial_product')
|
||||||
|
process = models.ForeignKey(Process, verbose_name='关联工序', on_delete=models.CASCADE)
|
||||||
|
sort = models.IntegerField('排序号', default=1)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = '输出物料'
|
verbose_name = '输出物料'
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
from .models import Material, Process, ProductProcess, Step
|
from .models import InputMaterial, Material, OutputMaterial, Process, ProductProcess, Step
|
||||||
from apps.system.serializers import FileSimpleSerializer
|
from apps.system.serializers import FileSimpleSerializer
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ class MaterialDetailSerializer(serializers.ModelSerializer):
|
||||||
class MaterialSimpleSerializer(serializers.ModelSerializer):
|
class MaterialSimpleSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Material
|
model = Material
|
||||||
fields = ['id', 'name', 'number']
|
fields = ['id', 'name', 'number', 'unit']
|
||||||
|
|
||||||
class ProcessSerializer(serializers.ModelSerializer):
|
class ProcessSerializer(serializers.ModelSerializer):
|
||||||
instruction_ = FileSimpleSerializer(source='instruction', read_only=True)
|
instruction_ = FileSimpleSerializer(source='instruction', read_only=True)
|
||||||
|
@ -53,3 +53,46 @@ class ProductProcessUpdateSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ProductProcess
|
model = ProductProcess
|
||||||
fields = ['sort']
|
fields = ['sort']
|
||||||
|
|
||||||
|
class InputMaterialListSerializer(serializers.ModelSerializer):
|
||||||
|
material_ = MaterialSimpleSerializer(source='material', read_only=True)
|
||||||
|
class Meta:
|
||||||
|
model = InputMaterial
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
class OutputMaterialListSerializer(serializers.ModelSerializer):
|
||||||
|
material_ = MaterialSimpleSerializer(source='material', read_only=True)
|
||||||
|
class Meta:
|
||||||
|
model = OutputMaterial
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
class InputMaterialSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = InputMaterial
|
||||||
|
fields = ['number', 'unit', 'sort', 'material', 'product', 'process']
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
if InputMaterial.objects.filter(material=validated_data['material'], product=validated_data['product'], process=validated_data['process'], is_deleted=False).exists():
|
||||||
|
raise ValidationError('该物料已存在')
|
||||||
|
return super().create(validated_data)
|
||||||
|
|
||||||
|
class InputMaterialUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = InputMaterial
|
||||||
|
fields = ['number', 'unit', 'sort']
|
||||||
|
|
||||||
|
class OutputMaterialSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = OutputMaterial
|
||||||
|
fields = ['number', 'unit', 'sort', 'material', 'product', 'process']
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
if OutputMaterial.objects.filter(material=validated_data['material'], product=validated_data['product'], process=validated_data['process'], is_deleted=False).exists():
|
||||||
|
raise ValidationError('该物料已存在')
|
||||||
|
return super().create(validated_data)
|
||||||
|
|
||||||
|
class OutputMaterialUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = OutputMaterial
|
||||||
|
fields = ['number', 'unit', 'sort']
|
|
@ -1,14 +1,16 @@
|
||||||
from django.db.models import base
|
from django.db.models import base
|
||||||
from rest_framework import urlpatterns
|
from rest_framework import urlpatterns
|
||||||
from apps.mtm.views import MaterialViewSet, ProcessViewSet, ProductProcessViewSet, StepViewSet
|
from apps.mtm.views import InputMaterialViewSet, MaterialViewSet, OutputMaterialViewSet, ProcessViewSet, StepViewSet
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
router = DefaultRouter()
|
router = DefaultRouter()
|
||||||
router.register('material', MaterialViewSet, basename='material')
|
router.register('material', MaterialViewSet, basename='material')
|
||||||
router.register('process', ProcessViewSet, basename='process')
|
router.register('process', ProcessViewSet, basename='process')
|
||||||
router.register('productprocess', ProductProcessViewSet, basename='productprocess')
|
# router.register('productprocess', ProductProcessViewSet, basename='productprocess')
|
||||||
router.register('step', StepViewSet, basename='step')
|
router.register('step', StepViewSet, basename='step')
|
||||||
|
router.register('inputmaterial', InputMaterialViewSet, basename='inputmaterial')
|
||||||
|
router.register('outputmaterial', OutputMaterialViewSet, basename='outputmaterial')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
path('', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,12 +2,13 @@ from django.shortcuts import render
|
||||||
from rest_framework.viewsets import ModelViewSet, GenericViewSet
|
from rest_framework.viewsets import ModelViewSet, GenericViewSet
|
||||||
from rest_framework.mixins import CreateModelMixin, ListModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin
|
from rest_framework.mixins import CreateModelMixin, ListModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin
|
||||||
|
|
||||||
from apps.mtm.models import Material, Process, ProductProcess, Step
|
from apps.mtm.models import InputMaterial, Material, OutputMaterial, Process, ProductProcess, Step
|
||||||
from apps.mtm.serializers import MaterialDetailSerializer, MaterialSerializer, MaterialSimpleSerializer, ProductProcessListSerializer, ProductProcessUpdateSerializer, ProcessSerializer, StepSerializer
|
from apps.mtm.serializers import InputMaterialListSerializer, InputMaterialSerializer, InputMaterialUpdateSerializer, MaterialDetailSerializer, MaterialSerializer, MaterialSimpleSerializer, OutputMaterialListSerializer, OutputMaterialSerializer, OutputMaterialUpdateSerializer, ProductProcessListSerializer, ProductProcessUpdateSerializer, ProcessSerializer, StepSerializer
|
||||||
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin
|
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from utils.pagination import PageOrNot
|
from utils.pagination import PageOrNot
|
||||||
|
from rest_framework.exceptions import APIException
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
@ -61,8 +62,9 @@ class ProcessViewSet(PageOrNot, CreateUpdateModelAMixin, ModelViewSet):
|
||||||
serializer = self.serializer_class(instance=Step.objects.filter(process=process, is_deleted=False), many=True)
|
serializer = self.serializer_class(instance=Step.objects.filter(process=process, is_deleted=False), many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
class StepViewSet(CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin, GenericViewSet):
|
class StepViewSet(CreateUpdateModelAMixin, CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, DestroyModelMixin, GenericViewSet):
|
||||||
"""
|
"""
|
||||||
|
子工序-增删改查
|
||||||
"""
|
"""
|
||||||
perms_map = {'*':'process_update'}
|
perms_map = {'*':'process_update'}
|
||||||
queryset = Step.objects.all()
|
queryset = Step.objects.all()
|
||||||
|
@ -72,17 +74,51 @@ class StepViewSet(CreateModelMixin, UpdateModelMixin, RetrieveModelMixin, Destro
|
||||||
ordering = ['sort']
|
ordering = ['sort']
|
||||||
|
|
||||||
|
|
||||||
class ProductProcessViewSet(PageOrNot, CreateModelMixin, UpdateModelMixin, ListModelMixin, DestroyModelMixin, GenericViewSet):
|
# class ProductProcessViewSet(PageOrNot, CreateModelMixin, UpdateModelMixin, ListModelMixin, DestroyModelMixin, GenericViewSet):
|
||||||
|
# """
|
||||||
|
# 产品生产工艺流程增删改查
|
||||||
|
# """
|
||||||
|
# perms_map={'*':'*'}
|
||||||
|
# queryset = ProductProcess.objects.select_related('process', 'product').all()
|
||||||
|
# filterset_fields = ['process', 'product']
|
||||||
|
# serializer_class = ProductProcessListSerializer
|
||||||
|
# ordering = ['sort']
|
||||||
|
|
||||||
|
# def get_serializer_class(self):
|
||||||
|
# if self.action == 'update':
|
||||||
|
# return ProductProcessUpdateSerializer
|
||||||
|
# return super().get_serializer_class()
|
||||||
|
|
||||||
|
class InputMaterialViewSet(CreateUpdateModelAMixin, ModelViewSet):
|
||||||
"""
|
"""
|
||||||
产品生产工艺流程增删改查
|
输入物料-增删改查
|
||||||
"""
|
"""
|
||||||
perms_map = {'*':'*'}
|
perms_map = {'*':'*'}
|
||||||
queryset = ProductProcess.objects.select_related('process', 'product').all()
|
queryset = InputMaterial.objects.select_related('material').all()
|
||||||
filterset_fields = ['process', 'product']
|
serializer_class = InputMaterialSerializer
|
||||||
serializer_class = ProductProcessListSerializer
|
filterset_fields = ['process', 'material']
|
||||||
ordering = ['sort']
|
ordering = ['sort', '-create_time']
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
if self.action == 'update':
|
if self.action == 'list':
|
||||||
return ProductProcessUpdateSerializer
|
return InputMaterialListSerializer
|
||||||
return super().get_serializer_class()
|
elif self.action == 'update':
|
||||||
|
return InputMaterialUpdateSerializer
|
||||||
|
return InputMaterialSerializer
|
||||||
|
|
||||||
|
class OutputMaterialViewSet(CreateUpdateModelAMixin, ModelViewSet):
|
||||||
|
"""
|
||||||
|
输出物料-增删改查
|
||||||
|
"""
|
||||||
|
perms_map = {'*':'*'}
|
||||||
|
queryset = OutputMaterial.objects.select_related('material').all()
|
||||||
|
serializer_class = OutputMaterialSerializer
|
||||||
|
filterset_fields = ['process', 'material']
|
||||||
|
ordering = ['sort', '-create_time']
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
if self.action == 'list':
|
||||||
|
return OutputMaterialListSerializer
|
||||||
|
elif self.action == 'update':
|
||||||
|
return OutputMaterialUpdateSerializer
|
||||||
|
return OutputMaterialSerializer
|
|
@ -5,12 +5,21 @@ class MyPagination(PageNumberPagination):
|
||||||
page_size = 10
|
page_size = 10
|
||||||
page_size_query_param = 'page_size'
|
page_size_query_param = 'page_size'
|
||||||
|
|
||||||
|
def paginate_queryset(self, queryset, request, view):
|
||||||
|
if request.query_params.get('pageoff', None) or request.query_params.get('page', None)=='0':
|
||||||
|
if queryset.count()<500:
|
||||||
|
return None
|
||||||
|
elif queryset.count()>=500:
|
||||||
|
raise ParseError('单次请求数据量大,请分页获取')
|
||||||
|
return super().paginate_queryset(queryset, request, view=view)
|
||||||
|
|
||||||
class PageOrNot:
|
class PageOrNot:
|
||||||
def paginate_queryset(self, queryset):
|
def paginate_queryset(self, queryset):
|
||||||
if (self.paginator is None):
|
if (self.paginator is None):
|
||||||
return None
|
return None
|
||||||
elif self.request.query_params.get('pageoff', None) and queryset.count()<500:
|
elif self.request.query_params.get('pageoff', None) or self.request.query_params.get('page', None) == 0:
|
||||||
|
if queryset.count()<500:
|
||||||
return None
|
return None
|
||||||
elif self.request.query_params.get('pageoff', None) and queryset.count()>=500:
|
elif queryset.count()>=500:
|
||||||
raise ParseError('单次请求数据量大,请求中止')
|
raise ParseError('单次请求数据量大,请求中止')
|
||||||
return self.paginator.paginate_queryset(queryset, self.request, view=self)
|
return self.paginator.paginate_queryset(queryset, self.request, view=self)
|
||||||
|
|
Loading…
Reference in New Issue