# coding=utf-8

from rest_framework import generics
from rest_framework.exceptions import NotFound

from utils.permission import IsCustomer
from utils import response_ok
from apps.collection.models import Collection
from apps.collection.filters import CollectionFilter
from .serializers import CollectionSerializer


class CollectionListViewSet(generics.ListAPIView):
    permission_classes = [IsCustomer, ]
    queryset = Collection.objects.filter(delete=False)
    serializer_class = CollectionSerializer

    def filter_queryset(self, queryset):
        queryset = queryset.filter(customer=self.request.customer)
        f = CollectionFilter(self.request.GET, queryset=queryset)
        return f.qs

    def list(self, request, *args, **kwargs):
        try:
            data = super(CollectionListViewSet, self).list(request)
        except NotFound:
            return response_ok([])
        return data
