diff --git a/django_celery_results/models/__init__.py b/django_celery_results/models/__init__.py new file mode 100644 index 00000000..4165e265 --- /dev/null +++ b/django_celery_results/models/__init__.py @@ -0,0 +1,3 @@ +from .generic import ChordCounter, GroupResult, TaskResult + +__ALL__ = [ChordCounter, GroupResult, TaskResult] diff --git a/django_celery_results/models.py b/django_celery_results/models/abstract.py similarity index 82% rename from django_celery_results/models.py rename to django_celery_results/models/abstract.py index d30abb78..fb98f91e 100644 --- a/django_celery_results/models.py +++ b/django_celery_results/models/abstract.py @@ -1,10 +1,6 @@ -"""Database models.""" - -import json +"""Abstract models.""" from celery import states -from celery.result import GroupResult as CeleryGroupResult -from celery.result import result_from_tuple from django.conf import settings from django.db import models from django.utils.translation import gettext_lazy as _ @@ -15,8 +11,8 @@ TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES)) -class TaskResult(models.Model): - """Task result/status.""" +class AbstractTaskResult(models.Model): + """Abstract Task result/status.""" task_id = models.CharField( max_length=getattr( @@ -94,8 +90,8 @@ class TaskResult(models.Model): class Meta: """Table information.""" + abstract = True ordering = ['-date_done'] - verbose_name = _('task result') verbose_name_plural = _('task results') @@ -131,49 +127,8 @@ def __str__(self): return ''.format(self) -class ChordCounter(models.Model): - """Chord synchronisation.""" - - group_id = models.CharField( - max_length=getattr( - settings, - "DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH", - 255), - unique=True, - verbose_name=_("Group ID"), - help_text=_("Celery ID for the Chord header group"), - ) - sub_tasks = models.TextField( - help_text=_( - "JSON serialized list of task result tuples. " - "use .group_result() to decode" - ) - ) - count = models.PositiveIntegerField( - help_text=_( - "Starts at len(chord header) and decrements after each task is " - "finished" - ) - ) - - def group_result(self, app=None): - """Return the :class:`celery.result.GroupResult` of self. - - Arguments: - app (celery.app.base.Celery): app instance to create the - :class:`celery.result.GroupResult` with. - - """ - return CeleryGroupResult( - self.group_id, - [result_from_tuple(r, app=app) - for r in json.loads(self.sub_tasks)], - app=app - ) - - -class GroupResult(models.Model): - """Task Group result/status.""" +class AbstractGroupResult(models.Model): + """Abstract Task Group result/status.""" group_id = models.CharField( max_length=getattr( @@ -226,8 +181,8 @@ def __str__(self): class Meta: """Table information.""" + abstract = True ordering = ['-date_done'] - verbose_name = _('group result') verbose_name_plural = _('group results') diff --git a/django_celery_results/models/generic.py b/django_celery_results/models/generic.py new file mode 100644 index 00000000..74faf614 --- /dev/null +++ b/django_celery_results/models/generic.py @@ -0,0 +1,73 @@ +"""Database models.""" + +import json + +from celery.result import GroupResult as CeleryGroupResult +from celery.result import result_from_tuple +from django.conf import settings +from django.db import models +from django.utils.translation import gettext_lazy as _ + +from django_celery_results.models.abstract import ( + AbstractGroupResult, + AbstractTaskResult +) + + +class TaskResult(AbstractTaskResult): + """Task result/status.""" + + class Meta(AbstractTaskResult.Meta): + """Table information.""" + + abstract = False + + +class ChordCounter(models.Model): + """Chord synchronisation.""" + + group_id = models.CharField( + max_length=getattr( + settings, + "DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH", + 255), + unique=True, + verbose_name=_("Group ID"), + help_text=_("Celery ID for the Chord header group"), + ) + sub_tasks = models.TextField( + help_text=_( + "JSON serialized list of task result tuples. " + "use .group_result() to decode" + ) + ) + count = models.PositiveIntegerField( + help_text=_( + "Starts at len(chord header) and decrements after each task is " + "finished" + ) + ) + + def group_result(self, app=None): + """Return the GroupResult of self. + + Arguments: + --------- + app (Celery): app instance to create the GroupResult with. + + """ + return CeleryGroupResult( + self.group_id, + [result_from_tuple(r, app=app) + for r in json.loads(self.sub_tasks)], + app=app + ) + + +class GroupResult(AbstractGroupResult): + """Task Group result/status.""" + + class Meta(AbstractGroupResult.Meta): + """Table information.""" + + abstract = False