-
Notifications
You must be signed in to change notification settings - Fork 221
resolve #305 abstract models #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9c9a1be
94bd57e
29528ff
e9548e9
fef0798
f57755e
a6ea7bd
2db6ba4
4468aaf
7317e7f
76987be
da75874
88b2f5e
de03da5
d254df7
3ef8f71
20ee880
0229118
21f1100
6390704
ae9a711
7f83d9d
7d037d6
d18961b
6095a3b
03d4ac8
641f70c
91fccb0
44de6be
3dc476b
5e0e37f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,4 +30,5 @@ cover/ | |
| htmlcov/ | ||
| coverage.xml | ||
| .env | ||
| *.ignore | ||
| *.ignore | ||
| .vscode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .generic import ChordCounter, GroupResult, TaskResult | ||
|
|
||
| __all__ = ["ChordCounter", "GroupResult", "TaskResult"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Database models.""" | ||
|
|
||
| from django_celery_results.models.abstract import ( | ||
| AbstractChordCounter, | ||
| AbstractGroupResult, | ||
| AbstractTaskResult, | ||
| ) | ||
|
|
||
|
|
||
| class TaskResult(AbstractTaskResult): | ||
| """Task result/status.""" | ||
|
|
||
| class Meta(AbstractTaskResult.Meta): | ||
| """Table information.""" | ||
|
|
||
| abstract = False | ||
| app_label = "django_celery_results" | ||
| swappable = "CELERY_RESULTS_TASKRESULT_MODEL" | ||
|
|
||
|
|
||
| class ChordCounter(AbstractChordCounter): | ||
| """Chord synchronisation.""" | ||
|
|
||
| class Meta(AbstractChordCounter.Meta): | ||
| """Table information.""" | ||
|
|
||
| abstract = False | ||
| app_label = "django_celery_results" | ||
diegocastrum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| swappable = "CELERY_RESULTS_CHORDCOUNTER_MODEL" | ||
|
|
||
|
|
||
| class GroupResult(AbstractGroupResult): | ||
| """Task Group result/status.""" | ||
|
|
||
| class Meta(AbstractGroupResult.Meta): | ||
| """Table information.""" | ||
|
|
||
| abstract = False | ||
| app_label = "django_celery_results" | ||
diegocastrum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| swappable = "CELERY_RESULTS_GROUPRESULT_MODEL" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from django.apps import apps | ||
| from django.conf import settings | ||
| from django.core.exceptions import ImproperlyConfigured | ||
|
|
||
| from .generic import ChordCounter, GroupResult, TaskResult | ||
|
|
||
|
|
||
| def get_task_result_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 get_chord_counter_model(): | ||
| """Return the ChordCounter model that is active in this project.""" | ||
|
|
||
| if not hasattr(settings, 'CELERY_RESULTS_CHORDCOUNTER_MODEL'): | ||
| return ChordCounter | ||
|
|
||
| try: | ||
| return apps.get_model( | ||
| settings.CELERY_RESULTS_CHORDCOUNTER_MODEL | ||
| ) | ||
| except ValueError: | ||
| raise ImproperlyConfigured( | ||
| "CELERY_RESULTS_CHORDCOUNTER_MODEL must be of the form " | ||
| "'app_label.model_name'" | ||
| ) | ||
| except LookupError: | ||
| raise ImproperlyConfigured( | ||
| "CELERY_RESULTS_CHORDCOUNTER_MODEL refers to model " | ||
| f"'{settings.CELERY_RESULTS_CHORDCOUNTER_MODEL}' that has not " | ||
| "been installed" | ||
| ) | ||
|
|
||
|
|
||
| def get_group_result_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" | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from collections.abc import Mapping | ||
|
|
||
| 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 not callback: | ||
| return default | ||
|
|
||
| if not callable(callback): | ||
| raise ImproperlyConfigured(f"{settings_name} must be callable.") | ||
|
|
||
| return callback | ||
|
|
||
|
|
||
| extend_task_props_callback = get_callback_function( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding some sanity checks on the return value of the callback.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm personally not a fan of this generic function, I'd err of the side of caution and make the callback handling as clean and explicit as possible so any errors we need to raise have a clear source.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point @AllexVeldman, what do you think @auvipy?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AllexVeldman & @auvipy I think I found a proper way to control the callback internally being able to check explicitly that return value. I just created a new function called Let me know what you think!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
| "CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK" | ||
| ) | ||
|
|
||
|
|
||
| def get_task_props_extension(request, task_props): | ||
| """Extend the task properties with custom props to fill custom models.""" | ||
| if not extend_task_props_callback: | ||
| return {} | ||
|
|
||
| task_props_extension = extend_task_props_callback(request, task_props) or {} # noqa E501 | ||
| if not isinstance(task_props_extension, Mapping): | ||
| raise ImproperlyConfigured( | ||
| "CELERY_RESULTS_EXTEND_TASK_PROPS_CALLBACK must return a Mapping." | ||
| ) | ||
|
|
||
| return task_props_extension | ||
Uh oh!
There was an error while loading. Please reload this page.