Recalculate toque number fallback
This commit is contained in:
parent
0833d4057f
commit
0dfa1c85d5
|
|
@ -0,0 +1,54 @@
|
||||||
|
"""Run recalculating toque number for establishments without indexing."""
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from establishment.models import Establishment, RatingStrategy
|
||||||
|
from location.models import Country
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
|
||||||
|
help = 'Recalculation toque number for all establishments without indexing.'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
restaurants = Establishment.objects.restaurants()
|
||||||
|
|
||||||
|
# update establishments with a specific rating strategy
|
||||||
|
strategies = RatingStrategy.objects.with_country()
|
||||||
|
for strategy in strategies:
|
||||||
|
qs = restaurants.by_country(strategy.country). \
|
||||||
|
by_public_mark_range(strategy.public_mark_min_value,
|
||||||
|
strategy.public_mark_max_value)
|
||||||
|
qs.update(toque_number=strategy.toque_number)
|
||||||
|
|
||||||
|
countries = Country.objects.filter(pk__in=strategies.values('country'))
|
||||||
|
|
||||||
|
for country in countries:
|
||||||
|
rating_strategies = RatingStrategy.objects.by_country(country)
|
||||||
|
qs = Establishment.objects.restaurants(). \
|
||||||
|
by_country(country). \
|
||||||
|
exclude_public_mark_ranges(
|
||||||
|
ranges=[(strategy.public_mark_min_value,
|
||||||
|
strategy.public_mark_max_value) for
|
||||||
|
strategy in rating_strategies])
|
||||||
|
qs.update(toque_number=0)
|
||||||
|
|
||||||
|
# update establishments for other countries
|
||||||
|
strategy_for_other_countries = RatingStrategy.objects.with_country(False)
|
||||||
|
for strategy in strategy_for_other_countries:
|
||||||
|
qs = Establishment.objects.restaurants(). \
|
||||||
|
exclude_countries(countries). \
|
||||||
|
by_public_mark_range(strategy.public_mark_min_value,
|
||||||
|
strategy.public_mark_max_value)
|
||||||
|
qs.update(toque_number=strategy.toque_number)
|
||||||
|
|
||||||
|
# set null for others
|
||||||
|
qs = Establishment.objects.restaurants(). \
|
||||||
|
exclude_countries(countries). \
|
||||||
|
exclude_public_mark_ranges(
|
||||||
|
ranges=[(strategy.public_mark_min_value,
|
||||||
|
strategy.public_mark_max_value)
|
||||||
|
for strategy in
|
||||||
|
strategy_for_other_countries])
|
||||||
|
qs.update(toque_number=0)
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"""Establishment models."""
|
"""Establishment models."""
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
from operator import or_
|
||||||
|
|
||||||
import elasticsearch_dsl
|
import elasticsearch_dsl
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
@ -159,6 +160,10 @@ class EstablishmentQuerySet(models.QuerySet):
|
||||||
ids = [result.meta.id for result in search]
|
ids = [result.meta.id for result in search]
|
||||||
return self.filter(id__in=ids)
|
return self.filter(id__in=ids)
|
||||||
|
|
||||||
|
def by_country(self, country):
|
||||||
|
"""Return establishments by country code"""
|
||||||
|
return self.filter(address__city__country=country)
|
||||||
|
|
||||||
def by_country_code(self, code):
|
def by_country_code(self, code):
|
||||||
"""Return establishments by country code"""
|
"""Return establishments by country code"""
|
||||||
return self.filter(address__city__country__code=code)
|
return self.filter(address__city__country__code=code)
|
||||||
|
|
@ -308,6 +313,20 @@ class EstablishmentQuerySet(models.QuerySet):
|
||||||
"""Return QuerySet with subtype by value."""
|
"""Return QuerySet with subtype by value."""
|
||||||
return self.filter(establishment_subtypes__index_name=value)
|
return self.filter(establishment_subtypes__index_name=value)
|
||||||
|
|
||||||
|
def by_public_mark_range(self, min_value, max_value):
|
||||||
|
"""Filter by public mark range."""
|
||||||
|
return self.filter(public_mark__gte=min_value, public_mark__lte=max_value)
|
||||||
|
|
||||||
|
def exclude_public_mark_ranges(self, ranges):
|
||||||
|
"""Exclude public mark ranges."""
|
||||||
|
return self.exclude(reduce(or_, [Q(public_mark__gte=r[0],
|
||||||
|
public_mark__lte=r[1])
|
||||||
|
for r in ranges]))
|
||||||
|
|
||||||
|
def exclude_countries(self, countries):
|
||||||
|
"""Exclude countries."""
|
||||||
|
return self.exclude(address__city__country__in=countries)
|
||||||
|
|
||||||
|
|
||||||
class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
|
class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
|
||||||
"""Establishment model."""
|
"""Establishment model."""
|
||||||
|
|
@ -770,6 +789,10 @@ class RatingStrategyQuerySet(models.QuerySet):
|
||||||
"""Filter by country."""
|
"""Filter by country."""
|
||||||
return self.filter(country=country)
|
return self.filter(country=country)
|
||||||
|
|
||||||
|
def with_country(self, switcher=True):
|
||||||
|
"""With country."""
|
||||||
|
return self.exclude(country__isnull=switcher)
|
||||||
|
|
||||||
def for_public_mark(self, public_mark):
|
def for_public_mark(self, public_mark):
|
||||||
"""Filter for value."""
|
"""Filter for value."""
|
||||||
return self.filter(public_mark_min_value__lte=public_mark,
|
return self.filter(public_mark_min_value__lte=public_mark,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user