"""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__) @shared_task def recalculate_price_levels_by_country(country_id): try: country = Country.objects.get(pk=country_id) 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)