Skip to content

Commit d9b1daf

Browse files
committed
Added support for abstract base models
1 parent 12a231f commit d9b1daf

File tree

3 files changed

+83
-52
lines changed

3 files changed

+83
-52
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .generic import ChordCounter, GroupResult, TaskResult
2+
3+
__ALL__ = [ChordCounter, GroupResult, TaskResult]

django_celery_results/models.py renamed to django_celery_results/models/abstract.py

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
"""Database models."""
2-
3-
import json
1+
"""Abstract models."""
42

53
from celery import states
6-
from celery.result import GroupResult as CeleryGroupResult
7-
from celery.result import result_from_tuple
84
from django.conf import settings
95
from django.db import models
106
from django.utils.translation import gettext_lazy as _
@@ -15,8 +11,8 @@
1511
TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))
1612

1713

18-
class TaskResult(models.Model):
19-
"""Task result/status."""
14+
class AbstractTaskResult(models.Model):
15+
"""Abstract Task result/status."""
2016

2117
task_id = models.CharField(
2218
max_length=getattr(
@@ -94,8 +90,8 @@ class TaskResult(models.Model):
9490
class Meta:
9591
"""Table information."""
9692

93+
abstract = True
9794
ordering = ['-date_done']
98-
9995
verbose_name = _('task result')
10096
verbose_name_plural = _('task results')
10197

@@ -131,49 +127,8 @@ def __str__(self):
131127
return '<Task: {0.task_id} ({0.status})>'.format(self)
132128

133129

134-
class ChordCounter(models.Model):
135-
"""Chord synchronisation."""
136-
137-
group_id = models.CharField(
138-
max_length=getattr(
139-
settings,
140-
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
141-
255),
142-
unique=True,
143-
verbose_name=_("Group ID"),
144-
help_text=_("Celery ID for the Chord header group"),
145-
)
146-
sub_tasks = models.TextField(
147-
help_text=_(
148-
"JSON serialized list of task result tuples. "
149-
"use .group_result() to decode"
150-
)
151-
)
152-
count = models.PositiveIntegerField(
153-
help_text=_(
154-
"Starts at len(chord header) and decrements after each task is "
155-
"finished"
156-
)
157-
)
158-
159-
def group_result(self, app=None):
160-
"""Return the GroupResult of self.
161-
162-
Arguments:
163-
---------
164-
app (Celery): app instance to create the GroupResult with.
165-
166-
"""
167-
return CeleryGroupResult(
168-
self.group_id,
169-
[result_from_tuple(r, app=app)
170-
for r in json.loads(self.sub_tasks)],
171-
app=app
172-
)
173-
174-
175-
class GroupResult(models.Model):
176-
"""Task Group result/status."""
130+
class AbstractGroupResult(models.Model):
131+
"""Abstract Task Group result/status."""
177132

178133
group_id = models.CharField(
179134
max_length=getattr(
@@ -226,8 +181,8 @@ def __str__(self):
226181
class Meta:
227182
"""Table information."""
228183

184+
abstract = True
229185
ordering = ['-date_done']
230-
231186
verbose_name = _('group result')
232187
verbose_name_plural = _('group results')
233188

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Database models."""
2+
3+
import json
4+
5+
from celery.result import GroupResult as CeleryGroupResult
6+
from celery.result import result_from_tuple
7+
from django.conf import settings
8+
from django.db import models
9+
from django.utils.translation import gettext_lazy as _
10+
11+
from django_celery_results.models.abstract import (
12+
AbstractGroupResult,
13+
AbstractTaskResult
14+
)
15+
16+
17+
class TaskResult(AbstractTaskResult):
18+
"""Task result/status."""
19+
20+
class Meta(AbstractTaskResult.Meta):
21+
"""Table information."""
22+
23+
abstract = False
24+
25+
26+
class ChordCounter(models.Model):
27+
"""Chord synchronisation."""
28+
29+
group_id = models.CharField(
30+
max_length=getattr(
31+
settings,
32+
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
33+
255),
34+
unique=True,
35+
verbose_name=_("Group ID"),
36+
help_text=_("Celery ID for the Chord header group"),
37+
)
38+
sub_tasks = models.TextField(
39+
help_text=_(
40+
"JSON serialized list of task result tuples. "
41+
"use .group_result() to decode"
42+
)
43+
)
44+
count = models.PositiveIntegerField(
45+
help_text=_(
46+
"Starts at len(chord header) and decrements after each task is "
47+
"finished"
48+
)
49+
)
50+
51+
def group_result(self, app=None):
52+
"""Return the GroupResult of self.
53+
54+
Arguments:
55+
---------
56+
app (Celery): app instance to create the GroupResult with.
57+
58+
"""
59+
return CeleryGroupResult(
60+
self.group_id,
61+
[result_from_tuple(r, app=app)
62+
for r in json.loads(self.sub_tasks)],
63+
app=app
64+
)
65+
66+
67+
class GroupResult(AbstractGroupResult):
68+
"""Task Group result/status."""
69+
70+
class Meta(AbstractGroupResult.Meta):
71+
"""Table information."""
72+
73+
abstract = False

0 commit comments

Comments
 (0)