kopia lustrzana https://github.com/wagtail/wagtail
Create generic field display registry to render model fields in the admin
rodzic
0bebe532e8
commit
80f9bbf279
wagtail
admin/ui
documents
images
|
@ -0,0 +1,40 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import models
|
||||
|
||||
from wagtail.admin.ui.components import Component
|
||||
from wagtail.utils.registry import ModelFieldRegistry
|
||||
|
||||
display_class_registry = ModelFieldRegistry()
|
||||
|
||||
|
||||
def register_display_class(field_class, to=None, display_class=None, exact_class=False):
|
||||
"""
|
||||
Define how model field values should be rendered in the admin.
|
||||
The `display_class` should be a subclass of `wagtail.admin.ui.components.Component`
|
||||
that takes a single argument in its constructor: the value of the field.
|
||||
|
||||
This is mainly useful for defining how fields are rendered in the inspect view,
|
||||
but it can also be used in other places, e.g. listing views.
|
||||
"""
|
||||
|
||||
if display_class is None:
|
||||
raise ImproperlyConfigured(
|
||||
"register_display_class must be passed a 'display_class' keyword argument"
|
||||
)
|
||||
|
||||
if to and field_class != models.ForeignKey:
|
||||
raise ImproperlyConfigured(
|
||||
"The 'to' argument on register_display_class is only valid for ForeignKey fields"
|
||||
)
|
||||
|
||||
display_class_registry.register(
|
||||
field_class, to=to, value=display_class, exact_class=exact_class
|
||||
)
|
||||
|
||||
|
||||
class BaseFieldDisplay(Component):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def get_context_data(self, parent_context):
|
||||
return {"value": self.value}
|
|
@ -1,6 +1,9 @@
|
|||
from django.apps import AppConfig
|
||||
from django.db.models import ForeignKey
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from . import get_document_model
|
||||
|
||||
|
||||
class WagtailDocsAppConfig(AppConfig):
|
||||
name = "wagtail.documents"
|
||||
|
@ -13,7 +16,14 @@ class WagtailDocsAppConfig(AppConfig):
|
|||
|
||||
register_signal_handlers()
|
||||
|
||||
from wagtail.documents import get_document_model
|
||||
Document = get_document_model()
|
||||
|
||||
from wagtail.admin.ui.fields import register_display_class
|
||||
|
||||
from .components import DocumentDisplay
|
||||
|
||||
register_display_class(ForeignKey, to=Document, display_class=DocumentDisplay)
|
||||
|
||||
from wagtail.models.reference_index import ReferenceIndex
|
||||
|
||||
ReferenceIndex.register_model(get_document_model())
|
||||
ReferenceIndex.register_model(Document)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
from wagtail.admin.ui.fields import BaseFieldDisplay
|
||||
|
||||
|
||||
class DocumentDisplay(BaseFieldDisplay):
|
||||
template_name = "wagtaildocs/components/document_display.html"
|
|
@ -0,0 +1 @@
|
|||
<a href="{{ value.url }}">{{ value.title }} <span class="meta">({{ value.file_extension|upper }}, {{ value.file.size|filesizeformat }})</span></a>
|
|
@ -36,6 +36,12 @@ class WagtailImagesAppConfig(AppConfig):
|
|||
ForeignKey, to=Image, comparison_class=ImageFieldComparison
|
||||
)
|
||||
|
||||
from wagtail.admin.ui.fields import register_display_class
|
||||
|
||||
from .components import ImageDisplay
|
||||
|
||||
register_display_class(ForeignKey, to=Image, display_class=ImageDisplay)
|
||||
|
||||
from wagtail.models.reference_index import ReferenceIndex
|
||||
|
||||
ReferenceIndex.register_model(get_image_model())
|
||||
ReferenceIndex.register_model(Image)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
from wagtail.admin.ui.fields import BaseFieldDisplay
|
||||
from wagtail.images.shortcuts import get_rendition_or_not_found
|
||||
|
||||
|
||||
class ImageDisplay(BaseFieldDisplay):
|
||||
rendition_spec = "max-400x400"
|
||||
|
||||
def render_html(self, parent_context):
|
||||
return get_rendition_or_not_found(self.value, self.rendition_spec).img_tag()
|
Ładowanie…
Reference in New Issue