Prepare models for habits
Add fields for weekdays and days of month Update requirements to include dateutil, polymorphic, multiselectfield
This commit is contained in:
parent
27a19758f3
commit
893c13a0b4
|
@ -1,5 +1,24 @@
|
||||||
from django.db import models
|
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):
|
class PercentageField(models.FloatField):
|
||||||
default_validators = [MaxValueValidator(100), MinValueValidator(0)]
|
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)]
|
|
@ -1,3 +1,44 @@
|
||||||
from django.db import models
|
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.
|
# 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
|
||||||
|
|
|
@ -15,6 +15,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'colorfield',
|
'colorfield',
|
||||||
|
'multiselectfield',
|
||||||
'dbsettings',
|
'dbsettings',
|
||||||
'common',
|
'common',
|
||||||
'frontend',
|
'frontend',
|
||||||
|
|
|
@ -4,6 +4,8 @@ django[mysql]
|
||||||
django-colorfield
|
django-colorfield
|
||||||
django-multiupload
|
django-multiupload
|
||||||
django-storages
|
django-storages
|
||||||
|
django-polymorphic
|
||||||
|
django-multiselectfield
|
||||||
git+git://github.com/Chive/django-multiupload.git@ba52e161a68ce19062c3655e89544c2d377990a0#egg=multiupload
|
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-dbsettings.git
|
||||||
git+https://kumig.it/kumisystems/django-filtersignals.git
|
git+https://kumig.it/kumisystems/django-filtersignals.git
|
||||||
|
@ -11,3 +13,4 @@ mysqlclient
|
||||||
boto3
|
boto3
|
||||||
argon2_cffi
|
argon2_cffi
|
||||||
python-telegram-bot
|
python-telegram-bot
|
||||||
|
python-dateutil
|
||||||
|
|
Loading…
Reference in a new issue