From 3baf716c6f4a75a46c317e94e0eaa2533b68a85a Mon Sep 17 00:00:00 2001 From: sosokker Date: Mon, 6 Nov 2023 22:18:20 +0700 Subject: [PATCH] Use Signal to calculate Priority --- backend/tasks/apps.py | 3 +++ backend/tasks/models.py | 23 ++--------------------- backend/tasks/signals.py | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 backend/tasks/signals.py diff --git a/backend/tasks/apps.py b/backend/tasks/apps.py index 3ff3ab3..0d81307 100644 --- a/backend/tasks/apps.py +++ b/backend/tasks/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class TasksConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'tasks' + + def ready(self): + import tasks.signals \ No newline at end of file diff --git a/backend/tasks/models.py b/backend/tasks/models.py index 9f79e0b..7dcdbc8 100644 --- a/backend/tasks/models.py +++ b/backend/tasks/models.py @@ -68,27 +68,8 @@ class Todo(Todo): priority = models.PositiveSmallIntegerField(choices=EisenhowerMatrix.choices, default=EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT) - def calculate_eisenhower_matrix_category(self): - if self.end_event: - time_until_due = (self.end_event - timezone.now()).days - else: - time_until_due = float('inf') - - urgency_threshold = 3 - importance_threshold = 3 - - if time_until_due <= urgency_threshold and self.importance >= importance_threshold: - return Todo.EisenhowerMatrix.IMPORTANT_URGENT - elif time_until_due > urgency_threshold and self.importance >= importance_threshold: - return Todo.EisenhowerMatrix.IMPORTANT_NOT_URGENT - elif time_until_due <= urgency_threshold and self.importance < importance_threshold: - return Todo.EisenhowerMatrix.NOT_IMPORTANT_URGENT - else: - return Todo.EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT - - def save(self, *args, **kwargs): - self.priority = self.calculate_eisenhower_matrix_category() - super(Todo, self).save(*args, **kwargs) + def __str__(self): + return self.title class Subtask(models.Model): diff --git a/backend/tasks/signals.py b/backend/tasks/signals.py new file mode 100644 index 0000000..af17e57 --- /dev/null +++ b/backend/tasks/signals.py @@ -0,0 +1,25 @@ +from django.db.models.signals import pre_save +from django.dispatch import receiver +from django.utils import timezone + +from tasks.models import Todo + + +@receiver(pre_save, sender=Todo) +def update_priority(sender, instance, **kwargs): + if instance.end_event: + time_until_due = (instance.end_event - timezone.now()).days + else: + time_until_due = float('inf') + + urgency_threshold = 3 + importance_threshold = 3 + + if time_until_due <= urgency_threshold and instance.importance >= importance_threshold: + instance.priority = Todo.EisenhowerMatrix.IMPORTANT_URGENT + elif time_until_due > urgency_threshold and instance.importance >= importance_threshold: + instance.priority = Todo.EisenhowerMatrix.IMPORTANT_NOT_URGENT + elif time_until_due <= urgency_threshold and instance.importance < importance_threshold: + instance.priority = Todo.EisenhowerMatrix.NOT_IMPORTANT_URGENT + else: + instance.priority = Todo.EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT \ No newline at end of file