This commit introduces support for uploading optional video files to products and image files to blog posts. Enhanced admin interfaces were added to preview these files directly. Also includes adjustments to GraphQL types and serializers to expose the new fields.
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
from django.contrib.admin import register
|
|
from django.db.models import TextField
|
|
from django.utils.html import format_html
|
|
from django.utils.translation import gettext_lazy as _
|
|
from unfold.admin import ModelAdmin
|
|
from unfold_markdown import MarkdownWidget
|
|
|
|
from engine.blog.models import Post, PostTag
|
|
from engine.core.admin import ActivationActionsMixin, FieldsetsMixin
|
|
|
|
|
|
@register(Post)
|
|
class PostAdmin(FieldsetsMixin, ActivationActionsMixin, ModelAdmin):
|
|
list_display = ("title", "author", "slug", "created", "modified")
|
|
list_filter = ("author", "tags", "created", "modified")
|
|
search_fields = ("title", "content", "slug")
|
|
filter_horizontal = ("tags",)
|
|
date_hierarchy = "created"
|
|
autocomplete_fields = ("tags",)
|
|
formfield_overrides = {TextField: {"widget": MarkdownWidget}}
|
|
readonly_fields = (
|
|
"uuid",
|
|
"slug",
|
|
"modified",
|
|
"created",
|
|
"blogpic_preview",
|
|
)
|
|
|
|
general_fields = [
|
|
"title",
|
|
"content",
|
|
"file",
|
|
"blogpic",
|
|
"blogpic_preview",
|
|
]
|
|
relation_fields = [
|
|
"author",
|
|
"tags",
|
|
]
|
|
|
|
def blogpic_preview(self, obj: Post):
|
|
if obj.blogpic:
|
|
return format_html(
|
|
'<img src="{}" style="max-width:400px;max-height:300px;object-fit:contain">',
|
|
obj.blogpic.url,
|
|
)
|
|
return "—"
|
|
|
|
blogpic_preview.short_description = _("picture preview") # ty:ignore[unresolved-attribute]
|
|
|
|
|
|
@register(PostTag)
|
|
class PostTagAdmin(ModelAdmin):
|
|
list_display = ("tag_name", "name")
|
|
search_fields = ("tag_name", "name")
|
|
ordering = ("tag_name",)
|