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 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}"