Add color widget to the admin-UI

This commit is contained in:
Paul Konstantin Gerke 2022-03-08 12:55:50 +01:00
parent 98b0072634
commit b431c27835
4 changed files with 31 additions and 24 deletions

View File

@ -6,6 +6,7 @@ from import_export import fields, resources
from import_export.admin import ImportExportMixin, ExportActionMixin from import_export.admin import ImportExportMixin, ExportActionMixin
from core import models from core import models
from core.forms import TagAdminForm
class ImportExportResourceBase(resources.ModelResource): class ImportExportResourceBase(resources.ModelResource):
@ -185,6 +186,7 @@ class TaggedItemInline(admin.StackedInline):
@admin.register(models.Tag) @admin.register(models.Tag)
class TagAdmin(admin.ModelAdmin): class TagAdmin(admin.ModelAdmin):
form = TagAdminForm
inlines = [TaggedItemInline] inlines = [TaggedItemInline]
list_display = ["name", "slug", "color", "last_used"] list_display = ["name", "slug", "color", "last_used"]
ordering = ["name", "slug"] ordering = ["name", "slug"]

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django import forms from django import forms
from django.forms import widgets
from django.conf import settings 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 _
@ -313,3 +314,8 @@ class BMIForm(CoreModelForm):
), ),
"notes": forms.Textarea(attrs={"rows": 5}), "notes": forms.Textarea(attrs={"rows": 5}),
} }
class TagAdminForm(forms.ModelForm):
class Meta:
widgets = {"color": widgets.TextInput(attrs={"type": "color"})}

View File

@ -1,4 +1,4 @@
# Generated by Django 4.0.3 on 2022-03-05 12:00 # Generated by Django 4.0.3 on 2022-03-08 11:51
import core.models import core.models
import django.core.validators import django.core.validators
@ -41,7 +41,7 @@ class Migration(migrations.Migration):
default=core.models.random_color, default=core.models.random_color,
max_length=32, max_length=32,
validators=[ validators=[
django.core.validators.RegexValidator("^#[0-9A-F]{6}$") django.core.validators.RegexValidator("^#[0-9a-fA-F]{6}$")
], ],
verbose_name="Color", verbose_name="Color",
), ),
@ -54,8 +54,7 @@ class Migration(migrations.Migration):
), ),
], ],
options={ options={
"verbose_name": "Tag", "verbose_name": "Tags",
"verbose_name_plural": "Tags",
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -102,7 +101,7 @@ class Migration(migrations.Migration):
name="tags", name="tags",
field=core.models.TaggableManager( field=core.models.TaggableManager(
blank=True, blank=True,
help_text=None, help_text="Click on the tags to add (+) or remove (-) tags or use the text editor to create new tags.",
through="core.Tagged", through="core.Tagged",
to="core.Tag", to="core.Tag",
verbose_name="Tags", verbose_name="Tags",

View File

@ -82,24 +82,24 @@ def validate_time(time, field_name):
def random_color(): def random_color():
TAG_COLORS = [ TAG_COLORS = [
"#FF0000", "#ff0000",
"#00FF00", "#00ff00",
"#0000FF", "#0000ff",
"#FF00FF", "#ff00ff",
"#FFFF00", "#ffff00",
"#00FFFF", "#00ffff",
"#FF7F7F", "#ff7f7f",
"#7FFF7F", "#7fff7f",
"#7F7FFF", "#7f7fff",
"#FF7FFF", "#ff7fff",
"#FFFF7F", "#ffff7f",
"#7FFFFF", "#7fffff",
"#7F0000", "#7f0000",
"#007F00", "#007f00",
"#00007F", "#00007f",
"#7F007F", "#7f007f",
"#7F7F00", "#7f7f00",
"#007F7F", "#007f7f",
] ]
return TAG_COLORS[random.randrange(0, len(TAG_COLORS))] return TAG_COLORS[random.randrange(0, len(TAG_COLORS))]
@ -112,7 +112,7 @@ class Tag(TagBase):
verbose_name=_("Color"), verbose_name=_("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-fA-F]{6}$")],
) )
last_used = models.DateTimeField( last_used = models.DateTimeField(