fix similarity rules

This commit is contained in:
Anatoly 2020-01-15 17:50:24 +03:00
parent 59f7f8d2d4
commit a468f264e0
2 changed files with 16 additions and 12 deletions

View File

@ -296,7 +296,7 @@ class EstablishmentQuerySet(models.QuerySet):
.order_by('mark_similarity') \
.distinct('mark_similarity', 'id')
def same_subtype(self, establishment):
def annotate_same_subtype(self, establishment):
"""Annotate flag same subtype."""
return self.annotate(same_subtype=Case(
models.When(
@ -312,10 +312,10 @@ class EstablishmentQuerySet(models.QuerySet):
Return QuerySet with objects that similar to Artisan/Producer(s).
:param establishment: Establishment instance
"""
base_qs = self.similar_base(establishment).same_subtype(establishment)
base_qs = self.similar_base(establishment).annotate_same_subtype(establishment)
similarity_rules = {
'ordering': [F('same_subtype').desc(), ],
'distinctions': ['same_subtype', ]
'ordering': [F('annotate_same_subtype').desc(), ],
'distinctions': ['annotate_same_subtype', ]
}
if establishment.address and establishment.address.coordinates:
base_qs = base_qs.annotate_distance(point=establishment.location)
@ -365,10 +365,12 @@ class EstablishmentQuerySet(models.QuerySet):
Return QuerySet with objects that similar to Distillery.
:param distillery: Establishment instance
"""
base_qs = self.similar_base(distillery).same_subtype(distillery)
base_qs = self.similar_base(distillery).annotate_same_subtype(distillery).filter(
same_subtype=True
)
similarity_rules = {
'ordering': [F('same_subtype').desc(), ],
'distinctions': ['same_subtype', ]
'ordering': [],
'distinctions': []
}
if distillery.address and distillery.address.coordinates:
base_qs = base_qs.annotate_distance(point=distillery.location)
@ -385,10 +387,12 @@ class EstablishmentQuerySet(models.QuerySet):
Return QuerySet with objects that similar to Food Producer.
:param food_producer: Establishment instance
"""
base_qs = self.similar_base(food_producer).same_subtype(food_producer)
base_qs = self.similar_base(food_producer).annotate_same_subtype(food_producer).filter(
same_subtype=True
)
similarity_rules = {
'ordering': [F('same_subtype').desc(), ],
'distinctions': ['same_subtype', ]
'ordering': [],
'distinctions': []
}
if food_producer.address and food_producer.address.coordinates:
base_qs = base_qs.annotate_distance(point=food_producer.location)

View File

@ -183,7 +183,7 @@ class ProductQuerySet(models.QuerySet):
"""Return objects with geo location."""
return self.filter(establishment__address__coordinates__isnull=False)
def same_subtype(self, product):
def annotate_same_subtype(self, product):
"""Annotate flag same subtype."""
return self.annotate(same_subtype=Case(
models.When(
@ -222,7 +222,7 @@ class ProductQuerySet(models.QuerySet):
similarity_rules['ordering'].append(F('distance').asc())
similarity_rules['distinction'].append('distance')
return self.similar_base(product) \
.same_subtype(product) \
.annotate_same_subtype(product) \
.order_by(*similarity_rules['ordering']) \
.distinct(*similarity_rules['distinction'],
'id')