From 69f1a5cf481816a55535493a56b2c4e296ac7878 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 17 Sep 2019 17:16:21 +0300 Subject: [PATCH] GM-73 added ordering --- apps/establishment/models.py | 10 ++++++---- apps/establishment/views/web.py | 6 +++--- project/settings/development.py | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 84fc3e02..c07de769 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -129,7 +129,7 @@ class EstablishmentQuerySet(models.QuerySet): then=10 ), default='public_mark', - output_field=models.PositiveSmallIntegerField())) + output_field=models.FloatField())) def annotate_additional_mark(self, public_mark: float): """ @@ -159,10 +159,12 @@ class EstablishmentQuerySet(models.QuerySet): Required fields: distance_mark, additional_mark. Fields Description: - Annotated field is obtained by adding the distance and additional marks. + Annotated field is obtained by formula: + (distance + additional marks) * intermediate_public_mark. """ - return self.annotate(total_mark=models.F('distance_mark') + - models.F('additional_mark')) + return self.annotate( + total_mark=(models.F('distance_mark') + models.F('additional_mark')) * + models.F('intermediate_public_mark')) def similar(self, establishment_pk: int): """ diff --git a/apps/establishment/views/web.py b/apps/establishment/views/web.py index b4b4b587..cc0d1c56 100644 --- a/apps/establishment/views/web.py +++ b/apps/establishment/views/web.py @@ -11,7 +11,7 @@ from utils.views import JWTGenericViewMixin from establishment.views import EstablishmentMixin -class EstablishmentListView(EstablishmentMixin, JWTGenericViewMixin, generics.ListAPIView): +class EstablishmentListView(EstablishmentMixin, generics.ListAPIView): """Resource for getting a list of establishments.""" serializer_class = serializers.EstablishmentListSerializer filter_class = filters.EstablishmentFilter @@ -29,9 +29,9 @@ class EstablishmentSimilarListView(EstablishmentListView): def get_queryset(self): """Override get_queryset method""" - qs = super(EstablishmentListView, self).get_queryset() + qs = super().get_queryset() return qs.similar(establishment_pk=self.kwargs.get('pk'))\ - .order_by('?')[:13] + .order_by('-total_mark')[:13] class EstablishmentRetrieveView(EstablishmentMixin, JWTGenericViewMixin, generics.RetrieveAPIView): diff --git a/project/settings/development.py b/project/settings/development.py index 7c47ff5b..a7c8d9fd 100644 --- a/project/settings/development.py +++ b/project/settings/development.py @@ -32,4 +32,4 @@ ELASTICSEARCH_INDEX_NAMES = { sentry_sdk.init( dsn="https://35d9bb789677410ab84a822831c6314f@sentry.io/1729093", integrations=[DjangoIntegration()] -) \ No newline at end of file +)