119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
from datetime import datetime
|
|
|
|
from dateutil.rrule import rrulestr
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.models import Group
|
|
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 SubscriptionSettings(models.Model):
|
|
days_before = models.PositiveIntegerField()
|
|
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def __str__(self):
|
|
return f"{self.days_before} day(s)"
|
|
|
|
|
|
class GroupToolSubscription(SubscriptionSettings):
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
|
tool = models.ForeignKey(Tool, on_delete=models.CASCADE)
|
|
|
|
def get_task_subscriptions(self):
|
|
for task in self.tool.task_set.all():
|
|
yield GroupTaskSubscription(
|
|
days_before=self.days_before,
|
|
group=self.group,
|
|
task=task)
|
|
|
|
class Meta:
|
|
# Django doesn't support multiple-column primary keys
|
|
unique_together = (("group", "tool"),)
|
|
|
|
def __str__(self):
|
|
return f"{self.group}-{self.tool}, {super().__str__()}"
|
|
|
|
|
|
class GroupTaskSubscription(SubscriptionSettings):
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
|
task = models.ForeignKey(Task, on_delete=models.CASCADE)
|
|
|
|
class Meta:
|
|
# Django doesn't support multiple-column primary keys
|
|
unique_together = (("group", "task"),)
|
|
|
|
@property
|
|
def should_remind(self):
|
|
next_recurrence = self.task.next_recurrence
|
|
if next_recurrence is None:
|
|
return False
|
|
time_until_overdue = next_recurrence - datetime.now()
|
|
return self.task.is_overdue or (time_until_overdue.days <= self.days_before)
|
|
|
|
def __str__(self):
|
|
return f"{self.group}-{self.task}, {super().__str__()}"
|
|
|
|
|
|
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}"
|