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

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django import forms
from django.forms import widgets
from django.conf import settings
from django.utils import timezone
from django.utils.translation import gettext as _
@ -313,3 +314,8 @@ class BMIForm(CoreModelForm):
),
"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 django.core.validators
@ -41,7 +41,7 @@ class Migration(migrations.Migration):
default=core.models.random_color,
max_length=32,
validators=[
django.core.validators.RegexValidator("^#[0-9A-F]{6}$")
django.core.validators.RegexValidator("^#[0-9a-fA-F]{6}$")
],
verbose_name="Color",
),
@ -54,8 +54,7 @@ class Migration(migrations.Migration):
),
],
options={
"verbose_name": "Tag",
"verbose_name_plural": "Tags",
"verbose_name": "Tags",
},
),
migrations.CreateModel(
@ -102,7 +101,7 @@ class Migration(migrations.Migration):
name="tags",
field=core.models.TaggableManager(
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",
to="core.Tag",
verbose_name="Tags",

View File

@ -82,24 +82,24 @@ def validate_time(time, field_name):
def random_color():
TAG_COLORS = [
"#FF0000",
"#00FF00",
"#0000FF",
"#FF00FF",
"#FFFF00",
"#00FFFF",
"#FF7F7F",
"#7FFF7F",
"#7F7FFF",
"#FF7FFF",
"#FFFF7F",
"#7FFFFF",
"#7F0000",
"#007F00",
"#00007F",
"#7F007F",
"#7F7F00",
"#007F7F",
"#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))]
@ -112,7 +112,7 @@ class Tag(TagBase):
verbose_name=_("Color"),
max_length=32,
default=random_color,
validators=[RegexValidator(r"^#[0-9A-F]{6}$")],
validators=[RegexValidator(r"^#[0-9a-fA-F]{6}$")],
)
last_used = models.DateTimeField(