from apps.qm.serializers import StandardCreateUpdateSerializer, StandardSerializer, TestItemCreateUpdateSerializer, TestItemSerializer
from apps.qm.models import Standard, TestItem
from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from apps.system.mixins import CreateUpdateModelAMixin
# Create your views here.
class StandardViewSet(CreateUpdateModelAMixin, ModelViewSet):
    """
    标准规范-增删改查
    """
    perms_map = {'*': '*'}
    queryset = Standard.objects.select_related('file').all()
    serializer_class = StandardSerializer
    search_fields = ['name', 'number']
    filterset_fields = []
    ordering_fields = ['number']
    ordering = ['id']

    def get_serializer_class(self):
        if self.action in ['create', 'update']:
            return StandardCreateUpdateSerializer
        return StandardSerializer

class TestItemViewSet(CreateUpdateModelAMixin, ModelViewSet):
    """
    检验项目增删改查
    """
    perms_map = {'*': '*'}
    queryset = TestItem.objects.select_related('standard').all()
    serializer_class = TestItemSerializer
    search_fields = ['name', 'standard__name', 'standard__number']
    filterset_fields = ['standard']
    ordering_fields = ['id']
    ordering = ['id']

    def get_serializer_class(self):
        if self.action in ['create', 'update']:
            return TestItemCreateUpdateSerializer
        return TestItemSerializer
