File tree Expand file tree Collapse file tree 3 files changed +31
-5
lines changed
django_tasks/backends/database Expand file tree Collapse file tree 3 files changed +31
-5
lines changed Original file line number Diff line number Diff line change 10
10
class DBTaskResultAdmin (admin .ModelAdmin ):
11
11
list_display = (
12
12
"id" ,
13
- "get_task_name " ,
13
+ "task_name " ,
14
14
"status" ,
15
15
"enqueued_at" ,
16
16
"started_at" ,
@@ -40,7 +40,3 @@ def get_readonly_fields(
40
40
self , request : HttpRequest , obj : Optional [DBTaskResult ] = None
41
41
) -> List [str ]:
42
42
return [f .name for f in self .model ._meta .fields ]
43
-
44
- @admin .display (description = "Task" )
45
- def get_task_name (self , obj : DBTaskResult ) -> str :
46
- return obj .task .name
Original file line number Diff line number Diff line change @@ -170,6 +170,20 @@ def task_result(self) -> "TaskResult[T]":
170
170
171
171
return task_result
172
172
173
+ @property
174
+ def task_name (self ) -> str :
175
+ # If the function for an existing task is no longer available, it'll either raise an
176
+ # ImportError or ModuleNotFoundError (a subclass of ImportError).
177
+ try :
178
+ return self .task .name
179
+ except ImportError :
180
+ pass
181
+
182
+ try :
183
+ return self .task_path .rsplit ("." , 1 )[1 ]
184
+ except IndexError :
185
+ return self .task_path
186
+
173
187
@retry (backoff_delay = 0 )
174
188
def claim (self ) -> None :
175
189
"""
Original file line number Diff line number Diff line change @@ -232,6 +232,22 @@ def test_missing_task_path(self) -> None:
232
232
with self .assertRaises (ImportError ):
233
233
_ = db_task_result .task
234
234
235
+ def test_task_name (self ) -> None :
236
+ for task_path , expected_task_name in [
237
+ ("tests.tasks.noop_task" , "noop_task" ),
238
+ ("tests.tasks.task_not_found" , "task_not_found" ),
239
+ ("tests.tasks.module_not_found.module_not_found" , "module_not_found" ),
240
+ ("unexpected_function" , "unexpected_function" ),
241
+ ]:
242
+ with self .subTest (task_path ):
243
+ db_task_result = DBTaskResult .objects .create (
244
+ args_kwargs = {"args" : [], "kwargs" : {}},
245
+ task_path = task_path ,
246
+ backend_name = "default" ,
247
+ )
248
+
249
+ self .assertEqual (db_task_result .task_name , expected_task_name )
250
+
235
251
def test_check (self ) -> None :
236
252
errors = list (default_task_backend .check ())
237
253
You can’t perform that action at this time.
0 commit comments