From ea6ce432d359cb6cf94c90f86839917b6683cb60 Mon Sep 17 00:00:00 2001 From: Erick Otenyo Date: Fri, 14 Jun 2024 10:28:28 +0300 Subject: [PATCH 1/2] Add after_generate_forecast, after_clear_old_forecasts and after_forecast_add_from_form hooks --- forecastmanager/forms.py | 5 +++++ .../management/commands/clear_old_forecasts.py | 4 ++++ forecastmanager/management/commands/generate_forecast.py | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/forecastmanager/forms.py b/forecastmanager/forms.py index 2592056..b2ea40c 100644 --- a/forecastmanager/forms.py +++ b/forecastmanager/forms.py @@ -1,6 +1,7 @@ from django import forms from django.db.models import Q from django.utils.translation import gettext_lazy as _ +from wagtail import hooks from wagtail.admin.forms import WagtailAdminModelForm from forecastmanager.forecast_settings import ( @@ -134,6 +135,10 @@ def save(self, commit=True): if commit: forecast.save() + + for fn in hooks.get_hooks("after_forecast_add_from_form"): + fn() + return forecast diff --git a/forecastmanager/management/commands/clear_old_forecasts.py b/forecastmanager/management/commands/clear_old_forecasts.py index fb5dd1a..65addba 100644 --- a/forecastmanager/management/commands/clear_old_forecasts.py +++ b/forecastmanager/management/commands/clear_old_forecasts.py @@ -2,6 +2,7 @@ from django.core.management.base import BaseCommand from django.utils import timezone +from wagtail import hooks from forecastmanager.models import Forecast @@ -28,3 +29,6 @@ def handle(self, *args, **options): old_forecasts.delete() logger.info("Old forecasts deleted successfully.") + + for fn in hooks.get_hooks("after_clear_old_forecasts"): + fn() diff --git a/forecastmanager/management/commands/generate_forecast.py b/forecastmanager/management/commands/generate_forecast.py index 23b8692..ac59a7b 100644 --- a/forecastmanager/management/commands/generate_forecast.py +++ b/forecastmanager/management/commands/generate_forecast.py @@ -4,6 +4,7 @@ from dateutil.parser import parse from django.core.management.base import BaseCommand from django.utils import timezone +from wagtail import hooks from wagtail.models import Site from forecastmanager.constants import WEATHER_CONDITIONS_AS_DICT @@ -188,6 +189,8 @@ def handle(self, *args, **options): else: cities_data[timezone_date] = [city_forecast] + created_forecast_pks = [] + # Create the forecast for the cities for forecast_date, city_forecasts in cities_data.items(): effective_time = f"{forecast_date.hour}:00" @@ -208,3 +211,9 @@ def handle(self, *args, **options): forecast.city_forecasts.add(city_forecast) forecast.save() + + # Add the forecast to the list of created forecasts + created_forecast_pks.append(forecast.pk) + + for fn in hooks.get_hooks("after_generate_forecast"): + fn(created_forecast_pks) From daf3bf5092bc06dc219ca4460cda6bd708d84d8c Mon Sep 17 00:00:00 2001 From: Erick Otenyo Date: Fri, 14 Jun 2024 10:28:46 +0300 Subject: [PATCH 2/2] Bump version --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 7bc863d..29ee045 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = forecastmanager -version = 0.5.0 +version = 0.5.1 description = Integration of Weather City Forecasts Manager in Wagtail Projects. long_description = file:README.md long_description_content_type = text/markdown