From ce21f39ec72ed88f4bd4e5cbc21b857a1bd23be7 Mon Sep 17 00:00:00 2001 From: Paul Konstantin Gerke Date: Tue, 8 Mar 2022 13:44:35 +0100 Subject: [PATCH] Add tags to most things except: Timers and Children --- core/forms.py | 20 ++-- ...diaperchange_tags_feeding_tags_and_more.py | 113 ++++++++++++++++++ core/models.py | 19 ++- 3 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 core/migrations/0020_bmi_tags_diaperchange_tags_feeding_tags_and_more.py diff --git a/core/forms.py b/core/forms.py index a04f84955..483dac5b9 100644 --- a/core/forms.py +++ b/core/forms.py @@ -6,7 +6,6 @@ from django.utils.translation import gettext as _ from core import models -from core.widgets import TagsEditor def set_initial_values(kwargs, form_type): @@ -128,7 +127,7 @@ def save(self, commit=True): class DiaperChangeForm(CoreModelForm): class Meta: model = models.DiaperChange - fields = ["child", "time", "wet", "solid", "color", "amount", "notes"] + fields = ["child", "time", "wet", "solid", "color", "amount", "notes", "tags"] widgets = { "time": forms.DateTimeInput( attrs={ @@ -143,7 +142,7 @@ class Meta: class FeedingForm(CoreModelForm): class Meta: model = models.Feeding - fields = ["child", "start", "end", "type", "method", "amount", "notes"] + fields = ["child", "start", "end", "type", "method", "amount", "notes", "tags"] widgets = { "start": forms.DateTimeInput( attrs={ @@ -172,14 +171,13 @@ class Meta: "data-target": "#datetimepicker_time", } ), - "tags": TagsEditor(), } class SleepForm(CoreModelForm): class Meta: model = models.Sleep - fields = ["child", "start", "end", "notes"] + fields = ["child", "start", "end", "notes", "tags"] widgets = { "start": forms.DateTimeInput( attrs={ @@ -200,7 +198,7 @@ class Meta: class TemperatureForm(CoreModelForm): class Meta: model = models.Temperature - fields = ["child", "temperature", "time", "notes"] + fields = ["child", "temperature", "time", "notes", "tags"] widgets = { "time": forms.DateTimeInput( attrs={ @@ -239,7 +237,7 @@ def save(self, commit=True): class TummyTimeForm(CoreModelForm): class Meta: model = models.TummyTime - fields = ["child", "start", "end", "milestone"] + fields = ["child", "start", "end", "milestone", "tags"] widgets = { "start": forms.DateTimeInput( attrs={ @@ -259,7 +257,7 @@ class Meta: class WeightForm(CoreModelForm): class Meta: model = models.Weight - fields = ["child", "weight", "date", "notes"] + fields = ["child", "weight", "date", "notes", "tags"] widgets = { "date": forms.DateInput( attrs={ @@ -274,7 +272,7 @@ class Meta: class HeightForm(CoreModelForm): class Meta: model = models.Height - fields = ["child", "height", "date", "notes"] + fields = ["child", "height", "date", "notes", "tags"] widgets = { "date": forms.DateInput( attrs={ @@ -289,7 +287,7 @@ class Meta: class HeadCircumferenceForm(CoreModelForm): class Meta: model = models.HeadCircumference - fields = ["child", "head_circumference", "date", "notes"] + fields = ["child", "head_circumference", "date", "notes", "tags"] widgets = { "date": forms.DateInput( attrs={ @@ -304,7 +302,7 @@ class Meta: class BMIForm(CoreModelForm): class Meta: model = models.BMI - fields = ["child", "bmi", "date", "notes"] + fields = ["child", "bmi", "date", "notes", "tags"] widgets = { "date": forms.DateInput( attrs={ diff --git a/core/migrations/0020_bmi_tags_diaperchange_tags_feeding_tags_and_more.py b/core/migrations/0020_bmi_tags_diaperchange_tags_feeding_tags_and_more.py new file mode 100644 index 000000000..44d453910 --- /dev/null +++ b/core/migrations/0020_bmi_tags_diaperchange_tags_feeding_tags_and_more.py @@ -0,0 +1,113 @@ +# Generated by Django 4.0.3 on 2022-03-08 12:42 + +import core.models +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0019_tag_tagged_note_tags"), + ] + + operations = [ + migrations.AddField( + model_name="bmi", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="diaperchange", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="feeding", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="headcircumference", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="height", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="sleep", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="temperature", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="tummytime", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AddField( + model_name="weight", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + ] diff --git a/core/models.py b/core/models.py index e45ef5d4b..c581c1299 100644 --- a/core/models.py +++ b/core/models.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import random from datetime import timedelta from typing import Iterable, Optional @@ -12,8 +13,6 @@ from django.utils.translation import gettext_lazy as _ from django.core.validators import RegexValidator -import random - from taggit.managers import TaggableManager as TaggitTaggableManager from taggit.models import TagBase, GenericTaggedItemBase @@ -151,6 +150,13 @@ def __init__(self, *args, **kwargs): ) super().__init__(*args, **kwargs) + def formfield(self, *args, **kwargs): + # Local import required because .widgets imports .models + from core.widgets import TagsEditor + + kwargs["widget"] = TagsEditor + return super().formfield(*args, **kwargs) + class Child(models.Model): model_name = "child" @@ -230,6 +236,7 @@ class DiaperChange(models.Model): ) amount = models.FloatField(blank=True, null=True, verbose_name=_("Amount")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -299,6 +306,7 @@ class Feeding(models.Model): ) amount = models.FloatField(blank=True, null=True, verbose_name=_("Amount")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -364,6 +372,7 @@ class Sleep(models.Model): editable=False, null=True, verbose_name=_("Duration") ) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() naps = NapsManager() @@ -414,6 +423,7 @@ class Temperature(models.Model): ) time = models.DateTimeField(blank=False, null=False, verbose_name=_("Time")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -541,6 +551,7 @@ class TummyTime(models.Model): milestone = models.CharField( blank=True, max_length=255, verbose_name=_("Milestone") ) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -576,6 +587,7 @@ class Weight(models.Model): weight = models.FloatField(blank=False, null=False, verbose_name=_("Weight")) date = models.DateField(blank=False, null=False, verbose_name=_("Date")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -603,6 +615,7 @@ class Height(models.Model): height = models.FloatField(blank=False, null=False, verbose_name=_("Height")) date = models.DateField(blank=False, null=False, verbose_name=_("Date")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -632,6 +645,7 @@ class HeadCircumference(models.Model): ) date = models.DateField(blank=False, null=False, verbose_name=_("Date")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager() @@ -656,6 +670,7 @@ class BMI(models.Model): bmi = models.FloatField(blank=False, null=False, verbose_name=_("BMI")) date = models.DateField(blank=False, null=False, verbose_name=_("Date")) notes = models.TextField(blank=True, null=True, verbose_name=_("Notes")) + tags = TaggableManager(blank=True, through=Tagged) objects = models.Manager()