This commit is contained in:
Paul Konstantin Gerke 2022-02-27 20:00:12 +01:00
parent ae1dee1d7b
commit 3ac21ed67b
3 changed files with 33 additions and 10 deletions

View File

@ -7,6 +7,7 @@ from django.utils.translation import gettext as _
from core import models from core import models
from core.widgets import TagsEditor from core.widgets import TagsEditor
def set_initial_values(kwargs, form_type): def set_initial_values(kwargs, form_type):
""" """
Sets initial value for add forms based on provided kwargs. Sets initial value for add forms based on provided kwargs.
@ -170,7 +171,7 @@ class NoteForm(CoreModelForm):
"data-target": "#datetimepicker_time", "data-target": "#datetimepicker_time",
} }
), ),
"tags": TagsEditor() "tags": TagsEditor(),
} }

View File

@ -22,6 +22,7 @@ from taggit.models import TagBase, GenericTaggedItemBase, TaggedItemBase
random.seed(time.time()) random.seed(time.time())
def validate_date(date, field_name): def validate_date(date, field_name):
""" """
Confirm that a date is not in the future. Confirm that a date is not in the future.
@ -81,15 +82,31 @@ def validate_time(time, field_name):
{field_name: _("Date/time can not be in the future.")}, code="time_invalid" {field_name: _("Date/time can not be in the future.")}, code="time_invalid"
) )
TAG_COLORS = [
"#FF0000", "#00FF00", "#0000FF", "#FF00FF", "#FFFF00", "#00FFFF",
"#FF7F7F", "#7FFF7F", "#7F7FFF", "#FF7FFF", "#FFFF7F", "#7FFFFF",
"#7F0000", "#007F00", "#00007F", "#7F007F", "#7F7F00", "#007F7F",
]
def random_color(): def random_color():
TAG_COLORS = [
"#FF0000",
"#00FF00",
"#0000FF",
"#FF00FF",
"#FFFF00",
"#00FFFF",
"#FF7F7F",
"#7FFF7F",
"#7F7FFF",
"#FF7FFF",
"#FFFF7F",
"#7FFFFF",
"#7F0000",
"#007F00",
"#00007F",
"#7F007F",
"#7F7F00",
"#007F7F",
]
return TAG_COLORS[random.randrange(0, len(TAG_COLORS))] return TAG_COLORS[random.randrange(0, len(TAG_COLORS))]
class BabyBuddyTag(TagBase): class BabyBuddyTag(TagBase):
class Meta: class Meta:
verbose_name = _("Tag") verbose_name = _("Tag")
@ -99,7 +116,7 @@ class BabyBuddyTag(TagBase):
"Color", "Color",
max_length=32, max_length=32,
default=random_color, default=random_color,
validators=[RegexValidator(r"^#[0-9A-F]{6}$")] validators=[RegexValidator(r"^#[0-9A-F]{6}$")],
) )
last_used = models.DateTimeField( last_used = models.DateTimeField(
@ -125,10 +142,12 @@ class BabyBuddyTaggableManager(TaggableManager):
""" """
Remove default help_text - only reason for this to exist. Remove default help_text - only reason for this to exist.
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs["help_text"] = kwargs.get("help_text") kwargs["help_text"] = kwargs.get("help_text")
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
class Child(models.Model): class Child(models.Model):
model_name = "child" model_name = "child"
first_name = models.CharField(max_length=255, verbose_name=_("First name")) first_name = models.CharField(max_length=255, verbose_name=_("First name"))
@ -299,6 +318,7 @@ class Feeding(models.Model):
validate_duration(self) validate_duration(self)
validate_unique_period(Feeding.objects.filter(child=self.child), self) validate_unique_period(Feeding.objects.filter(child=self.child), self)
class Note(models.Model): class Note(models.Model):
model_name = "note" model_name = "note"
child = models.ForeignKey( child = models.ForeignKey(

View File

@ -2,10 +2,12 @@ from django.forms import Media
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from django.forms import Widget from django.forms import Widget
class TagsEditor(Widget): class TagsEditor(Widget):
class Media: class Media:
js = ("babybuddy/js/tags_editor.js",) js = ("babybuddy/js/tags_editor.js",)
input_type = 'hidden' input_type = 'hidden'
template_name = 'core/widget_tag_editor.html' template_name = 'core/widget_tag_editor.html'