cmsmanage/tasks/models.py

87 lines
2.7 KiB
Python

from datetime import datetime
from dateutil.rrule import rrulestr
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
class Tool(models.Model):
name = models.CharField(max_length=200)
asset_tag = models.CharField(max_length=10, primary_key=True)
def __str__(self):
return f"{self.name} - {self.asset_tag}"
def get_absolute_url(self):
return reverse('toolDetail', args=[self.asset_tag])
class Task(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(primary_key=True)
tool = models.ForeignKey(Tool, on_delete=models.CASCADE)
recurrence_interval = models.CharField(max_length=200)
recurrence_base = models.DateField(null=True, blank=True)
def __str__(self):
return f"{self.tool.name}: {self.name}"
def get_absolute_url(self):
return reverse('taskDetail', args=[self.tool.asset_tag, self.slug])
@property
def last_event(self):
return self.event_set.latest('date')
@property
def next_recurrence(self):
if self.recurrence_base is None: # relative date
try:
rrule = rrulestr(self.recurrence_interval, dtstart=self.last_event.date)
return rrule[1]
except Event.DoesNotExist:
return None
else: # absolute date
rrule = rrulestr(self.recurrence_interval, dtstart=self.recurrence_base)
try:
return rrule.after(self.last_event.date)
except Event.DoesNotExist:
return rrule[1]
@property
def is_overdue(self):
next_rec = self.next_recurrence
if next_rec is None:
return False
else:
return next_rec < datetime.now()
class Reminder(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
days_before = models.IntegerField()
class Meta:
# Django doesn't support multiple-column primary keys
unique_together = (("user", "task"),)
@property
def should_remind(self):
time_until_overdue = self.task.next_recurrence - datetime.now()
return self.task.is_overdue or (time_until_overdue.days <= self.days_before)
def __str__(self):
return f"{self.user}-{self.task}, {self.days_before} day(s)"
class Event(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
date = models.DateField()
notes = models.TextField(blank=True)
def __str__(self):
return f"{self.task}: {self.user} {self.date}"