kopia lustrzana https://github.com/wagtail/wagtail
Split out common logic from get_value_data
rodzic
5f890f0acc
commit
39f7886a6f
|
@ -157,13 +157,39 @@ class BaseChooser(widgets.Input):
|
|||
self.get_context(name, value_data or {}, attrs),
|
||||
)
|
||||
|
||||
def get_instance(self, value):
|
||||
"""
|
||||
Given a value passed to this widget for rendering (which may be None, an id, or a model
|
||||
instance), return a model instance or None
|
||||
"""
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, self.model):
|
||||
return value
|
||||
else: # assume instance ID
|
||||
return self.model.objects.get(pk=value)
|
||||
|
||||
def get_value_data_from_instance(self, instance):
|
||||
"""
|
||||
Given a model instance, return a value that we can pass to both the server-side template
|
||||
and the client-side rendering code (via telepath) that contains all the information needed
|
||||
for display. Typically this is a dict of id, title etc; it must be JSON-serialisable.
|
||||
"""
|
||||
return {
|
||||
"id": instance.pk,
|
||||
"edit_url": AdminURLFinder().get_edit_url(instance),
|
||||
}
|
||||
|
||||
def get_value_data(self, value):
|
||||
# Perform any necessary preprocessing on the value passed to render() before it is passed
|
||||
# on to render_html / render_js_init. This is a good place to perform database lookups
|
||||
# that are needed by both render_html and render_js_init. Return value is arbitrary
|
||||
# (we only care that render_html / render_js_init can accept it), but will typically be
|
||||
# a dict of data needed for rendering: id, title etc.
|
||||
return value
|
||||
"""
|
||||
Given a value passed to this widget for rendering (which may be None, an id, or a model
|
||||
instance), return a value that we can pass to both the server-side template and the
|
||||
client-side rendering code (via telepath) that contains all the information needed
|
||||
for display. Typically this is a dict of id, title etc; it must be JSON-serialisable.
|
||||
"""
|
||||
instance = self.get_instance(value)
|
||||
if instance:
|
||||
return self.get_value_data_from_instance(instance)
|
||||
|
||||
def render(self, name, value, attrs=None, renderer=None):
|
||||
# no point trying to come up with sensible semantics for when 'id' is missing from attrs,
|
||||
|
@ -244,27 +270,21 @@ class AdminPageChooser(BaseChooser):
|
|||
"user_perms": self.user_perms,
|
||||
}
|
||||
|
||||
def get_value_data(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, Page):
|
||||
page = value.specific
|
||||
else: # assume page ID
|
||||
try:
|
||||
page = self.model.objects.get(pk=value)
|
||||
except self.model.DoesNotExist:
|
||||
return None
|
||||
def get_instance(self, value):
|
||||
instance = super().get_instance(value)
|
||||
if instance:
|
||||
return instance.specific
|
||||
|
||||
page = page.specific
|
||||
|
||||
edit_url = AdminURLFinder().get_edit_url(page)
|
||||
parent_page = page.get_parent()
|
||||
return {
|
||||
"id": page.pk,
|
||||
"display_title": page.get_admin_display_title(),
|
||||
"parent_id": parent_page.pk if parent_page else None,
|
||||
"edit_url": edit_url,
|
||||
}
|
||||
def get_value_data_from_instance(self, instance):
|
||||
data = super().get_value_data_from_instance(instance)
|
||||
parent_page = instance.get_parent()
|
||||
data.update(
|
||||
{
|
||||
"display_title": instance.get_admin_display_title(),
|
||||
"parent_id": parent_page.pk if parent_page else None,
|
||||
}
|
||||
)
|
||||
return data
|
||||
|
||||
def get_context(self, name, value_data, attrs):
|
||||
context = super().get_context(name, value_data, attrs)
|
||||
|
|
|
@ -4,7 +4,6 @@ from django import forms
|
|||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail.admin.admin_url_finder import AdminURLFinder
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.admin.widgets import BaseChooser
|
||||
from wagtail.models import Task
|
||||
|
@ -17,21 +16,10 @@ class AdminTaskChooser(BaseChooser):
|
|||
model = Task
|
||||
template_name = "wagtailadmin/workflows/widgets/task_chooser.html"
|
||||
|
||||
def get_value_data(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, self.model):
|
||||
instance = value
|
||||
else: # assume ID
|
||||
instance = self.model.objects.get(pk=value)
|
||||
|
||||
edit_url = AdminURLFinder().get_edit_url(instance)
|
||||
|
||||
return {
|
||||
"id": instance.pk,
|
||||
"title": instance.name,
|
||||
"edit_url": edit_url,
|
||||
}
|
||||
def get_value_data_from_instance(self, instance):
|
||||
data = super().get_value_data_from_instance(instance)
|
||||
data["title"] = instance.name
|
||||
return data
|
||||
|
||||
def get_context(self, name, value_data, attrs):
|
||||
context = super().get_context(name, value_data, attrs)
|
||||
|
|
|
@ -5,7 +5,6 @@ from django.urls import reverse
|
|||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail.admin.admin_url_finder import AdminURLFinder
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.admin.widgets import BaseChooser
|
||||
from wagtail.documents import get_document_model
|
||||
|
@ -22,21 +21,10 @@ class AdminDocumentChooser(BaseChooser):
|
|||
super().__init__(**kwargs)
|
||||
self.model = get_document_model()
|
||||
|
||||
def get_value_data(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, self.model):
|
||||
doc = value
|
||||
else: # assume document ID
|
||||
doc = self.model.objects.get(pk=value)
|
||||
|
||||
edit_url = AdminURLFinder().get_edit_url(doc)
|
||||
|
||||
return {
|
||||
"id": doc.pk,
|
||||
"title": doc.title,
|
||||
"edit_url": edit_url,
|
||||
}
|
||||
def get_value_data_from_instance(self, instance):
|
||||
data = super().get_value_data_from_instance(instance)
|
||||
data["title"] = instance.title
|
||||
return data
|
||||
|
||||
def get_context(self, name, value_data, attrs):
|
||||
context = super().get_context(name, value_data, attrs)
|
||||
|
|
|
@ -5,7 +5,6 @@ from django.urls import reverse
|
|||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail.admin.admin_url_finder import AdminURLFinder
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.admin.widgets import BaseChooser
|
||||
from wagtail.images import get_image_model
|
||||
|
@ -24,27 +23,20 @@ class AdminImageChooser(BaseChooser):
|
|||
super().__init__(**kwargs)
|
||||
self.model = get_image_model()
|
||||
|
||||
def get_value_data(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, self.model):
|
||||
image = value
|
||||
else: # assume image ID
|
||||
image = self.model.objects.get(pk=value)
|
||||
|
||||
preview_image = get_rendition_or_not_found(image, "max-165x165")
|
||||
edit_url = AdminURLFinder().get_edit_url(image)
|
||||
|
||||
return {
|
||||
"id": image.pk,
|
||||
"title": image.title,
|
||||
"preview": {
|
||||
"url": preview_image.url,
|
||||
"width": preview_image.width,
|
||||
"height": preview_image.height,
|
||||
},
|
||||
"edit_url": edit_url,
|
||||
}
|
||||
def get_value_data_from_instance(self, instance):
|
||||
data = super().get_value_data_from_instance(instance)
|
||||
preview_image = get_rendition_or_not_found(instance, "max-165x165")
|
||||
data.update(
|
||||
{
|
||||
"title": instance.title,
|
||||
"preview": {
|
||||
"url": preview_image.url,
|
||||
"width": preview_image.width,
|
||||
"height": preview_image.height,
|
||||
},
|
||||
}
|
||||
)
|
||||
return data
|
||||
|
||||
def get_context(self, name, value_data, attrs):
|
||||
context = super().get_context(name, value_data, attrs)
|
||||
|
|
|
@ -5,7 +5,6 @@ from django.urls import reverse
|
|||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail.admin.admin_url_finder import AdminURLFinder
|
||||
from wagtail.admin.staticfiles import versioned_static
|
||||
from wagtail.admin.widgets import BaseChooser
|
||||
from wagtail.admin.widgets.button import ListingButton
|
||||
|
@ -23,21 +22,10 @@ class AdminSnippetChooser(BaseChooser):
|
|||
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def get_value_data(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
elif isinstance(value, self.model):
|
||||
instance = value
|
||||
else: # assume instance ID
|
||||
instance = self.model.objects.get(pk=value)
|
||||
|
||||
edit_url = AdminURLFinder().get_edit_url(instance)
|
||||
|
||||
return {
|
||||
"id": instance.pk,
|
||||
"string": str(instance),
|
||||
"edit_url": edit_url,
|
||||
}
|
||||
def get_value_data_from_instance(self, instance):
|
||||
data = super().get_value_data_from_instance(instance)
|
||||
data["string"] = str(instance)
|
||||
return data
|
||||
|
||||
def get_context(self, name, value_data, attrs):
|
||||
context = super().get_context(name, value_data, attrs)
|
||||
|
|
Ładowanie…
Reference in New Issue