2021-01-03 07:42:04 +00:00
|
|
|
from django.db import models
|
2021-01-08 17:32:25 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from colorfield.fields import ColorField
|
|
|
|
from polymorphic.models import PolymorphicModel
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
from common.fields import WeekdayField, DayOfMonthField
|
2021-01-03 07:42:04 +00:00
|
|
|
|
|
|
|
# Create your models here.
|
2021-01-08 17:32:25 +00:00
|
|
|
|
|
|
|
class Habit(models.Model):
|
|
|
|
name = models.CharField(max_length=64)
|
2021-01-15 14:14:08 +00:00
|
|
|
icon = models.CharField(default="fas fa-user-clock")
|
2021-01-08 17:32:25 +00:00
|
|
|
color = ColorField(default="#000000")
|
|
|
|
description = models.TextField(null=True, blank=True)
|
|
|
|
|
|
|
|
class HabitSchedule(PolymorphicModel):
|
|
|
|
habit = models.ForeignKey(Habit, models.CASCADE)
|
2021-01-15 14:14:08 +00:00
|
|
|
active = models.BooleanField(default=True)
|
2021-01-08 17:32:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def next_scheduled(self, today=True, now=timezone.now()):
|
|
|
|
raise NotImplementedError("%s does not implement next_scheduled." % self.__class__)
|
|
|
|
|
|
|
|
class MonthlyHabitSchedule(HabitSchedule):
|
|
|
|
day = DayOfMonthField()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def next_scheduled(self, today=True, now=timezone.now()):
|
|
|
|
if self.day < now.day:
|
2021-01-15 14:14:08 +00:00
|
|
|
date = now + relativedelta(months=1) + relativedelta(day=self.day)
|
2021-01-08 17:32:25 +00:00
|
|
|
elif self.day == now.day:
|
|
|
|
date = now if today else now + relativedelta(months=1)
|
|
|
|
else:
|
|
|
|
date = now + relativedelta(day=self.day)
|
|
|
|
|
2021-01-15 14:14:08 +00:00
|
|
|
if date.date() == now.date() and not today:
|
|
|
|
date = now + relativedelta(months=1) + relativedelta(day=self.day)
|
|
|
|
|
2021-01-08 17:32:25 +00:00
|
|
|
return date.date()
|
|
|
|
|
|
|
|
class WeeklyHabitSchedule(HabitSchedule):
|
|
|
|
weekdays = WeekdayField()
|
|
|
|
|
2021-01-15 14:14:08 +00:00
|
|
|
@property
|
|
|
|
def next_scheduled(self, today=True, now=timezone.now()):
|
|
|
|
found = None
|
|
|
|
for weekday in self.weekdays:
|
|
|
|
on = now + relativedelta(weekday=weekday)
|
|
|
|
if on < now:
|
|
|
|
on += relativedelta(weeks=1)
|
|
|
|
if on.day == now.day:
|
|
|
|
if today:
|
|
|
|
found = now
|
|
|
|
break
|
|
|
|
on += relativedelta(weeks=1)
|
|
|
|
if (not found) or on.date() < found.date():
|
|
|
|
found = on
|
|
|
|
|
|
|
|
return found.date()
|
|
|
|
|
2021-01-08 17:32:25 +00:00
|
|
|
class DailyHabitSchedule(HabitSchedule):
|
2021-01-15 14:14:08 +00:00
|
|
|
@property
|
|
|
|
def next_scheduled(self, today=True, now=timezone.now()):
|
|
|
|
if self.active:
|
|
|
|
return (now if today else (now + relativedelta(days=1))).date()
|
|
|
|
|
|
|
|
class DateHabitSchedule(HabitSchedule):
|
|
|
|
date = models.DateField()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def next_scheduled(self, today=True, now=timezone.now()):
|
|
|
|
if self.active and ((self.date > now.date()) or (today and (self.date == now.date()))):
|
|
|
|
return self.date
|