from django.shortcuts import render

from rest_framework.viewsets import ModelViewSet
from .models import Enterprise,EnterpriseAddress
from utils.queryset import get_child_queryset2
from .serializers import EnterpriseSerializer,EnterpriseListSerializer,EnterpriseAddressSerializer
from apps.system.permission_data import RbacFilterSet
from apps.system.mixins import CreateUpdateCustomMixin, OptimizationMixin
# Create your views here.

class EnterpriseViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'Enterprise_create',
                 'put': 'Enterprise_update', 'delete': 'Enterprise_delete'}
    queryset = Enterprise.objects
    serializer_class = EnterpriseSerializer
    search_fields = ['name','query_code', 'code']
    ordering = ['-create_time']
    def get_serializer_class(self):
        # 根据请求类型动态变更serializer
        if self.action == 'list':
            return EnterpriseListSerializer
        return EnterpriseSerializer

   
class EnterpriseAddressViewSet(CreateUpdateCustomMixin, OptimizationMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'enterpriseaddress_create',
                 'put': 'enterpriseaddress_update', 'delete': 'enterpriseaddress_delete'}
    queryset = EnterpriseAddress.objects
    serializer_class = EnterpriseAddressSerializer
    search_fields = ['address']
    filterset_fields = ['enterprise']
    ordering = ['-create_time']
    def paginate_queryset(self, queryset):
       
        if ((not self.request.query_params.get('page', None)) and (self.request.query_params.get('enterprise', None))) or (self.paginator is None):
            return None
        return self.paginator.paginate_queryset(queryset, self.request, view=self)
    

