Recalculate toque number fallback

This commit is contained in:
evgeniy-st 2019-11-12 18:15:53 +03:00
parent 0833d4057f
commit 0dfa1c85d5
2 changed files with 77 additions and 0 deletions

View File

@ -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)

View File

@ -1,6 +1,7 @@
"""Establishment models."""
from datetime import datetime
from functools import reduce
from operator import or_
import elasticsearch_dsl
from django.conf import settings
@ -159,6 +160,10 @@ class EstablishmentQuerySet(models.QuerySet):
ids = [result.meta.id for result in search]
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):
"""Return establishments by country code"""
return self.filter(address__city__country__code=code)
@ -308,6 +313,20 @@ class EstablishmentQuerySet(models.QuerySet):
"""Return QuerySet with subtype by 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):
"""Establishment model."""
@ -770,6 +789,10 @@ class RatingStrategyQuerySet(models.QuerySet):
"""Filter by 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):
"""Filter for value."""
return self.filter(public_mark_min_value__lte=public_mark,