39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
from django.shortcuts import render
|
|
from apps.cm.models import LableMat
|
|
from rest_framework.decorators import action
|
|
from apps.cm.serializers import TidSerializer, LabelMatSerializer
|
|
from apps.inm.models import MaterialBatch
|
|
from rest_framework.exceptions import ParseError
|
|
from rest_framework.response import Response
|
|
from apps.utils.viewsets import CustomGenericViewSet, RetrieveModelMixin, CustomListModelMixin
|
|
# Create your views here.
|
|
|
|
class LableMatViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericViewSet):
|
|
perms_map = {"post": "*", "get": "*"}
|
|
serializer_class = LabelMatSerializer
|
|
queryset = LableMat.objects.all()
|
|
select_related_fields = ["material", "material_origin", "supplier"]
|
|
|
|
@action(methods=['post'], detail=False, serializer_class=TidSerializer)
|
|
def get_from_mb(self, request, pk=None):
|
|
"""
|
|
从仓库明细获取物料标签
|
|
|
|
从仓库明细获取物料标签
|
|
"""
|
|
tid = request.data.get('tid')
|
|
try:
|
|
mb = MaterialBatch.objects.get(id=tid)
|
|
except MaterialBatch.DoesNotExist:
|
|
raise ParseError('仓库明细不存在')
|
|
obj, _ = LableMat.objects.get_or_create(
|
|
state=10,
|
|
material=mb.material,
|
|
batch=mb.batch,
|
|
defaults={
|
|
"supplier": mb.supplier
|
|
}
|
|
)
|
|
rdata = LabelMatSerializer(obj).data
|
|
rdata["code_label"] = f"mat:{obj.id}"
|
|
return Response(rdata) |