mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-19 22:14:07 +01:00
Use Signal to calculate Priority
This commit is contained in:
parent
39601926ff
commit
3baf716c6f
@ -4,3 +4,6 @@ from django.apps import AppConfig
|
|||||||
class TasksConfig(AppConfig):
|
class TasksConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'tasks'
|
name = 'tasks'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
import tasks.signals
|
||||||
@ -68,27 +68,8 @@ class Todo(Todo):
|
|||||||
|
|
||||||
priority = models.PositiveSmallIntegerField(choices=EisenhowerMatrix.choices, default=EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT)
|
priority = models.PositiveSmallIntegerField(choices=EisenhowerMatrix.choices, default=EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT)
|
||||||
|
|
||||||
def calculate_eisenhower_matrix_category(self):
|
def __str__(self):
|
||||||
if self.end_event:
|
return self.title
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
class Subtask(models.Model):
|
class Subtask(models.Model):
|
||||||
|
|||||||
25
backend/tasks/signals.py
Normal file
25
backend/tasks/signals.py
Normal file
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user