Merge branch 'develop' of /home/a.feteleu/projects/gm-backend with conflicts.
This commit is contained in:
parent
77d39954c3
commit
0e643a1dfa
|
|
@ -101,70 +101,16 @@ class EstablishmentQuerySet(models.QuerySet):
|
|||
DistanceMeasure(Distance('address__coordinates', point, srid=4236)).m,
|
||||
output_field=models.FloatField()))
|
||||
|
||||
def annotate_distance_mark(self):
|
||||
def annotate_mark_similarity(self, mark):
|
||||
"""
|
||||
Return QuerySet with annotated field - distance_mark.
|
||||
Required fields: distance.
|
||||
Return a QuerySet with annotated field - mark_similarity
|
||||
Description:
|
||||
If the radius of the establishments in QuerySet does not exceed 500 meters,
|
||||
then distance_mark is set to 0.6, otherwise 0.
|
||||
Similarity mark determined by comparison with compared establishment mark
|
||||
"""
|
||||
return self.annotate(distance_mark=models.Case(
|
||||
models.When(distance__lte=500,
|
||||
then=0.6),
|
||||
default=0,
|
||||
output_field=models.FloatField()))
|
||||
|
||||
def annotate_intermediate_public_mark(self):
|
||||
"""
|
||||
Return QuerySet with annotated field - intermediate_public_mark.
|
||||
Description:
|
||||
If establishments in collection POP and its mark is null, then
|
||||
intermediate_mark is set to 10;
|
||||
"""
|
||||
return self.annotate(intermediate_public_mark=models.Case(
|
||||
models.When(
|
||||
collections__collection_type=Collection.POP,
|
||||
public_mark__isnull=True,
|
||||
then=10
|
||||
),
|
||||
default='public_mark',
|
||||
output_field=models.FloatField()))
|
||||
|
||||
def annotate_additional_mark(self, public_mark: float):
|
||||
"""
|
||||
Return QuerySet with annotated field - additional_mark.
|
||||
Required fields: intermediate_public_mark
|
||||
Description:
|
||||
IF
|
||||
establishments public_mark + 3 > compared establishment public_mark
|
||||
OR
|
||||
establishments public_mark - 3 > compared establishment public_mark,
|
||||
THEN
|
||||
additional_mark is set to 0.4,
|
||||
ELSE
|
||||
set to 0.
|
||||
"""
|
||||
return self.annotate(additional_mark=models.Case(
|
||||
models.When(
|
||||
models.Q(intermediate_public_mark__lte=public_mark + 3) |
|
||||
models.Q(intermediate_public_mark__lte=public_mark - 3),
|
||||
then=0.4),
|
||||
default=0,
|
||||
output_field=models.FloatField()))
|
||||
|
||||
def annotate_total_mark(self):
|
||||
"""
|
||||
Return QuerySet with annotated field - total_mark.
|
||||
Required fields: distance_mark, additional_mark.
|
||||
Fields
|
||||
Description:
|
||||
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')) *
|
||||
models.F('intermediate_public_mark'))
|
||||
return self.annotate(mark_similarity=models.ExpressionWrapper(
|
||||
mark - models.F('mark'),
|
||||
output_field=models.FloatField()
|
||||
))
|
||||
|
||||
def similar(self, establishment_slug: str):
|
||||
"""
|
||||
|
|
@ -181,10 +127,11 @@ class EstablishmentQuerySet(models.QuerySet):
|
|||
reviews__status=Review.READY,
|
||||
public_mark__gte=10) \
|
||||
.annotate_distance(point=establishment.address.coordinates) \
|
||||
.annotate_distance_mark() \
|
||||
.annotate_intermediate_public_mark() \
|
||||
.annotate_additional_mark(public_mark=establishment.public_mark) \
|
||||
.annotate_total_mark()
|
||||
.annotate_mark_similarity(establishment_qs.first().public_mark)
|
||||
# .annotate_distance_mark() \
|
||||
# .annotate_intermediate_public_mark() \
|
||||
# .annotate_additional_mark(public_mark=establishment.public_mark) \
|
||||
# .annotate_total_mark()
|
||||
else:
|
||||
return self.none()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from rest_framework import generics
|
||||
|
||||
from utils.permissions import IsAuthenticatedAndTokenIsValid
|
||||
from . import models, serializers
|
||||
|
||||
|
||||
|
|
@ -9,4 +8,3 @@ class ImageUploadView(generics.CreateAPIView):
|
|||
model = models.Image
|
||||
queryset = models.Image.objects.all()
|
||||
serializer_class = serializers.ImageSerializer
|
||||
permission_classes = (IsAuthenticatedAndTokenIsValid, )
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user