added filter collection by country code from cookies
This commit is contained in:
parent
4cceb62d49
commit
6558c84be9
|
|
@ -28,9 +28,9 @@ class CollectionDateMixin(models.Model):
|
||||||
class CollectionQuerySet(models.QuerySet):
|
class CollectionQuerySet(models.QuerySet):
|
||||||
"""QuerySet for model Collection"""
|
"""QuerySet for model Collection"""
|
||||||
|
|
||||||
def by_country(self, country):
|
def by_country_code(self, code):
|
||||||
"""Filter collection by country."""
|
"""Filter collection by country code."""
|
||||||
return self.filter(country=country)
|
return self.filter(country__code=code)
|
||||||
|
|
||||||
def published(self):
|
def published(self):
|
||||||
"""Returned only published collection"""
|
"""Returned only published collection"""
|
||||||
|
|
|
||||||
|
|
@ -8,34 +8,34 @@ app_name = 'collection'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('list/', views.CollectionListView.as_view(),
|
path('list/', views.CollectionListView.as_view(),
|
||||||
name='collections_list'),
|
name='collections_list'),
|
||||||
path('create/', views.CollectionCreateView.as_view(),
|
# path('create/', views.CollectionCreateView.as_view(),
|
||||||
name='collection_create'),
|
# name='collection_create'),
|
||||||
path('<int:pk>/', views.CollectionRetrieveView.as_view(),
|
# path('<int:pk>/', views.CollectionRetrieveView.as_view(),
|
||||||
name='collection_retrieve'),
|
# name='collection_retrieve'),
|
||||||
path('<int:pk>/delete/', views.CollectionDestroyView.as_view(),
|
# path('<int:pk>/delete/', views.CollectionDestroyView.as_view(),
|
||||||
name='collection_destroy'),
|
# name='collection_destroy'),
|
||||||
path('<int:pk>/update/', views.CollectionUpdateView.as_view(),
|
# path('<int:pk>/update/', views.CollectionUpdateView.as_view(),
|
||||||
name='collection_update'),
|
# name='collection_update'),
|
||||||
|
|
||||||
path('collection_items/', views.CollectionItemListView.as_view(),
|
path('collection_items/', views.CollectionItemListView.as_view(),
|
||||||
name='collection_items_list'),
|
name='collection_items_list'),
|
||||||
path('collection_items/create/', views.CollectionItemCreateView.as_view(),
|
# path('collection_items/create/', views.CollectionItemCreateView.as_view(),
|
||||||
name='collection_items_create'),
|
# name='collection_items_create'),
|
||||||
path('collection_items/<int:pk>/', views.CollectionItemRetrieveView.as_view(),
|
# path('collection_items/<int:pk>/', views.CollectionItemRetrieveView.as_view(),
|
||||||
name='collection_items_retrieve'),
|
# name='collection_items_retrieve'),
|
||||||
path('collection_items/<int:pk>/delete/', views.CollectionDestroyView.as_view(),
|
# path('collection_items/<int:pk>/delete/', views.CollectionDestroyView.as_view(),
|
||||||
name='collection_items_destroy'),
|
# name='collection_items_destroy'),
|
||||||
path('collection_items/<int:pk>/update/', views.CollectionItemUpdateView.as_view(),
|
# path('collection_items/<int:pk>/update/', views.CollectionItemUpdateView.as_view(),
|
||||||
name='collection_items_update'),
|
# name='collection_items_update'),
|
||||||
|
|
||||||
path('guides/', views.GuideListView.as_view(),
|
path('guides/', views.GuideListView.as_view(),
|
||||||
name='guides_list'),
|
name='guides_list'),
|
||||||
path('guides/create/', views.GuideCreateView.as_view(),
|
# path('guides/create/', views.GuideCreateView.as_view(),
|
||||||
name='guide_create'),
|
# name='guide_create'),
|
||||||
path('guides/<int:pk>/', views.GuideRetrieveView.as_view(),
|
# path('guides/<int:pk>/', views.GuideRetrieveView.as_view(),
|
||||||
name='guide_retrieve'),
|
# name='guide_retrieve'),
|
||||||
path('guides/<int:pk>/delete/', views.GuideDestroyView.as_view(),
|
# path('guides/<int:pk>/delete/', views.GuideDestroyView.as_view(),
|
||||||
name='guide_destroy'),
|
# name='guide_destroy'),
|
||||||
path('guides/<int:pk>/update/', views.GuideUpdateView.as_view(),
|
# path('guides/<int:pk>/update/', views.GuideUpdateView.as_view(),
|
||||||
name='guide_update'),
|
# name='guide_update'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
|
|
||||||
from collection import models
|
from collection import models
|
||||||
from django.conf import settings
|
|
||||||
from collection.serializers import common as serializers
|
from collection.serializers import common as serializers
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,6 +11,10 @@ class CollectionViewMixin(generics.GenericAPIView):
|
||||||
model = models.Collection
|
model = models.Collection
|
||||||
queryset = models.Collection.objects.all()
|
queryset = models.Collection.objects.all()
|
||||||
|
|
||||||
|
def get_country_code(self):
|
||||||
|
"""Get country_code from cookies."""
|
||||||
|
return self.request.COOKIES.get('country_code')
|
||||||
|
|
||||||
|
|
||||||
class CollectionItemViewMixin(generics.GenericAPIView):
|
class CollectionItemViewMixin(generics.GenericAPIView):
|
||||||
"""Mixin for CollectionItem view"""
|
"""Mixin for CollectionItem view"""
|
||||||
|
|
@ -32,9 +36,8 @@ class CollectionListView(CollectionViewMixin, generics.ListAPIView):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Override get_queryset method"""
|
"""Override get_queryset method"""
|
||||||
country_id = self.request.query_params.get('country_id')
|
|
||||||
return models.Collection.objects.published()\
|
return models.Collection.objects.published()\
|
||||||
.by_country(country=country_id)
|
.by_country_code(code=self.get_country_code())
|
||||||
|
|
||||||
|
|
||||||
class CollectionRetrieveView(CollectionViewMixin, generics.RetrieveAPIView):
|
class CollectionRetrieveView(CollectionViewMixin, generics.RetrieveAPIView):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user