import datetime 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 from markdownx.models import MarkdownxField from recurrence.fields import RecurrenceField class Tool(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(unique=True) asset_tag = models.CharField(max_length=10, unique=True, blank=True) def __str__(self): return f"{self.name}{(' - ' + self.asset_tag) if self.asset_tag else ''}" def get_absolute_url(self): return reverse("tasks:toolDetail", args=[self.slug]) class Task(models.Model): name = models.CharField(max_length=200) slug = models.SlugField() tool = models.ForeignKey(Tool, on_delete=models.CASCADE) description = MarkdownxField(blank=True) recurrence = RecurrenceField(include_dtstart=False) recurrence_base = models.DateField(null=True, blank=True) class Meta: unique_together = (("tool", "slug"),) def __str__(self): return f"{self.tool.name}: {self.name}" def get_absolute_url(self): return reverse("tasks:taskDetail", args=[self.tool.slug, self.slug]) @property def last_event(self): return self.event_set.latest("date") @property def next_recurrence(self): def _date_to_datetime(date): return datetime.datetime.combine(date, datetime.time.min) if self.recurrence_base is None: # relative date try: return self.recurrence.after( _date_to_datetime(self.last_event.date), dtstart=_date_to_datetime(self.last_event.date), ) except Event.DoesNotExist: return None else: # absolute date try: return self.recurrence.after(_date_to_datetime(self.last_event.date)) except Event.DoesNotExist: return self.recurrence.occurrences()[0] @property def is_overdue(self): next_rec = self.next_recurrence if next_rec is None: return False else: return next_rec < datetime.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: 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: 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.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 = MarkdownxField(blank=True) def __str__(self): return f"{self.task}: {self.user} {self.date}"