forked from celery/django-celery-results
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[celery#305]: Improving abstract models implementation.
Added a `helpers` module into `models` containing the functions `taskresult_model()` and `groupresult_model()`. * `taskresult_model()`: will try to find the custom model using a dotted path defined under the constant `CELERY_RESULTS_TASKRESULT_MODEL` in the settings of the user's project * `groupresult_model()` will try to do the same using under the constant `CELERY_RESULTS_GROUPRESULT_MODEL`. By default if these attributes are not found `django-celery-results` will use the default models (`models.TaskResult` and `models.GroupResult`). Updated database backend in order to use custom models for `TaskResult and `GroupResult` it they're present. Instead to import explicitely the `TaskResult` and the `GroupResult` (default models from `django-celery-results`) we make use of the model helpers to load the right classes, the custom ones if they're present otherwise we use the default ones. Getting data from `task_kwargs` to extend the `task_properties` and be able to store them into the database (using the custom models). First of all we need a way to get data from `task_kwargs` (or somewhere else) just before a `task_result` record is created, evaluate that data and find the right values that will be used to fill the new fields defined in the custom model. So for this purpose we added a settings module to `django-celery-results` which will hold default settings, the first setting that will contain is a function in charge to get a callback from the settings of the user project. This callback will be feeded by the task `task_kwargs`, which will be intercepted in `DatabaseBackend._get_extended_properties` just before the `task_kwargs` are encoded by `encode_content()` method and send it to the `store_result` method from the object manager of `TaskModel` (Custom/Default one). To end, we must to extend the arguments of the `store_result` method from the `TaskResult` Manager adding `**extra_fields` that will make us able to send extra data to the custom model, when it's defined. --- Resolves celery#305 Fixes celery#314
- Loading branch information
1 parent
aba39e3
commit 16be793
Showing
8 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ cover/ | |
.cache/ | ||
htmlcov/ | ||
coverage.xml | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.apps import apps | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from .generic import TaskResult, GroupResult | ||
|
||
def taskresult_model(): | ||
"""Return the TaskResult model that is active in this project.""" | ||
if not hasattr(settings, 'CELERY_RESULTS_TASKRESULT_MODEL'): | ||
return TaskResult | ||
|
||
try: | ||
return apps.get_model( | ||
settings.CELERY_RESULTS_TASKRESULT_MODEL | ||
) | ||
except ValueError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_TASKRESULT_MODEL must be of the form " | ||
"'app_label.model_name'" | ||
) | ||
except LookupError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_TASKRESULT_MODEL refers to model " | ||
f"'{settings.CELERY_RESULTS_TASKRESULT_MODEL}' that has not " | ||
"been installed" | ||
) | ||
|
||
def groupresult_model(): | ||
"""Return the GroupResult model that is active in this project.""" | ||
if not hasattr(settings, 'CELERY_RESULTS_GROUPRESULT_MODEL'): | ||
return GroupResult | ||
|
||
try: | ||
return apps.get_model( | ||
settings.CELERY_RESULTS_GROUPRESULT_MODEL | ||
) | ||
except ValueError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_GROUPRESULT_MODEL must be of the form " | ||
"'app_label.model_name'" | ||
) | ||
except LookupError: | ||
raise ImproperlyConfigured( | ||
"CELERY_RESULTS_GROUPRESULT_MODEL refers to model " | ||
f"'{settings.CELERY_RESULTS_GROUPRESULT_MODEL}' that has not " | ||
"been installed" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.conf import settings | ||
|
||
|
||
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: | ||
return default | ||
|
||
if callable(callback): | ||
return callback | ||
|
||
extend_task_props_callback = get_callback_function( | ||
"CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK" | ||
) |