diff --git a/common/fields.py b/common/fields.py index 04a2e7b..4f1593c 100644 --- a/common/fields.py +++ b/common/fields.py @@ -1,5 +1,24 @@ from django.db import models -from django.core.validators import MinValueValidator, MaxValueValidator +from django.core.validators import MinValueValidator, MaxValueValidator, validate_comma_separated_integer_list + +from multiselectfield import MultiSelectField + +class WeekdayChoices(models.IntegerChoices): + MONDAY = 0, "Monday" + TUESDAY = 1, "Tuesday" + WEDNESDAY = 2, "Wednesday" + THURSDAY = 3, "Thursday" + FRIDAY = 4, "Friday" + SATURDAY = 5, "Saturday" + SUNDAY = 6, "Sunday" class PercentageField(models.FloatField): - default_validators = [MaxValueValidator(100), MinValueValidator(0)] \ No newline at end of file + default_validators = [MaxValueValidator(100), MinValueValidator(0)] + +class WeekdayField(MultiSelectField): + def __init__(self, *args, **kwargs): + self.choices = WeekdayChoices.choices + super().__init__(*args, **kwargs) + +class DayOfMonthField(models.PositiveIntegerField): + default_validators = [MinValueValidator(1), MaxValueValidator(31)] \ No newline at end of file diff --git a/habits/models.py b/habits/models.py index 71a8362..5cba07a 100644 --- a/habits/models.py +++ b/habits/models.py @@ -1,3 +1,44 @@ from django.db import models +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 # Create your models here. + +class Habit(models.Model): + name = models.CharField(max_length=64) + icon = models.CharField(default="fas fa-user-clock", max_length=64) + color = ColorField(default="#000000") + description = models.TextField(null=True, blank=True) + active = models.BooleanField(default=True) + +class HabitSchedule(PolymorphicModel): + habit = models.ForeignKey(Habit, models.CASCADE) + + @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: + date = now.replace(day=self.day) + relativedelta(months=1) + elif self.day == now.day: + date = now if today else now + relativedelta(months=1) + else: + date = now + relativedelta(day=self.day) + + return date.date() + +class WeeklyHabitSchedule(HabitSchedule): + weekdays = WeekdayField() + +class DailyHabitSchedule(HabitSchedule): + pass diff --git a/kumify/settings.py b/kumify/settings.py index 76f005f..34b9f9b 100644 --- a/kumify/settings.py +++ b/kumify/settings.py @@ -15,6 +15,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'colorfield', + 'multiselectfield', 'dbsettings', 'common', 'frontend', diff --git a/requirements.txt b/requirements.txt index 44b7f55..d19b323 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,10 +4,13 @@ django[mysql] django-colorfield django-multiupload django-storages +django-polymorphic +django-multiselectfield git+git://github.com/Chive/django-multiupload.git@ba52e161a68ce19062c3655e89544c2d377990a0#egg=multiupload git+https://kumig.it/kumisystems/django-dbsettings.git git+https://kumig.it/kumisystems/django-filtersignals.git mysqlclient boto3 argon2_cffi -python-telegram-bot \ No newline at end of file +python-telegram-bot +python-dateutil