2020-12-03 12:31:39 -05:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from dateutil.rrule import rrulestr
|
|
|
|
from django.contrib.auth import get_user_model
|
2020-12-17 11:47:22 -05:00
|
|
|
from django.contrib.auth.models import Group
|
2020-12-03 12:31:39 -05:00
|
|
|
from django.db import models
|
|
|
|
from django.urls import reverse
|
2020-12-22 15:15:43 -05:00
|
|
|
from markdownx.models import MarkdownxField
|
2020-12-03 12:31:39 -05:00
|
|
|
|
|
|
|
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)
|
2020-12-22 15:15:43 -05:00
|
|
|
description = MarkdownxField(blank=True)
|
2020-12-03 12:31:39 -05:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2020-12-17 11:47:22 -05:00
|
|
|
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)
|
2020-12-07 18:02:46 -05:00
|
|
|
task = models.ForeignKey(Task, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
# Django doesn't support multiple-column primary keys
|
2020-12-17 11:47:22 -05:00
|
|
|
unique_together = (("group", "task"),)
|
2020-12-07 18:02:46 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_remind(self):
|
2020-12-17 11:47:22 -05:00
|
|
|
next_recurrence = self.task.next_recurrence
|
|
|
|
if next_recurrence is None:
|
|
|
|
return False
|
|
|
|
time_until_overdue = next_recurrence - datetime.now()
|
2020-12-07 18:02:46 -05:00
|
|
|
return self.task.is_overdue or (time_until_overdue.days <= self.days_before)
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-12-17 11:47:22 -05:00
|
|
|
return f"{self.group}-{self.task}, {super().__str__()}"
|
2020-12-07 18:02:46 -05:00
|
|
|
|
|
|
|
|
2020-12-03 12:31:39 -05:00
|
|
|
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()
|
2020-12-22 15:15:43 -05:00
|
|
|
notes = MarkdownxField(blank=True)
|
2020-12-03 12:31:39 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.task}: {self.user} {self.date}"
|