From 4eefed90c917b916e7cfe2555940fc25e91ef7e5 Mon Sep 17 00:00:00 2001 From: Diego Castro Date: Mon, 15 Aug 2022 20:42:13 +0200 Subject: [PATCH] [#305]: Added a default callable to `get_callback_function` `get_callback_function()` gets a default callback as an arg returning explicitely an empty dict. `get_callback_function()` raises an `ImproperlyConfigured` exception when the callback is not callable. --- Resolves celery/django-celery-results#305 Fixes celery/django-celery-results#314 --- django_celery_results/backends/database.py | 2 ++ django_celery_results/settings.py | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/django_celery_results/backends/database.py b/django_celery_results/backends/database.py index 1d5db1e7..bb921500 100644 --- a/django_celery_results/backends/database.py +++ b/django_celery_results/backends/database.py @@ -129,6 +129,8 @@ def _store_result( self._get_extended_properties(request, traceback) ) + # TODO: Wrap this and make some sanity checks to complain the Mapping + # protocol. task_props.update( extend_task_props_callback(request, dict(task_props))) diff --git a/django_celery_results/settings.py b/django_celery_results/settings.py index 5f4eb61d..a9889a13 100644 --- a/django_celery_results/settings.py +++ b/django_celery_results/settings.py @@ -1,16 +1,19 @@ from django.conf import settings +from django.core.exceptions import ImproperlyConfigured def get_callback_function(settings_name, default=None): """Return the callback function for the given settings name.""" callback = getattr(settings, settings_name, None) - if callback is None: + if not callback: return default - if callable(callback): - return callback + if not callable(callback): + raise ImproperlyConfigured(f"{settings_name} must be callable.") + + return callback extend_task_props_callback = get_callback_function( - "CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK" + "CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK", dict ) \ No newline at end of file