
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from apps.utils.mixins import CustomListModelMixin, RetrieveModelMixin

from apps.wpmw.models import Wpr, WprDefect
from apps.wpmw.serializers import WprSerializer


class WprViewSet(CustomListModelMixin, RetrieveModelMixin, CustomGenericViewSet):
    """动态产品

    动态产品
    """
    perms_map = {"get": "*"}
    select_related_fields = ["wm", "mb", "material"]
    prefetch_related_fields = ["defects"]
    queryset = Wpr.objects.all()
    serializer_class = WprSerializer
    filterset_fields = {
        "mb": ["exact", "isnull"],
        "wm": ["exact", "isnull"],
        "material__process": ["exact"],
        "state": ["exact"],
        "defects": ["exact"]
    }
    ordering = ["-create_time"]
    ordering_fields = ["number", "create_time", "update_time"]
    search_fields = ["number", "material__name", "material__model", "material__specification"]

    def filter_queryset(self, queryset):
        qs = super().filter_queryset(queryset)
        if "mb__isnull" in self.request.query_params or "wm__isnull" in self.request.query_params:
            pass
        else:
            qs.exclude(mb=None, wm=None)
        return qs