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.
This commit is contained in:
Christopher C. Wells 2022-04-16 14:59:28 -07:00
parent 09e0853067
commit 379f9b1e86
4 changed files with 192 additions and 27 deletions

View File

@ -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",
),
),
]

View File

@ -5,7 +5,10 @@ from django.conf import settings
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from taggit.forms import TagField
from core import models from core import models
from core.widgets import TagsEditor
def set_initial_values(kwargs, form_type): def set_initial_values(kwargs, form_type):
@ -124,6 +127,17 @@ class ChildDeleteForm(forms.ModelForm):
return instance 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 PumpingForm(CoreModelForm):
class Meta: class Meta:
model = models.Pumping model = models.Pumping
@ -139,7 +153,7 @@ class PumpingForm(CoreModelForm):
} }
class DiaperChangeForm(CoreModelForm): class DiaperChangeForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.DiaperChange model = models.DiaperChange
fields = ["child", "time", "wet", "solid", "color", "amount", "notes", "tags"] 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: class Meta:
model = models.Feeding model = models.Feeding
fields = ["child", "start", "end", "type", "method", "amount", "notes", "tags"] 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: class Meta:
model = models.Note model = models.Note
fields = ["child", "note", "time", "tags"] fields = ["child", "note", "time", "tags"]
@ -185,11 +199,11 @@ class NoteForm(CoreModelForm):
"autocomplete": "off", "autocomplete": "off",
"data-target": "#datetimepicker_time", "data-target": "#datetimepicker_time",
} }
), )
} }
class SleepForm(CoreModelForm): class SleepForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.Sleep model = models.Sleep
fields = ["child", "start", "end", "notes", "tags"] fields = ["child", "start", "end", "notes", "tags"]
@ -210,7 +224,7 @@ class SleepForm(CoreModelForm):
} }
class TemperatureForm(CoreModelForm): class TemperatureForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.Temperature model = models.Temperature
fields = ["child", "temperature", "time", "notes", "tags"] fields = ["child", "temperature", "time", "notes", "tags"]
@ -249,7 +263,7 @@ class TimerForm(CoreModelForm):
return instance return instance
class TummyTimeForm(CoreModelForm): class TummyTimeForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.TummyTime model = models.TummyTime
fields = ["child", "start", "end", "milestone", "tags"] fields = ["child", "start", "end", "milestone", "tags"]
@ -269,7 +283,7 @@ class TummyTimeForm(CoreModelForm):
} }
class WeightForm(CoreModelForm): class WeightForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.Weight model = models.Weight
fields = ["child", "weight", "date", "notes", "tags"] fields = ["child", "weight", "date", "notes", "tags"]
@ -284,7 +298,7 @@ class WeightForm(CoreModelForm):
} }
class HeightForm(CoreModelForm): class HeightForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.Height model = models.Height
fields = ["child", "height", "date", "notes", "tags"] fields = ["child", "height", "date", "notes", "tags"]
@ -299,7 +313,7 @@ class HeightForm(CoreModelForm):
} }
class HeadCircumferenceForm(CoreModelForm): class HeadCircumferenceForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.HeadCircumference model = models.HeadCircumference
fields = ["child", "head_circumference", "date", "notes", "tags"] fields = ["child", "head_circumference", "date", "notes", "tags"]
@ -314,7 +328,7 @@ class HeadCircumferenceForm(CoreModelForm):
} }
class BMIForm(CoreModelForm): class BMIForm(CoreModelForm, TaggableModelForm):
class Meta: class Meta:
model = models.BMI model = models.BMI
fields = ["child", "bmi", "date", "notes", "tags"] fields = ["child", "bmi", "date", "notes", "tags"]

View File

@ -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",
),
),
]

View File

@ -152,22 +152,7 @@ class Tagged(GenericTaggedItemBase):
class TaggableManager(TaggitTaggableManager): class TaggableManager(TaggitTaggableManager):
""" pass
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)
class Pumping(models.Model): class Pumping(models.Model):