From 5cc28dc752c3ae78456bb835c3ba195489fc26d7 Mon Sep 17 00:00:00 2001 From: Raffaele Salmaso Date: Thu, 12 Jan 2017 17:06:00 +0100 Subject: Fixed #27728 -- Allowed overriding admin templatetags templates. --- tests/admin_changelist/tests.py | 10 ++--- .../admin/admin_views/article/actions.html | 6 +++ .../article/change_form_object_tools.html | 7 ++++ .../article/change_list_object_tools.html | 7 ++++ .../admin_views/article/change_list_results.html | 38 ++++++++++++++++++ .../admin/admin_views/article/date_hierarchy.html | 9 +++++ .../admin/admin_views/article/pagination.html | 12 ++++++ .../article/prepopulated_fields_js.html | 7 ++++ .../admin/admin_views/article/search_form.html | 16 ++++++++ .../admin/admin_views/article/submit_line.html | 7 ++++ tests/admin_views/test_templatetags.py | 45 +++++++++++++++++++++- 11 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 tests/admin_views/templates/admin/admin_views/article/actions.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/change_form_object_tools.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/change_list_object_tools.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/change_list_results.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/date_hierarchy.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/pagination.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/prepopulated_fields_js.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/search_form.html create mode 100644 tests/admin_views/templates/admin/admin_views/article/submit_line.html (limited to 'tests') diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index 5be70ee6ad..f37e3fa039 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -119,7 +119,7 @@ class ChangeListTests(TestCase): cl = m.get_changelist_instance(request) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') - context = Context({'cl': cl}) + context = Context({'cl': cl, 'opts': Child._meta}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) row_html = build_tbody_html(new_child.id, link, '-') @@ -137,7 +137,7 @@ class ChangeListTests(TestCase): cl = m.get_changelist_instance(request) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') - context = Context({'cl': cl}) + context = Context({'cl': cl, 'opts': Child._meta}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) row_html = build_tbody_html(new_child.id, link, '???') @@ -153,7 +153,7 @@ class ChangeListTests(TestCase): cl = m.get_changelist_instance(request) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') - context = Context({'cl': cl}) + context = Context({'cl': cl, 'opts': Child._meta}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) row_html = build_tbody_html( @@ -176,7 +176,7 @@ class ChangeListTests(TestCase): cl = m.get_changelist_instance(request) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') - context = Context({'cl': cl}) + context = Context({'cl': cl, 'opts': Child._meta}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) row_html = build_tbody_html(new_child.id, link, '%s' % new_parent) @@ -204,7 +204,7 @@ class ChangeListTests(TestCase): FormSet = m.get_changelist_formset(request) cl.formset = FormSet(queryset=cl.result_list) template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') - context = Context({'cl': cl}) + context = Context({'cl': cl, 'opts': Child._meta}) table_output = template.render(context) # make sure that hidden fields are in the correct place hiddenfields_div = ( diff --git a/tests/admin_views/templates/admin/admin_views/article/actions.html b/tests/admin_views/templates/admin/admin_views/article/actions.html new file mode 100644 index 0000000000..9aa238fd2a --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/actions.html @@ -0,0 +1,6 @@ +{% extends "admin/actions.html" %} +{% load i18n %} + +{% block actions-submit %} + +{% endblock %} diff --git a/tests/admin_views/templates/admin/admin_views/article/change_form_object_tools.html b/tests/admin_views/templates/admin/admin_views/article/change_form_object_tools.html new file mode 100644 index 0000000000..609974bb95 --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/change_form_object_tools.html @@ -0,0 +1,7 @@ +{% extends "admin/change_form_object_tools.html" %} +{% load i18n admin_urls %} + +{% block object-tools-items %} +
  • {% trans "Export" %}
  • +{{ block.super }} +{% endblock %} diff --git a/tests/admin_views/templates/admin/admin_views/article/change_list_object_tools.html b/tests/admin_views/templates/admin/admin_views/article/change_list_object_tools.html new file mode 100644 index 0000000000..1a9be91952 --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/change_list_object_tools.html @@ -0,0 +1,7 @@ +{% extends "admin/change_list_object_tools.html" %} +{% load i18n admin_urls %} + +{% block object-tools-items %} +
  • {% trans "Export" %}
  • +{{ block.super }} +{% endblock %} diff --git a/tests/admin_views/templates/admin/admin_views/article/change_list_results.html b/tests/admin_views/templates/admin/admin_views/article/change_list_results.html new file mode 100644 index 0000000000..ceb581f8ef --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/change_list_results.html @@ -0,0 +1,38 @@ +{% load i18n static %} +{% if result_hidden_fields %} +
    {# DIV for HTML validation #} +{% for item in result_hidden_fields %}{{ item }}{% endfor %} +
    +{% endif %} +{% if results %} +
    + + + +{% for header in result_headers %} +{% endfor %} + + + +{% for result in results %} +{% if result.form.non_field_errors %} + +{% endif %} +{% for item in result %}{{ item }}{% endfor %} +{% endfor %} + +
    + {% if header.sortable %} + {% if header.sort_priority > 0 %} +
    + + {% if num_sorted_fields > 1 %}{{ header.sort_priority }}{% endif %} + +
    + {% endif %} + {% endif %} +
    {% if header.sortable %}{{ header.text|capfirst }}{% else %}{{ header.text|capfirst }}{% endif %}
    +
    +
    {{ result.form.non_field_errors }}
    +
    +{% endif %} diff --git a/tests/admin_views/templates/admin/admin_views/article/date_hierarchy.html b/tests/admin_views/templates/admin/admin_views/article/date_hierarchy.html new file mode 100644 index 0000000000..de1cb747b5 --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/date_hierarchy.html @@ -0,0 +1,9 @@ +{% extends "admin/date_hierarchy.html" %} +{% load i18n %} + +{% block date-hierarchy-choices %} + + +{% endblock %} diff --git a/tests/admin_views/templates/admin/admin_views/article/pagination.html b/tests/admin_views/templates/admin/admin_views/article/pagination.html new file mode 100644 index 0000000000..e072cacd3c --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/pagination.html @@ -0,0 +1,12 @@ +{% load admin_list %} +{% load i18n %} +

    +{% if pagination_required %} +{% for i in page_range %} + {% paginator_number cl i %} +{% endfor %} +{% endif %} +{{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endif %} +{% if show_all_url %}  {% trans 'Show all' %}{% endif %} +{% if cl.formset and cl.result_count %}{% endif %} +

    diff --git a/tests/admin_views/templates/admin/admin_views/article/prepopulated_fields_js.html b/tests/admin_views/templates/admin/admin_views/article/prepopulated_fields_js.html new file mode 100644 index 0000000000..0ee8c7a06c --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/prepopulated_fields_js.html @@ -0,0 +1,7 @@ +{% load l10n static %} + diff --git a/tests/admin_views/templates/admin/admin_views/article/search_form.html b/tests/admin_views/templates/admin/admin_views/article/search_form.html new file mode 100644 index 0000000000..5b5e6a58f6 --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/search_form.html @@ -0,0 +1,16 @@ +{% load i18n static %} +{% if cl.search_fields %} +
    +{% endif %} diff --git a/tests/admin_views/templates/admin/admin_views/article/submit_line.html b/tests/admin_views/templates/admin/admin_views/article/submit_line.html new file mode 100644 index 0000000000..4a2ca08890 --- /dev/null +++ b/tests/admin_views/templates/admin/admin_views/article/submit_line.html @@ -0,0 +1,7 @@ +{% extends "admin/submit_line.html" %} +{% load i18n admin_urls %} + +{% block submit-row %} +{% if show_publish %}{% endif %} +{{ block.super }} +{% endblock %} diff --git a/tests/admin_views/test_templatetags.py b/tests/admin_views/test_templatetags.py index 44a08f32cd..db78636d5d 100644 --- a/tests/admin_views/test_templatetags.py +++ b/tests/admin_views/test_templatetags.py @@ -7,9 +7,10 @@ from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User from django.test import RequestFactory, TestCase from django.urls import reverse +from django.utils.encoding import force_text -from .admin import site -from .models import Question +from .admin import ArticleAdmin, site +from .models import Article, Question from .tests import AdminViewBasicTestCase @@ -28,6 +29,46 @@ class AdminTemplateTagsTest(AdminViewBasicTestCase): self.assertIs(template_context['extra'], True) self.assertIs(template_context['show_save'], True) + def test_can_override_change_form_templatetags(self): + """ + admin_modify templatetags can follow the 'standard' search patter admin/app_label/model/template.html + """ + factory = RequestFactory() + article = Article.objects.all()[0] + request = factory.get(reverse('admin:admin_views_article_change', args=[article.pk])) + request.user = self.superuser + admin = ArticleAdmin(Article, site) + extra_context = {'show_publish': True, 'extra': True} + response = admin.change_view(request, str(article.pk), extra_context=extra_context) + response.render() + self.assertIs(response.context_data['show_publish'], True) + self.assertIs(response.context_data['extra'], True) + content = force_text(response.content) + self.assertIs('name="_save"' in content, True) + self.assertIs('name="_publish"' in content, True) + self.assertIs('override-change_form_object_tools' in content, True) + self.assertIs('override-prepopulated_fields_js' in content, True) + + def test_can_override_change_list_templatetags(self): + """ + admin_list templatetags can follow the 'standard' search patter admin/app_label/model/template.html + """ + factory = RequestFactory() + request = factory.get(reverse('admin:admin_views_article_changelist')) + request.user = self.superuser + admin = ArticleAdmin(Article, site) + admin.date_hierarchy = 'date' + admin.search_fields = ('title', 'content',) + response = admin.changelist_view(request) + response.render() + content = force_text(response.content) + self.assertIs('override-actions' in content, True) + self.assertIs('override-change_list_object_tools' in content, True) + self.assertIs('override-change_list_results' in content, True) + self.assertIs('override-date_hierarchy' in content, True) + self.assertIs('override-pagination' in content, True) + self.assertIs('override-search_form' in content, True) + class DateHierarchyTests(TestCase): factory = RequestFactory() -- cgit v1.3