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

Update SummaryItem to use components

This commit is contained in:
Matt Westcott 2021-08-10 19:54:28 +01:00 committed by Matt Westcott
parent a2c0ca346f
commit 0fbf2fff30
2 changed files with 51 additions and 3 deletions

View File

@ -1,22 +1,70 @@
from django.template.loader import render_to_string
from warnings import warn
from django.template.loader import get_template, render_to_string
from wagtail.admin.auth import user_has_any_page_permission
from wagtail.admin.navigation import get_site_for_user
from wagtail.admin.ui.components import Component
from wagtail.core import hooks
from wagtail.core.models import Page, Site
from wagtail.utils.deprecation import RemovedInWagtail217Warning
class SummaryItem:
class SummaryItem(Component):
order = 100
template = None # RemovedInWagtail217Warning - should set template_name instead
def __init__(self, request):
self.request = request
def get_context(self):
# RemovedInWagtail217Warning:
# old get_context method deprecated in 2.15; provided here in case subclasses call it from
# overridden render() methods or via super()
return {}
get_context.is_base_method = True
def render(self):
# RemovedInWagtail217Warning:
# old render method deprecated in 2.15; provided here in case subclasses call it via super()
return render_to_string(self.template, self.get_context(), request=self.request)
render.is_base_method = True
def render_html(self, parent_context=None):
if not getattr(self.render, 'is_base_method', False):
# this SummaryItem subclass has overridden render() - use their implementation in
# preference to following the Component.render_html path
message = (
"Summary item %r registered with construct_homepage_summary_items must be updated "
"to override render_html(self, parent_context) rather than render(self)"
% self
)
warn(message, category=RemovedInWagtail217Warning)
return self.render()
elif not getattr(self.get_context, 'is_base_method', False):
# this SummaryItem subclass has overridden get_context() - use their implementation in
# preference to Component.get_context_data
message = (
"Summary item %r registered with construct_homepage_summary_items must be updated "
"to override get_context_data(self, parent_context) rather than get_context(self)"
% self
)
warn(message, category=RemovedInWagtail217Warning)
context_data = self.get_context()
else:
context_data = self.get_context_data(parent_context)
if self.template is not None:
warn(
"%s should define template_name instead of template" % type(self).__name__,
category=RemovedInWagtail217Warning
)
template_name = self.template
else:
template_name = self.template_name
template = get_template(template_name)
return template.render(context_data)
def is_shown(self):
return True

View File

@ -3,7 +3,7 @@
<h2 class="visuallyhidden">{% trans "Site summary" %}</h2>
<ul class="stats">
{% for item in summary_items %}
{{ item.render }}
{% component item %}
{% endfor %}
</ul>
</section>