Skip to content

Commit

Permalink
Optimize qs
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Mar 22, 2024
1 parent d3f3290 commit cb1182b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 6 additions & 3 deletions cms/utils/page.py
Expand Up @@ -95,10 +95,13 @@ def get_page_from_request(request, use_path=None, clean_path=None):
.get_for_site(site)
.filter(path=path)
.select_related('page__node')
.prefetch_related('page__pagecontent_set')
)
page = getattr(page_urls.first(), "page", None)
if page is not None:
page_urls = list(page_urls) # force queryset evaluation to save 1 query
try:
page = page_urls[0].page
except IndexError:
page = None
else:
page.urls_cache = {url.language: url for url in page_urls}
return page

Expand Down
8 changes: 3 additions & 5 deletions cms/views.py
Expand Up @@ -140,12 +140,10 @@ def details(request, slug):
# this means we need to correctly redirect that request.
return _handle_no_page(request)

# pagecontent_languages will return all languages available to the public
# get_page_from_request has prefetched all public pagecontent_set objects
# we use the _get_page_content_cache method to populate the cache with all public languages
# The languages are then filtered out by the user allowed languages
pagecontent_languages = [
pagecontent.language for pagecontent in page.pagecontent_set.all()
]
page._get_page_content_cache(None, fallback=True, force_reload=True)
pagecontent_languages = list(page.page_content_cache.keys())
available_languages = [
language for language in user_languages
if language in pagecontent_languages
Expand Down

0 comments on commit cb1182b

Please sign in to comment.