From 379f9b1e867c0b5670b290762460189abc1e2433 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 16 Apr 2022 14:59:28 -0700 Subject: [PATCH] Move tag widget customizations to form classes This allows the custom tag widget to function normally in Baby Buddy's frontend without breaking behaviors in the Django admin forms. The previous setup would prevent the standard tag editor behavior from working in the Django admin and give incorrect help text. --- .../0022_alter_settings_language.py | 38 ++++++ core/forms.py | 36 +++-- ...ter_tag_options_alter_bmi_tags_and_more.py | 128 ++++++++++++++++++ core/models.py | 17 +-- 4 files changed, 192 insertions(+), 27 deletions(-) create mode 100644 babybuddy/migrations/0022_alter_settings_language.py create mode 100644 core/migrations/0023_alter_tag_options_alter_bmi_tags_and_more.py diff --git a/babybuddy/migrations/0022_alter_settings_language.py b/babybuddy/migrations/0022_alter_settings_language.py new file mode 100644 index 00000000..5abc55c9 --- /dev/null +++ b/babybuddy/migrations/0022_alter_settings_language.py @@ -0,0 +1,38 @@ +# Generated by Django 4.0.3 on 2022-04-16 21:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("babybuddy", "0021_alter_settings_language"), + ] + + operations = [ + migrations.AlterField( + model_name="settings", + name="language", + field=models.CharField( + choices=[ + ("ca", "Catalan"), + ("zh-hans", "Chinese (simplified)"), + ("nl", "Dutch"), + ("en-US", "English (US)"), + ("en-GB", "English (UK)"), + ("fr", "French"), + ("fi", "Finnish"), + ("de", "German"), + ("it", "Italian"), + ("pl", "Polish"), + ("pt", "Portuguese"), + ("es", "Spanish"), + ("sv", "Swedish"), + ("tr", "Turkish"), + ], + default="en-US", + max_length=255, + verbose_name="Language", + ), + ), + ] diff --git a/core/forms.py b/core/forms.py index dd4ebe46..50fd32b3 100644 --- a/core/forms.py +++ b/core/forms.py @@ -5,7 +5,10 @@ from django.conf import settings from django.utils import timezone from django.utils.translation import gettext as _ +from taggit.forms import TagField + from core import models +from core.widgets import TagsEditor def set_initial_values(kwargs, form_type): @@ -124,6 +127,17 @@ class ChildDeleteForm(forms.ModelForm): return instance +class TaggableModelForm(forms.ModelForm): + tags = TagField( + widget=TagsEditor, + required=False, + strip=True, + help_text=_( + "Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags." + ), + ) + + class PumpingForm(CoreModelForm): class Meta: model = models.Pumping @@ -139,7 +153,7 @@ class PumpingForm(CoreModelForm): } -class DiaperChangeForm(CoreModelForm): +class DiaperChangeForm(CoreModelForm, TaggableModelForm): class Meta: model = models.DiaperChange fields = ["child", "time", "wet", "solid", "color", "amount", "notes", "tags"] @@ -154,7 +168,7 @@ class DiaperChangeForm(CoreModelForm): } -class FeedingForm(CoreModelForm): +class FeedingForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Feeding fields = ["child", "start", "end", "type", "method", "amount", "notes", "tags"] @@ -175,7 +189,7 @@ class FeedingForm(CoreModelForm): } -class NoteForm(CoreModelForm): +class NoteForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Note fields = ["child", "note", "time", "tags"] @@ -185,11 +199,11 @@ class NoteForm(CoreModelForm): "autocomplete": "off", "data-target": "#datetimepicker_time", } - ), + ) } -class SleepForm(CoreModelForm): +class SleepForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Sleep fields = ["child", "start", "end", "notes", "tags"] @@ -210,7 +224,7 @@ class SleepForm(CoreModelForm): } -class TemperatureForm(CoreModelForm): +class TemperatureForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Temperature fields = ["child", "temperature", "time", "notes", "tags"] @@ -249,7 +263,7 @@ class TimerForm(CoreModelForm): return instance -class TummyTimeForm(CoreModelForm): +class TummyTimeForm(CoreModelForm, TaggableModelForm): class Meta: model = models.TummyTime fields = ["child", "start", "end", "milestone", "tags"] @@ -269,7 +283,7 @@ class TummyTimeForm(CoreModelForm): } -class WeightForm(CoreModelForm): +class WeightForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Weight fields = ["child", "weight", "date", "notes", "tags"] @@ -284,7 +298,7 @@ class WeightForm(CoreModelForm): } -class HeightForm(CoreModelForm): +class HeightForm(CoreModelForm, TaggableModelForm): class Meta: model = models.Height fields = ["child", "height", "date", "notes", "tags"] @@ -299,7 +313,7 @@ class HeightForm(CoreModelForm): } -class HeadCircumferenceForm(CoreModelForm): +class HeadCircumferenceForm(CoreModelForm, TaggableModelForm): class Meta: model = models.HeadCircumference fields = ["child", "head_circumference", "date", "notes", "tags"] @@ -314,7 +328,7 @@ class HeadCircumferenceForm(CoreModelForm): } -class BMIForm(CoreModelForm): +class BMIForm(CoreModelForm, TaggableModelForm): class Meta: model = models.BMI fields = ["child", "bmi", "date", "notes", "tags"] diff --git a/core/migrations/0023_alter_tag_options_alter_bmi_tags_and_more.py b/core/migrations/0023_alter_tag_options_alter_bmi_tags_and_more.py new file mode 100644 index 00000000..19aee067 --- /dev/null +++ b/core/migrations/0023_alter_tag_options_alter_bmi_tags_and_more.py @@ -0,0 +1,128 @@ +# Generated by Django 4.0.3 on 2022-04-16 21:56 + +import core.models +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("core", "0022_alter_default_date_and_time"), + ] + + operations = [ + migrations.AlterModelOptions( + name="tag", + options={"verbose_name": "Tag", "verbose_name_plural": "Tags"}, + ), + migrations.AlterField( + model_name="bmi", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="diaperchange", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="feeding", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="headcircumference", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="height", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="note", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="sleep", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="temperature", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="tummytime", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + migrations.AlterField( + model_name="weight", + name="tags", + field=core.models.TaggableManager( + blank=True, + help_text="A comma-separated list of tags.", + through="core.Tagged", + to="core.Tag", + verbose_name="Tags", + ), + ), + ] diff --git a/core/models.py b/core/models.py index bd47b70c..03a89a21 100644 --- a/core/models.py +++ b/core/models.py @@ -152,22 +152,7 @@ class Tagged(GenericTaggedItemBase): class TaggableManager(TaggitTaggableManager): - """ - Replace the default help_text with - """ - - def __init__(self, *args, **kwargs): - kwargs["help_text"] = _( - "Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags." - ) - 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) + pass class Pumping(models.Model):