From b05a0bd502246b08d76baf1327c814687ea84aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 11 Nov 2024 00:38:23 +0100 Subject: [PATCH] Improve startup setup and tasks --- games/apps.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/games/apps.py b/games/apps.py index cebaa41..07fcf2f 100644 --- a/games/apps.py +++ b/games/apps.py @@ -1,6 +1,8 @@ from datetime import timedelta from django.apps import AppConfig +from django.core.management import call_command +from django.db.models.signals import post_migrate from django.utils.timezone import now @@ -9,13 +11,23 @@ class GamesConfig(AppConfig): name = "games" def ready(self): - from django_q.models import Schedule - from django_q.tasks import schedule + post_migrate.connect(schedule_tasks, sender=self) - if not Schedule.objects.filter(name="Update converted prices").exists(): - schedule( - "games.convert_price.update_converted_prices", - name="Update converted prices", - schedule_type=Schedule.HOURLY, - next_run=now() + timedelta(seconds=30), - ) + +def schedule_tasks(sender, **kwargs): + from django_q.models import Schedule + from django_q.tasks import schedule + + if not Schedule.objects.filter(name="Update converted prices").exists(): + schedule( + "games.convert_price.update_converted_prices", + name="Update converted prices", + schedule_type=Schedule.HOURLY, + next_run=now() + timedelta(seconds=30), + ) + + from games.models import ExchangeRate + + if not ExchangeRate.objects.exists(): + print("ExchangeRate table is empty. Loading fixture...") + call_command("loaddata", "exchangerates.yaml")