"""Views for app advertisement"""
from rest_framework import generics
from rest_framework import permissions

from advertisement.models import Advertisement
from advertisement.serializers import AdvertisementBaseSerializer


class AdvertisementBaseView(generics.GenericAPIView):
    """Advertisement list view."""

    pagination_class = None
    permission_classes = (permissions.AllowAny, )
    serializer_class = AdvertisementBaseSerializer

    def get_queryset(self):
        """Overridden get queryset method."""
        return Advertisement.objects.with_base_related()


class AdvertisementPageTypeListView(AdvertisementBaseView, generics.ListAPIView):
    """Advertisement list view by page type."""

    def get_queryset(self):
        """Overridden get queryset method."""
        product_type = self.kwargs.get('page_type')
        qs = super(AdvertisementPageTypeListView, self).get_queryset()
        if product_type:
            return qs.by_page_type(product_type) \
                     .by_country(self.request.country_code) \
                     .by_locale(self.request.locale) \
                     .distinct('id')
        return qs.none()
