Establishment each hour reindexing
This commit is contained in:
parent
9129d44708
commit
7f23f0e891
|
|
@ -120,6 +120,11 @@ class EstablishmentQuerySet(models.QuerySet):
|
||||||
def with_type_related(self):
|
def with_type_related(self):
|
||||||
return self.prefetch_related('establishment_subtypes')
|
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):
|
def search(self, value, locale=None):
|
||||||
"""Search text in JSON fields."""
|
"""Search text in JSON fields."""
|
||||||
if locale is not None:
|
if locale is not None:
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
"""Establishment app tasks."""
|
"""Establishment app tasks."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
|
from django.core import management
|
||||||
|
from django_elasticsearch_dsl.management.commands import search_index
|
||||||
|
|
||||||
from establishment import models
|
from establishment import models
|
||||||
from location.models import Country
|
from location.models import Country
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -12,10 +15,15 @@ logger = logging.getLogger(__name__)
|
||||||
def recalculate_price_levels_by_country(country_id):
|
def recalculate_price_levels_by_country(country_id):
|
||||||
try:
|
try:
|
||||||
country = Country.objects.get(pk=country_id)
|
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}')
|
logger.error(f'ESTABLISHMENT. Country does not exist. ID {country_id}')
|
||||||
else:
|
else:
|
||||||
qs = models.Establishment.objects.filter(address__city__country=country)
|
qs = models.Establishment.objects.filter(address__city__country=country)
|
||||||
for establishment in qs:
|
for establishment in qs:
|
||||||
establishment.recalculate_price_level(low_price=country.low_price,
|
establishment.recalculate_price_level(low_price=country.low_price,
|
||||||
high_price=country.high_price)
|
high_price=country.high_price)
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def rebuild_establishment_indices():
|
||||||
|
management.call_command(search_index.Command(), action='rebuild', models=[models.Establishment.__name__],
|
||||||
|
force=True)
|
||||||
|
|
|
||||||
|
|
@ -106,4 +106,4 @@ class EstablishmentDocument(Document):
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return super().get_queryset().published()
|
return super().get_queryset().published().with_es_related()
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from celery.schedules import crontab
|
||||||
|
|
||||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
@ -98,7 +99,8 @@ EXTERNAL_APPS = [
|
||||||
'timezone_field',
|
'timezone_field',
|
||||||
'storages',
|
'storages',
|
||||||
'sorl.thumbnail',
|
'sorl.thumbnail',
|
||||||
'timezonefinder'
|
'timezonefinder',
|
||||||
|
'django_celery_beat',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -324,6 +326,12 @@ CELERY_ACCEPT_CONTENT = ['application/json']
|
||||||
CELERY_TASK_SERIALIZER = 'json'
|
CELERY_TASK_SERIALIZER = 'json'
|
||||||
CELERY_RESULT_SERIALIZER = 'json'
|
CELERY_RESULT_SERIALIZER = 'json'
|
||||||
CELERY_TIMEZONE = TIME_ZONE
|
CELERY_TIMEZONE = TIME_ZONE
|
||||||
|
CELERY_BEAT_SCHEDULE = {
|
||||||
|
'send-summary-every-hour': {
|
||||||
|
'task': 'establishment.tasks.rebuild_establishment_indices',
|
||||||
|
'schedule': crontab(hour=1),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
# Django FCM (Firebase push notifications)
|
# Django FCM (Firebase push notifications)
|
||||||
FCM_DJANGO_SETTINGS = {
|
FCM_DJANGO_SETTINGS = {
|
||||||
|
|
|
||||||
|
|
@ -44,4 +44,5 @@ sorl-thumbnail==12.5.0
|
||||||
# temp solution
|
# temp solution
|
||||||
redis==3.2.0
|
redis==3.2.0
|
||||||
amqp>=2.4.0
|
amqp>=2.4.0
|
||||||
celery==4.3.0rc2
|
celery==4.3.0rc2
|
||||||
|
django-celery-beat==1.5.0
|
||||||
Loading…
Reference in New Issue
Block a user