Add queryset mixin
Switch view in urls/common Add homepage view Switch test
This commit is contained in:
parent
8766388256
commit
5793eaad32
|
|
@ -8,6 +8,8 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from utils.models import ProjectBaseMixin, URLImageMixin
|
from utils.models import ProjectBaseMixin, URLImageMixin
|
||||||
from utils.models import TranslatedFieldsMixin
|
from utils.models import TranslatedFieldsMixin
|
||||||
|
|
||||||
|
from utils.querysets import RelatedObjectsCountMixin
|
||||||
|
|
||||||
|
|
||||||
# Mixins
|
# Mixins
|
||||||
class CollectionNameMixin(models.Model):
|
class CollectionNameMixin(models.Model):
|
||||||
|
|
@ -30,7 +32,7 @@ class CollectionDateMixin(models.Model):
|
||||||
|
|
||||||
|
|
||||||
# Models
|
# Models
|
||||||
class CollectionQuerySet(models.QuerySet):
|
class CollectionQuerySet(RelatedObjectsCountMixin):
|
||||||
"""QuerySet for model Collection"""
|
"""QuerySet for model Collection"""
|
||||||
|
|
||||||
def by_country_code(self, code):
|
def by_country_code(self, code):
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ from http.cookies import SimpleCookie
|
||||||
from collection.models import Collection, Guide
|
from collection.models import Collection, Guide
|
||||||
from location.models import Country
|
from location.models import Country
|
||||||
|
|
||||||
|
from establishment.models import Establishment, EstablishmentType
|
||||||
# Create your tests here.
|
# Create your tests here.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -81,3 +82,24 @@ class CollectionGuideDetailTests(CollectionDetailTests):
|
||||||
def test_guide_detail_Read(self):
|
def test_guide_detail_Read(self):
|
||||||
response = self.client.get(f'/api/web/collections/guides/{self.guide.id}/', format='json')
|
response = self.client.get(f'/api/web/collections/guides/{self.guide.id}/', format='json')
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
class CollectionWebHomeTests(CollectionDetailTests):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.establishment_type = EstablishmentType.objects.create(name="Test establishment type")
|
||||||
|
|
||||||
|
self.establishment = Establishment.objects.create(
|
||||||
|
name="Test establishment",
|
||||||
|
establishment_type_id=self.establishment_type.id,
|
||||||
|
is_publish=True,
|
||||||
|
slug="test"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.establishment.collections.add(self.collection)
|
||||||
|
self.establishment.save()
|
||||||
|
|
||||||
|
def test_collection_list_filter(self):
|
||||||
|
print("========================================================================================")
|
||||||
|
response = self.client.get('/api/web/collections/', format='json')
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from collection.views import common as views
|
||||||
app_name = 'collection'
|
app_name = 'collection'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.CollectionListView.as_view(), name='list'),
|
path('', views.CollectionHomePageView.as_view(), name='list'),
|
||||||
path('<slug:slug>/establishments/', views.CollectionEstablishmentListView.as_view(),
|
path('<slug:slug>/establishments/', views.CollectionEstablishmentListView.as_view(),
|
||||||
name='detail'),
|
name='detail'),
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,28 @@ class CollectionListView(CollectionViewMixin, generics.ListAPIView):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
"""Override get_queryset method"""
|
"""Override get_queryset method"""
|
||||||
return models.Collection.objects.published()\
|
queryset = models.Collection.objects.published()\
|
||||||
.by_country_code(code=self.request.country_code)\
|
.by_country_code(code=self.request.country_code)\
|
||||||
.order_by('-on_top', '-created')
|
.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)\
|
||||||
|
.annotate_related_objects_count() \
|
||||||
|
.filter_related_gt(3) \
|
||||||
|
.order_by('-on_top', '-created')
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
class CollectionEstablishmentListView(CollectionListView):
|
class CollectionEstablishmentListView(CollectionListView):
|
||||||
"""Retrieve list of establishment for collection."""
|
"""Retrieve list of establishment for collection."""
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
"""Utils QuerySet Mixins"""
|
"""Utils QuerySet Mixins"""
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
from utils.methods import get_contenttype
|
from utils.methods import get_contenttype
|
||||||
|
|
||||||
|
|
@ -15,3 +16,31 @@ class ContentTypeQuerySetMixin(models.QuerySet):
|
||||||
"""Filter QuerySet by ContentType."""
|
"""Filter QuerySet by ContentType."""
|
||||||
return self.filter(content_type=get_contenttype(app_label=app_label,
|
return self.filter(content_type=get_contenttype(app_label=app_label,
|
||||||
model=model))
|
model=model))
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedObjectsCountMixin(models.QuerySet):
|
||||||
|
"""QuerySet for ManyToMany related count filter"""
|
||||||
|
|
||||||
|
def get_related_objects_names(self):
|
||||||
|
related_objects_names = []
|
||||||
|
|
||||||
|
for related_object in self.model._meta.related_objects:
|
||||||
|
if isinstance(related_object, models.ManyToManyRel):
|
||||||
|
related_objects_names.append(related_object.name)
|
||||||
|
|
||||||
|
return related_objects_names
|
||||||
|
|
||||||
|
def annotate_related_objects_count(self):
|
||||||
|
|
||||||
|
annotations = {}
|
||||||
|
for related_object in self.get_related_objects_names():
|
||||||
|
annotations[f"{related_object}_count"] = models.Count(f"{related_object}")
|
||||||
|
|
||||||
|
return self.annotate(**annotations)
|
||||||
|
|
||||||
|
def filter_related_gt(self, count):
|
||||||
|
q_objects = Q()
|
||||||
|
for related_object in self.get_related_objects_names():
|
||||||
|
q_objects.add(Q(**{f"{related_object}_count__gt": count}), Q.OR)
|
||||||
|
|
||||||
|
return self.filter(q_objects)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user