Merge branch 'feature/scheduled-establishment-reindexing' into 'develop'

Feature/scheduled establishment reindexing

See merge request gm/gm-backend!73
This commit is contained in:
d.kuzmenko 2019-10-25 12:16:00 +00:00
commit 0015e16959
5 changed files with 27 additions and 4 deletions

View File

@ -120,6 +120,11 @@ class EstablishmentQuerySet(models.QuerySet):
def with_type_related(self):
return self.prefetch_related('establishment_subtypes')
def with_es_related(self):
"""Return qs with related for ES indexing objects."""
return self.select_related('address', 'establishment_type', 'address__city', 'address__city__country').\
prefetch_related('tags', 'schedule')
def search(self, value, locale=None):
"""Search text in JSON fields."""
if locale is not None:

View File

@ -1,10 +1,15 @@
"""Establishment app tasks."""
import logging
from celery import shared_task
from celery.schedules import crontab
from celery.task import periodic_task
from django.core import management
from django_elasticsearch_dsl.management.commands import search_index
from establishment import models
from location.models import Country
logger = logging.getLogger(__name__)
@ -12,10 +17,15 @@ logger = logging.getLogger(__name__)
def recalculate_price_levels_by_country(country_id):
try:
country = Country.objects.get(pk=country_id)
except Country.DoesNotExist as ex:
except Country.DoesNotExist as _:
logger.error(f'ESTABLISHMENT. Country does not exist. ID {country_id}')
else:
qs = models.Establishment.objects.filter(address__city__country=country)
for establishment in qs:
establishment.recalculate_price_level(low_price=country.low_price,
high_price=country.high_price)
@periodic_task(run_every=crontab(minute=59))
def rebuild_establishment_indices():
management.call_command(search_index.Command(), action='rebuild', models=[models.Establishment.__name__],
force=True)

View File

@ -106,4 +106,4 @@ class EstablishmentDocument(Document):
)
def get_queryset(self):
return super().get_queryset().published()
return super().get_queryset().published().with_es_related()

View File

@ -9,6 +9,7 @@ from account.models import User
from news.models import News, NewsType
from establishment.models import Establishment, EstablishmentType, Employee
from location.models import Country
class BaseTestCase(APITestCase):
@ -39,7 +40,13 @@ class TranslateFieldTests(BaseTestCase):
self.news_type = NewsType.objects.create(name="Test news type")
self.news_type.save()
self.country_ru = Country.objects.get(
name={"en-GB": "Russian"}
)
self.news_item = News.objects.create(
id=8,
created_by=self.user,
modified_by=self.user,
title={
@ -52,6 +59,7 @@ class TranslateFieldTests(BaseTestCase):
news_type=self.news_type,
slug='test',
state=News.PUBLISHED,
country=self.country_ru,
)
self.news_item.save()

View File

@ -98,7 +98,7 @@ EXTERNAL_APPS = [
'timezone_field',
'storages',
'sorl.thumbnail',
'timezonefinder'
'timezonefinder',
]