117 lines
3.0 KiB
Python
117 lines
3.0 KiB
Python
from django import forms
|
|
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
|
|
from .models import Box, BoxType, Facet, Tag, Thing, ThingFile, ThingLink
|
|
|
|
|
|
@admin.register(BoxType)
|
|
class BoxTypeAdmin(admin.ModelAdmin):
|
|
"""Admin configuration for BoxType model."""
|
|
|
|
list_display = ('name', 'width', 'height', 'length')
|
|
search_fields = ('name',)
|
|
|
|
|
|
@admin.register(Box)
|
|
class BoxAdmin(admin.ModelAdmin):
|
|
"""Admin configuration for Box model."""
|
|
|
|
list_display = ('id', 'box_type')
|
|
list_filter = ('box_type',)
|
|
search_fields = ('id',)
|
|
|
|
|
|
class ThingFileInline(admin.TabularInline):
|
|
"""Inline admin for Thing files."""
|
|
|
|
model = ThingFile
|
|
extra = 1
|
|
fields = ('title', 'file')
|
|
|
|
|
|
class ThingLinkInline(admin.TabularInline):
|
|
"""Inline admin for Thing links."""
|
|
|
|
model = ThingLink
|
|
extra = 1
|
|
fields = ('title', 'url')
|
|
|
|
|
|
class ThingAdmin(admin.ModelAdmin):
|
|
"""Admin configuration for Thing model."""
|
|
|
|
list_display = ('name', 'box')
|
|
list_filter = ('box', 'tags')
|
|
search_fields = ('name', 'description')
|
|
filter_horizontal = ('tags',)
|
|
inlines = [ThingFileInline, ThingLinkInline]
|
|
|
|
|
|
admin.site.register(Thing, ThingAdmin)
|
|
|
|
|
|
@admin.register(ThingFile)
|
|
class ThingFileAdmin(admin.ModelAdmin):
|
|
"""Admin configuration for ThingFile model."""
|
|
|
|
list_display = ('thing', 'title', 'uploaded_at')
|
|
list_filter = ('thing',)
|
|
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."""
|
|
|
|
list_display = ('thing', 'title', 'url', 'uploaded_at')
|
|
list_filter = ('thing',)
|
|
search_fields = ('title', 'url')
|