gault-millau/apps/collection/views/common.py
2019-09-27 17:11:42 +03:00

87 lines
2.7 KiB
Python

from rest_framework import generics
from rest_framework import permissions
from collection import models
from utils.pagination import ProjectPageNumberPagination
from django.shortcuts import get_object_or_404
from establishment.serializers import EstablishmentListSerializer
from collection.serializers import common as serializers
# Mixins
class CollectionViewMixin(generics.GenericAPIView):
"""Mixin for Collection view"""
model = models.Collection
queryset = models.Collection.objects.all()
class GuideViewMixin(generics.GenericAPIView):
"""Mixin for Guide view"""
model = models.Guide
queryset = models.Guide.objects.all()
# Views
# Collections
class CollectionListView(CollectionViewMixin, generics.ListAPIView):
"""List Collection view"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.CollectionSerializer
def get_queryset(self):
"""Override get_queryset method"""
queryset = models.Collection.objects.published()\
.by_country_code(code=self.request.country_code)\
.order_by('-on_top', '-created')
return queryset
class CollectionHomePageView(CollectionViewMixin, generics.ListAPIView):
"""List Collection view"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.CollectionSerializer
def get_queryset(self):
"""Override get_queryset method"""
queryset = models.Collection.objects.published()\
.by_country_code(code=self.request.country_code)\
.filter_all_related_gt(3)\
.order_by('-on_top', '-modified')
return queryset
class CollectionEstablishmentListView(CollectionListView):
"""Retrieve list of establishment for collection."""
permission_classes = (permissions.AllowAny,)
pagination_class = ProjectPageNumberPagination
serializer_class = EstablishmentListSerializer
lookup_field = 'slug'
def get_queryset(self):
"""
Override get_queryset method.
"""
queryset = super(CollectionEstablishmentListView, self).get_queryset()
# Perform the lookup filtering.
collection = get_object_or_404(queryset, slug=self.kwargs['slug'])
# May raise a permission denied
self.check_object_permissions(self.request, collection)
return collection.establishments.all()
# Guide
class GuideListView(GuideViewMixin, generics.ListAPIView):
"""List Guide view"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.GuideSerializer
class GuideRetrieveView(GuideViewMixin, generics.RetrieveAPIView):
"""Retrieve Guide view"""
permission_classes = (permissions.AllowAny,)
serializer_class = serializers.GuideSerializer