kopia lustrzana https://github.com/wagtail/wagtail
Implement generating a StreamField ChooserBlock from ChooserViewSet
rodzic
316493873f
commit
161dd69c8a
|
@ -59,6 +59,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
|
|||
.. autoattribute:: create_view_class
|
||||
.. autoattribute:: widget_class
|
||||
.. autoattribute:: register_widget
|
||||
.. autoattribute:: block_class
|
||||
.. autoattribute:: creation_form_class
|
||||
.. autoattribute:: form_fields
|
||||
.. autoattribute:: exclude_form_fields
|
||||
|
|
|
@ -6,6 +6,7 @@ from django.utils.translation import gettext as _
|
|||
from wagtail.admin.forms.models import register_form_field_override
|
||||
from wagtail.admin.views.generic import chooser as chooser_views
|
||||
from wagtail.admin.widgets.chooser import BaseChooser
|
||||
from wagtail.blocks import ChooserBlock
|
||||
|
||||
from .base import ViewSet
|
||||
|
||||
|
@ -44,6 +45,9 @@ class ChooserViewSet(ViewSet):
|
|||
#: The base Widget class that the chooser widget will be derived from.
|
||||
base_widget_class = BaseChooser
|
||||
|
||||
#: The base ChooserBlock class that the StreamField chooser block will be derived from.
|
||||
base_block_class = ChooserBlock
|
||||
|
||||
#: Defaults to True; if False, the chooser widget will not automatically be registered for use in admin forms.
|
||||
register_widget = True
|
||||
|
||||
|
@ -125,6 +129,13 @@ class ChooserViewSet(ViewSet):
|
|||
permission_policy=self.permission_policy,
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def model_name(self):
|
||||
if isinstance(self.model, str):
|
||||
return self.model.split(".")[-1]
|
||||
else:
|
||||
return self.model.__name__
|
||||
|
||||
@cached_property
|
||||
def widget_class(self):
|
||||
"""
|
||||
|
@ -152,6 +163,28 @@ class ChooserViewSet(ViewSet):
|
|||
},
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def block_class(self):
|
||||
"""
|
||||
Returns a StreamField ChooserBlock class using this chooser.
|
||||
"""
|
||||
meta = type(
|
||||
"Meta",
|
||||
(self.base_block_class._meta_class,),
|
||||
{
|
||||
"icon": self.icon,
|
||||
},
|
||||
)
|
||||
return type(
|
||||
"%sChooserBlock" % self.model_name,
|
||||
(self.base_block_class,),
|
||||
{
|
||||
"target_model": self.model,
|
||||
"widget": self.widget_class(),
|
||||
"Meta": meta,
|
||||
},
|
||||
)
|
||||
|
||||
def get_urlpatterns(self):
|
||||
return super().get_urlpatterns() + [
|
||||
path("", self.choose_view, name="choose"),
|
||||
|
|
Ładowanie…
Reference in New Issue