from apps.sam.serializers import ContractCreateUpdateSerializer, ContractSerializer, CustomerCreateUpdateSerializer, CustomerSerializer
from apps.sam.models import Contract, Customer
from rest_framework.viewsets import ModelViewSet
from apps.system.mixins import CreateUpdateCustomMixin
from django.shortcuts import render

# Create your views here.
class CustomerViewSet(CreateUpdateCustomMixin, ModelViewSet):
    """
    客户-增删改查
    """
    perms_map = {'*': '*'}
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer
    search_fields = ['name', 'contact']
    filterset_fields = []
    ordering_fields = ['create_time']
    ordering = ['-create_time']

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


class ContractViewSet(CreateUpdateCustomMixin, ModelViewSet):
    """
    合同-增删改查
    """
    perms_map = {'*': '*'}
    queryset = Contract.objects.select_related('customer').all()
    serializer_class = ContractSerializer
    search_fields = ['name']
    filterset_fields = []
    ordering_fields = ['create_time']
    ordering = ['-create_time']

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