From 05312da83b1dee797c8d974a4229a41d62addeed Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Tue, 11 Jan 2022 17:58:55 +0100
Subject: [PATCH] Fix Celery task registration with import commands

---
 CHANGELOG.rst                  | 1 +
 aleksis/apps/untis/commands.py | 3 ++-
 aleksis/apps/untis/tasks.py    | 9 +++++----
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index befdb51..53af44f 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -26,6 +26,7 @@ Fixed
 * Import failed if there were classes without class teachers.
 * Management command ``move_dates_for_testing`` throwed misleading errors.
 * Events weren't always deleted due to wrong date filters.
+* Celery tasks always ran the last import command and not the supposed one.
 
 `2.0`_ - 2021-10-30
 -------------------
diff --git a/aleksis/apps/untis/commands.py b/aleksis/apps/untis/commands.py
index 6866cc6..ce6b17a 100644
--- a/aleksis/apps/untis/commands.py
+++ b/aleksis/apps/untis/commands.py
@@ -32,7 +32,7 @@ class ImportCommand:
         if background:
             from .tasks import TASKS
 
-            task = TASKS[cls]
+            task = TASKS[cls.task_name]
             task.delay()
         else:
             _untis_import_mysql(cls.get_terms())
@@ -90,3 +90,4 @@ class CurrentFutureImportCommand(ImportCommand):
 
 
 COMMANDS_BY_NAME = {c.name: c for c in ImportCommand.__subclasses__()}
+COMMANDS_BY_TASK_NAME = {c.task_name: c for c in ImportCommand.__subclasses__()}
diff --git a/aleksis/apps/untis/tasks.py b/aleksis/apps/untis/tasks.py
index c7aaa85..6da6405 100644
--- a/aleksis/apps/untis/tasks.py
+++ b/aleksis/apps/untis/tasks.py
@@ -1,12 +1,13 @@
 from aleksis.core.celery import app
 
-from .commands import ImportCommand
+from .commands import COMMANDS_BY_TASK_NAME, ImportCommand
 
 TASKS = {}
 for import_command in ImportCommand.__subclasses__():
 
-    @app.task(name=import_command.task_name)
-    def _task():
+    @app.task(name=import_command.task_name, bind=True)
+    def _task(self):
+        import_command = COMMANDS_BY_TASK_NAME[self.name]
         import_command.run()
 
-    TASKS[import_command] = _task
+    TASKS[import_command.task_name] = _task
-- 
GitLab