Skip to content

Commit f04bf48

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

File tree

3 files changed

+132
-38
lines changed

3 files changed

+132
-38
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: 58 additions & 38 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,9 +11,7 @@
1511
TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))
1612

1713

18-
class TaskResult(models.Model):
19-
"""Task result/status."""
20-
14+
class AbstractTaskResult(models.Model):
2115
task_id = models.CharField(
2216
max_length=getattr(
2317
settings,
@@ -94,8 +88,8 @@ class TaskResult(models.Model):
9488
class Meta:
9589
"""Table information."""
9690

91+
abstract = True
9792
ordering = ['-date_done']
98-
9993
verbose_name = _('task result')
10094
verbose_name_plural = _('task results')
10195

@@ -131,48 +125,74 @@ def __str__(self):
131125
return '<Task: {0.task_id} ({0.status})>'.format(self)
132126

133127

134-
class ChordCounter(models.Model):
135-
"""Chord synchronisation."""
128+
129+
class AbstractGroupResult(models.Model):
130+
"""Task Group result/status."""
136131

137132
group_id = models.CharField(
138133
max_length=getattr(
139134
settings,
140135
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
141-
255),
136+
255
137+
),
142138
unique=True,
143139
verbose_name=_("Group ID"),
144-
help_text=_("Celery ID for the Chord header group"),
140+
help_text=_("Celery ID for the Group that was run"),
145141
)
146-
sub_tasks = models.TextField(
147-
help_text=_(
148-
"JSON serialized list of task result tuples. "
149-
"use .group_result() to decode"
150-
)
142+
date_created = models.DateTimeField(
143+
auto_now_add=True,
144+
verbose_name=_("Created DateTime"),
145+
help_text=_("Datetime field when the group result was created in UTC"),
151146
)
152-
count = models.PositiveIntegerField(
153-
help_text=_(
154-
"Starts at len(chord header) and decrements after each task is "
155-
"finished"
156-
)
147+
date_done = models.DateTimeField(
148+
auto_now=True,
149+
verbose_name=_("Completed DateTime"),
150+
help_text=_("Datetime field when the group was completed in UTC"),
151+
)
152+
content_type = models.CharField(
153+
max_length=128,
154+
verbose_name=_("Result Content Type"),
155+
help_text=_("Content type of the result data"),
156+
)
157+
content_encoding = models.CharField(
158+
max_length=64,
159+
verbose_name=_("Result Encoding"),
160+
help_text=_("The encoding used to save the task result data"),
157161
)
162+
result = models.TextField(
163+
null=True, default=None, editable=False,
164+
verbose_name=_('Result Data'),
165+
help_text=_('The data returned by the task. '
166+
'Use content_encoding and content_type fields to read.'))
167+
168+
def as_dict(self):
169+
return {
170+
'group_id': self.group_id,
171+
'result': self.result,
172+
'date_done': self.date_done,
173+
}
174+
175+
def __str__(self):
176+
return f'<Group: {self.group_id}>'
177+
178+
objects = managers.GroupResultManager()
158179

159-
def group_result(self, app=None):
160-
"""Return the GroupResult of self.
180+
class Meta:
181+
"""Table information."""
182+
183+
ordering = ['-date_done']
161184

162-
Arguments:
163-
---------
164-
app (Celery): app instance to create the GroupResult with.
165185

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-
)
186+
# Explicit names to solve https://code.djangoproject.com/ticket/33483
187+
indexes = [
188+
models.Index(fields=['date_created'],
189+
name='django_cele_date_cr_bd6c1d_idx'),
190+
models.Index(fields=['date_done'],
191+
name='django_cele_date_do_caae0e_idx'),
192+
]
173193

174194

175-
class GroupResult(models.Model):
195+
class AbstractGroupResult(models.Model):
176196
"""Task Group result/status."""
177197

178198
group_id = models.CharField(
@@ -226,10 +246,10 @@ def __str__(self):
226246
class Meta:
227247
"""Table information."""
228248

229-
ordering = ['-date_done']
230-
249+
abstract = True
231250
verbose_name = _('group result')
232251
verbose_name_plural = _('group results')
252+
ordering = ['-date_done']
233253

234254
# Explicit names to solve https://code.djangoproject.com/ticket/33483
235255
indexes = [
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
19+
class Meta(AbstractTaskResult.Meta):
20+
"""Table information."""
21+
22+
abstract = False
23+
24+
25+
class ChordCounter(models.Model):
26+
"""Chord synchronisation."""
27+
28+
group_id = models.CharField(
29+
max_length=getattr(
30+
settings,
31+
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
32+
255),
33+
unique=True,
34+
verbose_name=_("Group ID"),
35+
help_text=_("Celery ID for the Chord header group"),
36+
)
37+
sub_tasks = models.TextField(
38+
help_text=_(
39+
"JSON serialized list of task result tuples. "
40+
"use .group_result() to decode"
41+
)
42+
)
43+
count = models.PositiveIntegerField(
44+
help_text=_(
45+
"Starts at len(chord header) and decrements after each task is "
46+
"finished"
47+
)
48+
)
49+
50+
def group_result(self, app=None):
51+
"""Return the GroupResult of self.
52+
53+
Arguments:
54+
---------
55+
app (Celery): app instance to create the GroupResult with.
56+
57+
"""
58+
return CeleryGroupResult(
59+
self.group_id,
60+
[result_from_tuple(r, app=app)
61+
for r in json.loads(self.sub_tasks)],
62+
app=app
63+
)
64+
65+
66+
class GroupResult(AbstractGroupResult):
67+
68+
class Meta(AbstractGroupResult.Meta):
69+
"""Table information."""
70+
71+
abstract = False

0 commit comments

Comments
 (0)