Merge branch 'feature/collections-update-recomendations' into 'develop'

Feature/collections update recomendations

See merge request gm/gm-backend!36
This commit is contained in:
d.kuzmenko 2019-09-27 14:43:43 +00:00
commit 8957a52fe7
5 changed files with 89 additions and 6 deletions

View File

@ -8,6 +8,8 @@ from django.utils.translation import gettext_lazy as _
from utils.models import ProjectBaseMixin, URLImageMixin
from utils.models import TranslatedFieldsMixin
from utils.querysets import RelatedObjectsCountMixin
# Mixins
class CollectionNameMixin(models.Model):
@ -30,7 +32,7 @@ class CollectionDateMixin(models.Model):
# Models
class CollectionQuerySet(models.QuerySet):
class CollectionQuerySet(RelatedObjectsCountMixin):
"""QuerySet for model Collection"""
def by_country_code(self, code):

View File

@ -8,6 +8,7 @@ from http.cookies import SimpleCookie
from collection.models import Collection, Guide
from location.models import Country
from establishment.models import Establishment, EstablishmentType
# Create your tests here.
@ -81,3 +82,27 @@ class CollectionGuideDetailTests(CollectionDetailTests):
def test_guide_detail_Read(self):
response = self.client.get(f'/api/web/collections/guides/{self.guide.id}/', format='json')
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")
for i in range(1, 5):
setattr(self, f"establishment{i}",
Establishment.objects.create(
name=f"Test establishment {i}",
establishment_type_id=self.establishment_type.id,
is_publish=True,
slug=f"test-establishment-{i}"
)
)
getattr(self, f"establishment{i}").collections.add(self.collection)
def test_collection_list_filter(self):
response = self.client.get('/api/web/collections/?country_code=en', format='json')
data = response.json()
self.assertIn('count', data)
self.assertGreater(data['count'], 0)

View File

@ -6,7 +6,7 @@ from collection.views import common as views
app_name = 'collection'
urlpatterns = [
path('', views.CollectionListView.as_view(), name='list'),
path('', views.CollectionHomePageView.as_view(), name='list'),
path('<slug:slug>/establishments/', views.CollectionEstablishmentListView.as_view(),
name='detail'),

View File

@ -30,9 +30,26 @@ class CollectionListView(CollectionViewMixin, generics.ListAPIView):
def get_queryset(self):
"""Override get_queryset method"""
return models.Collection.objects.published()\
.by_country_code(code=self.request.country_code)\
.order_by('-on_top', '-created')
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):

View File

@ -1,6 +1,8 @@
"""Utils QuerySet Mixins"""
from django.db import models
from django.db.models import Q, Sum, F
from functools import reduce
from operator import add
from utils.methods import get_contenttype
@ -15,3 +17,40 @@ class ContentTypeQuerySetMixin(models.QuerySet):
"""Filter QuerySet by ContentType."""
return self.filter(content_type=get_contenttype(app_label=app_label,
model=model))
class RelatedObjectsCountMixin(models.QuerySet):
"""QuerySet for ManyToMany related count filter"""
def _get_related_objects_names(self):
"""Get all related objects (with reversed)"""
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):
"""Annotate all related objects to queryset"""
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):
"""QuerySet filter by related objects 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._annotate_related_objects_count().filter(q_objects)
def filter_all_related_gt(self, count):
"""Queryset filter by all related objects count"""
exp =reduce(add, [F(f"{related_object}_count") for related_object in self._get_related_objects_names()])
return self._annotate_related_objects_count()\
.annotate(all_related_count=exp)\
.filter(all_related_count__gt=count)