-
Notifications
You must be signed in to change notification settings - Fork 220
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
5e10a6a
1b1facb
b597c1c
1311f5e
ab74d4d
8c83433
0ea8ab8
603637b
20a03e0
97880a2
49874bc
8509a64
d72b7e8
4b9dfef
60fd681
c2042fa
1cf3b52
c1bd4c2
3591140
885f08c
3f1bfac
271b080
7395269
ed17294
7be45b5
e7fb95e
6245d83
34e36cd
4c24fde
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,37 @@ | ||||||||||||
| """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" | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class ChordCounter(AbstractChordCounter): | ||||||||||||
| """Chord synchronisation.""" | ||||||||||||
|
|
||||||||||||
| class Meta(AbstractChordCounter.Meta): | ||||||||||||
| """Table information.""" | ||||||||||||
|
|
||||||||||||
| abstract = False | ||||||||||||
| app_label = "django_celery_results" | ||||||||||||
|
Comment on lines
+26
to
+27
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.
Suggested change
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class GroupResult(AbstractGroupResult): | ||||||||||||
| """Task Group result/status.""" | ||||||||||||
|
|
||||||||||||
| class Meta(AbstractGroupResult.Meta): | ||||||||||||
| """Table information.""" | ||||||||||||
|
|
||||||||||||
| abstract = False | ||||||||||||
| app_label = "django_celery_results" | ||||||||||||
|
Comment on lines
+36
to
+37
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.
Suggested change
|
||||||||||||
| 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 taskresult_model(): | ||
|
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 would follow Django auth's app establish pattern from |
||
| """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 chordcounter_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 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" | ||
| ) | ||
| 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( | ||
|
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. 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, 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. 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? 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! 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.