Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page template filter inherit templates #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions djangocms_pageadmin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from djangocms_versioning.helpers import version_list_url
from djangocms_versioning.models import Version

from .filters import LanguageFilter, UnpublishedFilter
from .filters import LanguageFilter, TemplateFilter, UnpublishedFilter
from .forms import DuplicateForm
from .helpers import proxy_model

Expand All @@ -37,7 +37,7 @@
class PageContentAdmin(VersioningAdminMixin, DefaultPageContentAdmin):
change_list_template = "admin/djangocms_pageadmin/pagecontent/change_list.html"
list_display_links = None
list_filter = (LanguageFilter, UnpublishedFilter)
list_filter = (LanguageFilter, UnpublishedFilter, TemplateFilter)
search_fields = ("title",)

def get_list_display(self, request):
Expand Down
46 changes: 46 additions & 0 deletions djangocms_pageadmin/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from cms.constants import TEMPLATE_INHERITANCE_MAGIC
from cms.utils.conf import get_cms_setting
from cms.utils.i18n import get_language_tuple, get_site_language_from_request

from djangocms_versioning.constants import UNPUBLISHED
Expand Down Expand Up @@ -63,3 +65,47 @@ def choices(self, changelist):
),
"display": title,
}


class TemplateFilter(admin.SimpleListFilter):
title = _("template")
parameter_name = "template"

def lookups(self, request, model_admin):
for value, name in get_cms_setting('TEMPLATES'):
yield (value, name)

def queryset(self, request, queryset):
template = self.value()
if not template:
return queryset

print("Starting to get tings done")
inherit_qs = queryset.filter(template=TEMPLATE_INHERITANCE_MAGIC)
filtered_qs = queryset.filter(template=template)
inherit_list = [
inherit_template.pk for inherit_template in inherit_qs if inherit_template.get_template() == template]

print("Done")

return filtered_qs | queryset.filter(pk__in=inherit_list)

#return queryset.filter(template=template)

def choices(self, changelist):

# changelist.result_list[0].get_template()

yield {
"selected": self.value() is None,
"query_string": changelist.get_query_string(remove=[self.parameter_name]),
"display": _("All"),
}
for lookup, title in self.lookup_choices:
yield {
"selected": self.value() == str(lookup),
"query_string": changelist.get_query_string(
{self.parameter_name: lookup}
),
"display": title,
}
40 changes: 40 additions & 0 deletions djangocms_pageadmin/test_utils/templates/integration/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% load cms_tags static menu_tags sekizai_tags %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}This is my new project home page{% endblock title %}</title>
<!-- required for "mobile" -->
<meta name="viewport" content="width=device-width,initial-scale=1">
{% render_block "css" %}
<style type="text/css">
.nav {
padding-left: 0;
}
.nav li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
html, body {
height: 100%;
}
</style>
</head>
<body>
{% cms_toolbar %}
<div style="max-width: 940px; margin:0 auto">
<ul class="nav">
{% show_menu 0 100 100 100 %}
</ul>
{% block content %}{% endblock content %}
</div>
<div class="additional-content">
{% block additional_content %}{% endblock %}
</div>
{% render_block "js" %}
{% with_data "js-script" as jsset %}
{% for js in jsset %}<script type="text/javascript" src="{% static js %}"></script>{% endfor %}
{% end_with_data %}
{% render_block "js_end" %}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute 'title' %}{% endblock title %}

{% block content %}
{% placeholder "placeholder_content_1" %}
{% endblock content %}

{% block additional_content %}
{% placeholder "placeholder_content_2" %}
{% endblock additional_content %}
13 changes: 13 additions & 0 deletions djangocms_pageadmin/test_utils/templates/integration/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute 'title' %}{% endblock title %}

{% block content %}
<h1>This is a custom page template</h1>
{% placeholder "placeholder_content_1" %}
{% endblock content %}

{% block additional_content %}
{% placeholder "placeholder_content_2" %}
{% endblock additional_content %}
12 changes: 12 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


HELPER_SETTINGS = {
"INSTALLED_APPS": [
"djangocms_pageadmin",
Expand Down Expand Up @@ -31,6 +34,15 @@
},
]
},
"TEMPLATE_DIRS": (
os.path.join(
os.path.dirname(__file__),
'djangocms_pageadmin', 'test_utils', 'templates', 'integration'),
),
"print (": (
('fullwidth.html', 'Fullwidth'),
('page.html', 'Page')
),
"PARLER_ENABLE_CACHING": False,
"LANGUAGE_CODE": "en",
}
Expand Down
20 changes: 20 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from djangocms_pageadmin.test_utils.helpers import get_toolbar

from cms.utils.conf import get_cms_setting

parse_html = partial(BeautifulSoup, features="lxml")

Expand Down Expand Up @@ -106,6 +107,25 @@ def test_unpublished_filter(self):
self.assertEqual(set(qs_default), set(expected))
self.assertEqual(set(qs_unpublished), set(expected_unpublished))

def test_template_filter(self):
template_1 = get_cms_setting('TEMPLATES')[0][0]
template_2 = get_cms_setting('TEMPLATES')[1][0]
template_1_pages = PageContentWithVersionFactory.create_batch(3, template=template_1, language="en")
template_2_pages = PageContentWithVersionFactory.create_batch(3, template=template_2, language="en")
base_url = self.get_admin_url(PageContent, "changelist")

with self.login_user_context(self.get_superuser()):
# All / No templates filterd is the default
response_default = self.client.get(base_url)
# fullwidth template set
response_template_1 = self.client.get(base_url + "?template={}".format(template_1))
# page template set
response_template_2 = self.client.get(base_url + "?template={}".format(template_2))

self.assertEqual(set(response_default.context["cl"].queryset), set(template_1_pages) | set(template_2_pages))
self.assertEqual(set(response_template_1.context["cl"].queryset), set(template_1_pages))
self.assertEqual(set(response_template_2.context["cl"].queryset), set(template_2_pages))


class ListActionsTestCase(CMSTestCase):
def setUp(self):
Expand Down