mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
implement SnippetChooserBlock
This commit is contained in:
parent
dfb2a999f4
commit
85902d111d
17
wagtail/wagtailsnippets/blocks.py
Normal file
17
wagtail/wagtailsnippets/blocks.py
Normal file
@ -0,0 +1,17 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.utils.functional import cached_property
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from wagtail.wagtailadmin.blocks import ChooserBlock
|
||||
|
||||
class SnippetChooserBlock(ChooserBlock):
|
||||
def __init__(self, target_model, **kwargs):
|
||||
super(SnippetChooserBlock, self).__init__(**kwargs)
|
||||
self.target_model = target_model
|
||||
|
||||
@cached_property
|
||||
def widget(self):
|
||||
from wagtail.wagtailsnippets.widgets import AdminSnippetChooser
|
||||
content_type = ContentType.objects.get_for_model(self.target_model)
|
||||
return AdminSnippetChooser(content_type)
|
@ -22,10 +22,10 @@ class AdminSnippetChooser(AdminChooser):
|
||||
self.target_content_type = content_type
|
||||
|
||||
def render_html(self, name, value, attrs):
|
||||
original_field_html = super(AdminSnippetChooser, self).render_html(name, value, attrs)
|
||||
|
||||
model_class = self.target_content_type.model_class()
|
||||
instance = self.get_instance(model_class, value)
|
||||
instance, value = self.get_instance_and_id(model_class, value)
|
||||
|
||||
original_field_html = super(AdminSnippetChooser, self).render_html(name, value, attrs)
|
||||
|
||||
return render_to_string("wagtailsnippets/widgets/snippet_chooser.html", {
|
||||
'widget': self,
|
||||
|
Loading…
Reference in New Issue
Block a user