0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Refactor choosers to not pass model instances to templates

Passing the model instance means that logic such as rendering thumbnails and generating URLs to edit pages ends up on the template. This is bad because we're going to want to populate these widgets through mechanisms other than rendering the template (e.g. setState on the widget's Javascript API), and that would end up duplicating the logic on the template.
This commit is contained in:
Matt Westcott 2020-12-12 03:36:02 +00:00 committed by Karl Hobley
parent d401d6dbf6
commit d2ccd0eb84
11 changed files with 45 additions and 39 deletions

View File

@ -9,7 +9,9 @@
<div id="{{ attrs.id }}-chooser" class="chooser {% block chooser_class %}page-chooser{% endblock %} {% if not value %}blank{% endif %}" {% block chooser_attributes %}{% endblock %}>
<div class="chosen">
{% block chosen_state_view %}{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ display_title }}</span>
{% endblock %}
<ul class="actions">
{% if not widget.is_required and widget.show_clear_link %}
@ -19,7 +21,7 @@
{% if widget.show_edit_link %}
<li>
{% block edit_link %}
<a href="{% block edit_chosen_item_url %}#{% endblock %}" class="edit-link button button-small button-secondary" target="_blank" rel="noopener noreferrer">{{ widget.link_to_chosen_text }}</a>
<a href="{% block edit_chosen_item_url %}{{ edit_url }}{% endblock %}" class="edit-link button button-small button-secondary" target="_blank" rel="noopener noreferrer">{{ widget.link_to_chosen_text }}</a>
{% endblock %}
</li>
{% endif %}

View File

@ -1,8 +1,2 @@
{% extends "wagtailadmin/widgets/chooser.html" %}
{% block chooser_attributes %}data-chooser-url="{% url "wagtailadmin_choose_page" %}"{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ page.specific.get_admin_display_title }}</span>
{% endblock %}
{% block edit_chosen_item_url %}{% if page %}{% url 'wagtailadmin_pages:edit' page.id %}{% endif %}{% endblock %}

View File

@ -3,7 +3,5 @@
{% block chooser_attributes %}data-chooser-url="{% url "wagtailadmin_workflows:task_chooser" %}"{% endblock %}
{% block chosen_state_view %}
<span class="name">{{ task.name }}</span>
<span class="name">{{ display_title }}</span>
{% endblock %}
{% block edit_chosen_item_url %}{% if task %}{% url 'wagtailadmin_workflows:edit_task' task.id %}{% endif %}{% endblock %}

View File

@ -3,6 +3,7 @@ import json
from django import forms
from django.forms import widgets
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.admin.staticfiles import versioned_static
@ -117,7 +118,7 @@ class AdminPageChooser(AdminChooser):
def render_html(self, name, value, attrs):
model_class = self._get_lowest_common_page_class()
instance, value = self.get_instance_and_id(model_class, value)
page, value = self.get_instance_and_id(model_class, value)
original_field_html = super().render_html(name, value, attrs)
@ -126,7 +127,8 @@ class AdminPageChooser(AdminChooser):
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'page': instance,
'display_title': page.specific.get_admin_display_title() if page else '',
'edit_url': reverse('wagtailadmin_pages:edit', args=[page.id]) if page else '',
})
def render_js_init(self, id_, name, value):

View File

@ -2,6 +2,7 @@ import json
from django import forms
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.admin.staticfiles import versioned_static
@ -15,7 +16,7 @@ class AdminTaskChooser(AdminChooser):
link_to_chosen_text = _('Edit this task')
def render_html(self, name, value, attrs):
instance, value = self.get_instance_and_id(Task, value)
task, value = self.get_instance_and_id(Task, value)
original_field_html = super().render_html(name, value, attrs)
return render_to_string("wagtailadmin/workflows/widgets/task_chooser.html", {
@ -23,7 +24,8 @@ class AdminTaskChooser(AdminChooser):
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'task': instance,
'display_title': task.name if task else '',
'edit_url': reverse('wagtailadmin_workflows:edit_task', args=[task.id]) if task else '',
})
def render_js_init(self, id_, name, value):

View File

@ -1,9 +1,3 @@
{% extends "wagtailadmin/widgets/chooser.html" %}
{% block chooser_class %}document-chooser{% endblock %}
{% block chooser_attributes %}data-chooser-url="{% url "wagtaildocs:chooser" %}"{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ document.title }}</span>
{% endblock %}
{% block edit_chosen_item_url %}{% if document %}{% url 'wagtaildocs:edit' document.id %}{% endif %}{% endblock %}

View File

@ -2,6 +2,7 @@ import json
from django import forms
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.admin.staticfiles import versioned_static
@ -19,7 +20,7 @@ class AdminDocumentChooser(AdminChooser):
self.document_model = get_document_model()
def render_html(self, name, value, attrs):
instance, value = self.get_instance_and_id(self.document_model, value)
document, value = self.get_instance_and_id(self.document_model, value)
original_field_html = super().render_html(name, value, attrs)
return render_to_string("wagtaildocs/widgets/document_chooser.html", {
@ -27,7 +28,8 @@ class AdminDocumentChooser(AdminChooser):
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'document': instance,
'display_title': document.title if document else '',
'edit_url': reverse('wagtaildocs:edit', args=[document.id]) if document else '',
})
def render_js_init(self, id_, name, value):

View File

@ -6,12 +6,6 @@
{% block chosen_state_view %}
<div class="preview-image">
{% if image %}
{% image image max-165x165 class="show-transparency" title=image.title %}
{% else %}
<img alt="">
{% endif %}
<img alt="{{ title }}" class="show-transparency" height="{{ preview.height }}" src="{{ preview.url }}" title="{{ title }}" width="{{ preview.width }}">
</div>
{% endblock %}
{% block edit_chosen_item_url %}{% if image %}{% url 'wagtailimages:edit' image.id %}{% endif %}{% endblock %}

View File

@ -2,11 +2,13 @@ import json
from django import forms
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.admin.staticfiles import versioned_static
from wagtail.admin.widgets import AdminChooser
from wagtail.images import get_image_model
from wagtail.images.shortcuts import get_rendition_or_not_found
class AdminImageChooser(AdminChooser):
@ -19,15 +21,26 @@ class AdminImageChooser(AdminChooser):
self.image_model = get_image_model()
def render_html(self, name, value, attrs):
instance, value = self.get_instance_and_id(self.image_model, value)
image, value = self.get_instance_and_id(self.image_model, value)
original_field_html = super().render_html(name, value, attrs)
if image:
preview_image = get_rendition_or_not_found(image, 'max-165x165')
else:
preview_image = None
return render_to_string("wagtailimages/widgets/image_chooser.html", {
'widget': self,
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'image': instance,
'title': image.title if image else '',
'preview': {
'url': preview_image.url,
'width': preview_image.width,
'height': preview_image.height,
} if preview_image else {},
'edit_url': reverse('wagtailimages:edit', args=[image.id]) if image else '',
})
def render_js_init(self, id_, name, value):

View File

@ -3,9 +3,3 @@
{% block chooser_class %}snippet-chooser{% endblock %}
{% block chooser_attributes %}data-chooser-url="{% url 'wagtailsnippets:choose_generic' %}"{% endblock %}
{% block chosen_state_view %}
<span class="title">{{ item }}</span>
{% endblock %}
{% block edit_chosen_item_url %}{% if item %}{% url 'wagtailsnippets:edit' model_opts.app_label model_opts.model_name item.pk|admin_urlquote %}{% endif %}{% endblock %}

View File

@ -1,7 +1,9 @@
import json
from django import forms
from django.contrib.admin.utils import quote
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.admin.staticfiles import versioned_static
@ -25,13 +27,22 @@ class AdminSnippetChooser(AdminChooser):
original_field_html = super().render_html(name, value, attrs)
if instance:
app_label = self.target_model._meta.app_label
model_name = self.target_model._meta.model_name
quoted_id = quote(instance.pk)
edit_url = reverse('wagtailsnippets:edit', args=[app_label, model_name, quoted_id])
else:
edit_url = ''
return render_to_string("wagtailsnippets/widgets/snippet_chooser.html", {
'widget': self,
'model_opts': self.target_model._meta,
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
'item': instance,
'display_title': str(instance) if instance else '',
'edit_url': edit_url,
})
def render_js_init(self, id_, name, value):