from django.shortcuts import render
from rest_framework.mixins import ListModelMixin
from rest_framework.viewsets import GenericViewSet, ModelViewSet

from apps.inm.models import WareHouse,Inventory
from apps.inm.serializers import WareHouseSerializer, WareHouseCreateUpdateSerializer,InventorySerializer,InventoryCreateUpdateSerializer
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin


# Create your views here.
class WarehouseViewSet(CreateUpdateModelAMixin, ModelViewSet):
    """
    仓库-增删改查
    """
    perms_map = {'*': '*'}
    queryset = WareHouse.objects.select_related('create_by').all()
    serializer_class = WareHouseSerializer
    search_fields = ['name', 'number', 'place']
    filterset_fields = []
    ordering_fields = ['create_time']
    ordering = ['-create_time']

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

class InventoryViewSet(ListModelMixin, GenericViewSet):
    """
    物料基本信息-增删改查
    """
    perms_map = {'*': '*'}
    queryset = Inventory.objects.select_related('create_by').all()
    serializer_class = InventorySerializer
    filterset_fields = []
    ordering_fields = ['create_time']
    ordering = ['-create_time']
