Complete replacement of Thing types with tag system
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django_mptt_admin.admin import DjangoMpttAdmin
|
||||
from django.utils.html import format_html
|
||||
|
||||
from .models import Box, BoxType, Thing, ThingFile, ThingLink, ThingType
|
||||
from .models import Box, BoxType, Facet, Tag, Thing, ThingFile, ThingLink
|
||||
|
||||
|
||||
@admin.register(BoxType)
|
||||
@@ -21,13 +22,6 @@ class BoxAdmin(admin.ModelAdmin):
|
||||
search_fields = ('id',)
|
||||
|
||||
|
||||
@admin.register(ThingType)
|
||||
class ThingTypeAdmin(DjangoMpttAdmin):
|
||||
"""Admin configuration for ThingType model."""
|
||||
|
||||
search_fields = ('name',)
|
||||
|
||||
|
||||
class ThingFileInline(admin.TabularInline):
|
||||
"""Inline admin for Thing files."""
|
||||
|
||||
@@ -47,9 +41,10 @@ class ThingLinkInline(admin.TabularInline):
|
||||
class ThingAdmin(admin.ModelAdmin):
|
||||
"""Admin configuration for Thing model."""
|
||||
|
||||
list_display = ('name', 'thing_type', 'box')
|
||||
list_filter = ('thing_type', 'box')
|
||||
list_display = ('name', 'box')
|
||||
list_filter = ('box', 'tags')
|
||||
search_fields = ('name', 'description')
|
||||
filter_horizontal = ('tags',)
|
||||
inlines = [ThingFileInline, ThingLinkInline]
|
||||
|
||||
|
||||
@@ -65,6 +60,53 @@ class ThingFileAdmin(admin.ModelAdmin):
|
||||
search_fields = ('title',)
|
||||
|
||||
|
||||
class ColorInput(forms.TextInput):
|
||||
"""Color picker widget using HTML5 color input."""
|
||||
|
||||
input_type = 'color'
|
||||
|
||||
|
||||
class FacetAdminForm(forms.ModelForm):
|
||||
"""Form for Facet model with color picker widget."""
|
||||
|
||||
class Meta:
|
||||
model = Facet
|
||||
fields = '__all__'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['color'].widget = ColorInput(attrs={'type': 'color', 'style': 'height: 50px; width: 100px;'})
|
||||
|
||||
|
||||
@admin.register(Facet)
|
||||
class FacetAdmin(admin.ModelAdmin):
|
||||
"""Admin configuration for Facet model."""
|
||||
|
||||
form = FacetAdminForm
|
||||
list_display = ('name', 'color_preview', 'cardinality')
|
||||
search_fields = ('name',)
|
||||
prepopulated_fields = {'slug': ('name',)}
|
||||
list_filter = ('cardinality',)
|
||||
|
||||
def color_preview(self, obj):
|
||||
return format_html('<span style="color: {}; font-weight: bold;">■ {}</span>', obj.color, obj.color)
|
||||
|
||||
|
||||
@admin.register(Tag)
|
||||
class TagAdmin(admin.ModelAdmin):
|
||||
"""Admin configuration for Tag model."""
|
||||
|
||||
list_display = ('__str__', 'facet_with_color')
|
||||
list_filter = ('facet',)
|
||||
search_fields = ('name', 'facet__name')
|
||||
|
||||
def facet_with_color(self, obj):
|
||||
if obj.facet:
|
||||
return format_html('<span style="color: {}; font-weight: bold;">{}</span>', obj.facet.color, obj.facet.name)
|
||||
return '-'
|
||||
facet_with_color.short_description = 'Facet'
|
||||
|
||||
|
||||
@admin.register(ThingLink)
|
||||
class ThingLinkAdmin(admin.ModelAdmin):
|
||||
"""Admin configuration for ThingLink model."""
|
||||
|
||||
Reference in New Issue
Block a user