Skip to content

Commit

Permalink
Merge b38261f into 1df448b
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpenrath committed Jan 26, 2021
2 parents 1df448b + b38261f commit 1de7bdf
Show file tree
Hide file tree
Showing 88 changed files with 784 additions and 478 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,9 @@ Changelog
Unreleased
==================

* Add a `CMS_PAGETREE_DESCENDANTS_LIMIT` setting to be able to prevent a
pagetree node from being unfoldable if it has too many descendants
* Make search results foldable
* Fixed builds on RTD
* Enforce use of coverage > 4 for python 3.8 support

Expand Down
131 changes: 64 additions & 67 deletions cms/admin/pageadmin.py
Expand Up @@ -13,6 +13,7 @@
from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.admin.options import IS_POPUP_VAR
from django.contrib.admin.utils import get_deleted_objects
from django.contrib.admin.views.main import ERROR_FLAG
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core.exceptions import (ObjectDoesNotExist,
Expand Down Expand Up @@ -746,8 +747,6 @@ def get_sites_for_user(self, user):
return [site for site in sites if _has_perm(user, site)]

def changelist_view(self, request, extra_context=None):
from django.contrib.admin.views.main import ERROR_FLAG

if not self.has_change_permission(request, obj=None):
raise PermissionDenied

Expand All @@ -764,41 +763,8 @@ def changelist_view(self, request, extra_context=None):
if not language:
language = get_language()

query = request.GET.get('q', '')
pages = self.get_queryset(request)
pages, use_distinct = self.get_search_results(request, pages, query)

changelist_form = self.changelist_form(request.GET)

try:
changelist_form.full_clean()
pages = changelist_form.run_filters(pages)
except (ValueError, ValidationError):
# Wacky lookup parameters were given, so redirect to the main
# changelist page, without parameters, and pass an 'invalid=1'
# parameter via the query string. If wacky parameters were given
# and the 'invalid=1' parameter was already in the query string,
# something is screwed up with the database, so display an error
# page.
if ERROR_FLAG in request.GET.keys():
return SimpleTemplateResponse('admin/invalid_setup.html', {
'title': _('Database error'),
})
return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')

if changelist_form.is_filtered():
pages = pages.prefetch_related(
Prefetch(
'title_set',
to_attr='filtered_translations',
queryset=Title.objects.filter(language__in=get_language_list(site.pk))
),
)
pages = pages.distinct() if use_distinct else pages
# Evaluates the queryset
has_items = len(pages) >= 1
else:
has_items = pages.exists()
changelist_form.full_clean()

context = self.admin_site.each_context(request)
context.update({
Expand All @@ -816,10 +782,8 @@ def changelist_view(self, request, extra_context=None):
'tree': {
'site': site,
'sites': self.get_sites_for_user(request.user),
'query': query,
'query': request.GET.get('q', ''),
'is_filtered': changelist_form.is_filtered(),
'items': pages,
'has_items': has_items,
},
})
context.update(extra_context or {})
Expand Down Expand Up @@ -1420,17 +1384,50 @@ def get_tree(self, request):
pages = self.get_queryset(request)
node_id = request.GET.get('nodeId')
open_nodes = list(map(int, request.GET.getlist('openNodes[]')))
node_page = None
root_pages = None

# Retrieve search parameters if exist
query = request.GET.get('q', None)
changelist_form = self.changelist_form(request.GET)

try:
changelist_form.full_clean()
pages = changelist_form.run_filters(pages)
except (ValueError, ValidationError):
# Wacky lookup parameters were given, so redirect to the main
# changelist page, without parameters, and pass an 'invalid=1'
# parameter via the query string. If wacky parameters were given
# and the 'invalid=1' parameter was already in the query string,
# something is screwed up with the database, so display an error
# page.
if ERROR_FLAG in request.GET.keys():
return SimpleTemplateResponse('admin/invalid_setup.html', {
'title': _('Database error'),
})
return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')

# Prepare queries to get pages
if node_id:
page = get_object_or_404(pages, node_id=int(node_id))
pages = page.get_descendant_pages().filter(Q(node__in=open_nodes) |Q(node__parent__in=open_nodes))
# Get descendants of node id provided
node_page = get_object_or_404(pages, node_id=int(node_id))
pages = node_page.get_descendant_pages().filter(Q(node__in=open_nodes) | Q(node__parent__in=open_nodes))
elif changelist_form.is_filtered():
# Get nodes that match filters
search_results, use_distinct = self.get_search_results(request, pages, query)
search_results = search_results.distinct() if use_distinct else search_results
search_results_node_ids = search_results.values_list('node__id', flat=True)
pages = pages.filter(
# get all matched nodes
Q(node__in=search_results_node_ids)
# or children of the open descendants
| Q(node__parent__in=open_nodes)
)
else:
page = None
# Otherwise retrieve root nodes
pages = pages.filter(
# get all root nodes
Q(node__depth=1)
# or children which were previously open
| Q(node__depth=2, node__in=open_nodes)
# or children of the open descendants
| Q(node__parent__in=open_nodes)
)
Expand All @@ -1441,17 +1438,25 @@ def get_tree(self, request):
queryset=Title.objects.filter(language__in=get_language_list(site.pk))
),
)

# Select root_pages for the right case
if node_id:
root_pages = (page for page in pages if page.node.depth == node_page.node.depth + 1)
elif changelist_form.is_filtered():
root_pages = (page for page in pages if page.node.id in search_results_node_ids)
else:
root_pages = (page for page in pages if page.node.depth == 1)

rows = self.get_tree_rows(
request,
pages=pages,
language=get_site_language_from_request(request, site_id=site.pk),
depth=(page.node.depth + 1 if page else 1),
follow_descendants=True,
root_pages=root_pages,
open_nodes=open_nodes
)
return HttpResponse(u''.join(rows))

def get_tree_rows(self, request, pages, language, depth=1,
follow_descendants=True):
def get_tree_rows(self, request, pages, language, root_pages, open_nodes=[]):
"""
Used for rendering the page tree, inserts into context everything what
we need for single item
Expand All @@ -1463,6 +1468,7 @@ def get_tree_rows(self, request, pages, language, depth=1,
is_popup = (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET)
languages = get_language_list(site.pk)
user_can_add = page_permissions.user_can_add_subpage
descendants_limit = get_cms_setting('PAGETREE_DESCENDANTS_LIMIT')

def render_page_row(page):
page.title_cache = {trans.language: trans for trans in page.filtered_translations}
Expand All @@ -1488,14 +1494,15 @@ def render_page_row(page):
'node': page.node,
'ancestors': [node.item for node in page.node.get_cached_ancestors()],
'descendants': [node.item for node in page.node.get_cached_descendants()],
'follow_descendants': page.node.numchild <= descendants_limit,
'request': request,
'lang': language,
'metadata': metadata,
'page_languages': page.get_languages(),
'preview_language': language,
'follow_descendants': follow_descendants,
'site_languages': languages,
'is_popup': is_popup,
'open_nodes': open_nodes,
'has_add_page_permission': user_can_add(user, target=page),
'has_change_permission': self.has_change_permission(request, obj=page),
'has_publish_permission': self.has_publish_permission(request, obj=page),
Expand All @@ -1504,26 +1511,16 @@ def render_page_row(page):
}
return template.render(context)

if follow_descendants:
root_pages = (page for page in pages if page.node.depth == depth)
else:
# When the tree is filtered, it's displayed as a flat structure
root_pages = pages

if depth == 1:
nodes = []
nodes = []
for page in pages:
page.node.__dict__['item'] = page
nodes.append(page.node)

for page in pages:
page.node.__dict__['item'] = page
nodes.append(page.node)
for page in root_pages:
if page.node.id in open_nodes:
page.node._set_hierarchy(nodes, [])

for page in root_pages:
page.node._set_hierarchy(nodes)
yield render_page_row(page)
else:
for page in root_pages:
page.node.__dict__['item'] = page
yield render_page_row(page)
yield render_page_row(page)

def resolve(self, request):
if not request.user.is_staff:
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/ar/LC_MESSAGES/django.po
Expand Up @@ -1088,6 +1088,10 @@ msgstr "القائمة"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/bg/LC_MESSAGES/django.po
Expand Up @@ -1079,6 +1079,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/bn/LC_MESSAGES/django.po
Expand Up @@ -1075,6 +1075,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/ca/LC_MESSAGES/django.po
Expand Up @@ -1078,6 +1078,10 @@ msgstr "Menú"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/cs/LC_MESSAGES/django.po
Expand Up @@ -1090,6 +1090,10 @@ msgstr "Menu"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/da/LC_MESSAGES/django.po
Expand Up @@ -1083,6 +1083,10 @@ msgstr "Menu"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/de/LC_MESSAGES/django.po
Expand Up @@ -1101,6 +1101,10 @@ msgstr "Menu"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/el/LC_MESSAGES/django.po
Expand Up @@ -1083,6 +1083,10 @@ msgstr "Μενού"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/en/LC_MESSAGES/django.po
Expand Up @@ -1113,6 +1113,10 @@ msgstr "Menu"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-"
#| "add\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/en_GB/LC_MESSAGES/django.po
Expand Up @@ -1075,6 +1075,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/eo/LC_MESSAGES/django.po
Expand Up @@ -1075,6 +1075,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/es/LC_MESSAGES/django.po
Expand Up @@ -1104,6 +1104,10 @@ msgstr "Menú"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/es_AR/LC_MESSAGES/django.po
Expand Up @@ -1090,6 +1090,10 @@ msgstr "Menu"
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/es_BO/LC_MESSAGES/django.po
Expand Up @@ -1081,6 +1081,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down
4 changes: 4 additions & 0 deletions cms/locale/es_DO/LC_MESSAGES/django.po
Expand Up @@ -1075,6 +1075,10 @@ msgstr ""
#| " JavaScript seems to be disabled so please\n"
#| " <a href=\"%(admin_add_page)s\" class=\"js-welcome-\">add a page</a> manually.\n"
#| " "

msgid "There are too many descendants to unfold this page. Please search the page you are looking for."
msgstr ""

msgid ""
"\n"
" <em>There is no %(object)s around yet.</em>\n"
Expand Down

0 comments on commit 1de7bdf

Please sign in to comment.