def get_nodes(self, request): page_queryset = get_page_queryset(request) site = current_site(request) lang = get_language_from_request(request) filters = { 'site': site, } if hide_untranslated(lang, site.pk): filters['title_set__language'] = lang if not use_draft(request): filters['title_set__published'] = True if not use_draft(request): page_queryset = page_queryset.published() pages = page_queryset.filter(**filters).order_by("path") ids = {} nodes = [] first = True home_cut = False home_children = [] home = None actual_pages = [] # cache view perms visible_pages = get_visible_pages(request, pages, site) for page in pages: # Pages are ordered by path, therefore the first page is the root # of the page tree (a.k.a "home") if page.pk not in visible_pages: # Don't include pages the user doesn't have access to continue if not home: home = page if first and page.pk != home.pk: home_cut = True if (home_cut and (page.parent_id == home.pk or page.parent_id in home_children)): home_children.append(page.pk) if ((page.pk == home.pk and home.in_navigation) or page.pk != home.pk): first = False ids[page.id] = page actual_pages.append(page) page.title_cache = {} langs = [lang] if not hide_untranslated(lang): langs.extend(get_fallback_languages(lang)) titles = list(get_title_queryset(request).filter( page__in=ids, language__in=langs)) for title in titles: # add the title and slugs and some meta data page = ids[title.page_id] page.title_cache[title.language] = title for page in actual_pages: if page.title_cache: nodes.append(page_to_node(page, home, home_cut)) return nodes
def get_nodes(self, request): page_queryset = get_page_queryset(request) site = Site.objects.get_current() lang = get_language_from_request(request) filters = { 'site': site, } if hide_untranslated(lang, site.pk): filters['title_set__language'] = lang pages = page_queryset.published().filter(**filters).order_by( "tree_id", "lft") ids = {} nodes = [] first = True home_cut = False home_children = [] home = None actual_pages = [] # cache view perms visible_pages = get_visible_pages(request, pages, site) for page in pages: # Pages are ordered by tree_id, therefore the first page is the root # of the page tree (a.k.a "home") if page.pk not in visible_pages: # Don't include pages the user doesn't have access to continue if not home: home = page page.home_pk_cache = home.pk if first and page.pk != home.pk: home_cut = True if (page.parent_id == home.pk or page.parent_id in home_children) and home_cut: home_children.append(page.pk) if (page.pk == home.pk and home.in_navigation) or page.pk != home.pk: first = False ids[page.id] = page actual_pages.append(page) page.title_cache = {} langs = [lang] if not hide_untranslated(lang): langs.extend(get_fallback_languages(lang)) titles = list( get_title_queryset(request).filter(page__in=ids, language__in=langs)) for title in titles: # add the title and slugs and some meta data page = ids[title.page_id] page.title_cache[title.language] = title for page in actual_pages: if page.title_cache: nodes.append(page_to_node(page, home, home_cut)) return nodes
def get_page_path(self, lang): page = getattr(self.request, 'current_page', None) if page: with force_language(lang): try: return page.get_absolute_url(language=lang, fallback=False) except (Title.DoesNotExist, NoReverseMatch): if hide_untranslated(lang) and settings.USE_I18N: return '/%s/' % lang else: return page.get_absolute_url(language=lang, fallback=True) else: return '/%s/' % lang if settings.USE_I18N else '/'
def get_page_path(self, lang): page = getattr(self.request, 'current_page', None) if page: with force_language(lang): try: return page.get_absolute_url(language=lang, fallback=False) except Title.DoesNotExist: if hide_untranslated(lang): return '/%s/' % lang else: return page.get_absolute_url(language=lang, fallback=True) else: return '/%s/' % lang
def get_alternate(page, site=None, site_languages=None, protocol=None, enable_redirectors=None): # Optimize for repeated calls within the same site if site is None: site = Site.objects.get_current() if site_languages is None: site_languages = get_public_languages(site.pk) # TODO: build API for this default if enable_redirectors is None: enable_redirectors = get_config_redirectors() protocol = protocol if protocol is not None else 'http' # find all public translations for this page available_languages = {l for l in page.get_languages() if l in site_languages} sitemap_languages = [l for l in site_languages if l in available_languages] # handle case when hide_untranslated = False if len(sitemap_languages) < len(site_languages): for lang in site_languages: if (lang not in sitemap_languages and not hide_untranslated(lang, site.pk)): for fb_lang in sitemap_languages: if fb_lang in get_fallback_languages(lang, site.pk): sitemap_languages.append(lang) break # Append default redirector link if sitemap_languages and enable_redirectors: sitemap_languages.append(LANG_REDIRECTOR) alt_links = OrderedDict() for lang in sitemap_languages: if lang == LANG_REDIRECTOR: with override(sitemap_languages[0]): url = page.get_absolute_url() url = re.sub(r'^/{}'.format(sitemap_languages[0]), '', url) assert sitemap_languages[0] != LANG_REDIRECTOR else: with override(lang): url = page.get_absolute_url() alt_links[lang] = {'language_code': lang, 'location': '%s://%s%s' % (protocol, site.domain, url), 'redirector': lang == LANG_REDIRECTOR, 'path': url} return alt_links
def get_page_path(self, lang): page = getattr(self.request, 'current_page', None) if not page: return '/%s/' % lang if settings.USE_I18N else '/' page_languages = page.get_published_languages() if lang in page_languages: return page.get_absolute_url(lang, fallback=False) site = get_current_site() if is_valid_site_language(lang, site_id=site.pk): _valid_language = True _hide_untranslated = hide_untranslated(lang, site.pk) else: _valid_language = False _hide_untranslated = False if _hide_untranslated and settings.USE_I18N: return '/%s/' % lang default_language = get_default_language_for_site(site.pk) if not _valid_language and default_language in page_languages: # The request language is not configured for the current site. # Fallback to the default language configured for the current site. return page.get_absolute_url(default_language, fallback=False) if _valid_language: fallbacks = get_fallback_languages(lang, site_id=site.pk) or [] fallbacks = [ _lang for _lang in fallbacks if _lang in page_languages ] else: fallbacks = [] if fallbacks: return page.get_absolute_url(fallbacks[0], fallback=False) return '/%s/' % lang if settings.USE_I18N else '/'
def get_nodes(self, request): from cms.models import Title site = self.renderer.site lang = self.renderer.request_language pages = get_page_queryset( site, draft=self.renderer.draft_mode_active, published=not self.renderer.draft_mode_active, ) if is_valid_site_language(lang, site_id=site.pk): _valid_language = True _hide_untranslated = hide_untranslated(lang, site.pk) else: _valid_language = False _hide_untranslated = False if _valid_language: # The request language has been explicitly configured # for the current site. if _hide_untranslated: fallbacks = [] else: fallbacks = get_fallback_languages(lang, site_id=site.pk) languages = [lang ] + [_lang for _lang in fallbacks if _lang != lang] else: # The request language is not configured for the current site. # Fallback to all configured public languages for the current site. languages = get_public_languages(site.pk) fallbacks = languages pages = (pages.filter(title_set__language__in=languages). select_related('node').order_by('node__path').distinct()) if not self.renderer.draft_mode_active: # we're dealing with public pages. # prefetch the draft versions. pages = pages.select_related('publisher_public__node') pages = get_visible_nodes(request, pages, site) if not pages: return [] try: homepage = [page for page in pages if page.is_home][0] except IndexError: homepage = None titles = Title.objects.filter( language__in=languages, publisher_is_draft=self.renderer.draft_mode_active, ) lookup = Prefetch( 'title_set', to_attr='filtered_translations', queryset=titles, ) prefetch_related_objects(pages, lookup) # Build the blank title instances only once blank_title_cache = { language: EmptyTitle(language=language) for language in languages } if lang not in blank_title_cache: blank_title_cache[lang] = EmptyTitle(language=lang) # Maps a node id to its page id node_id_to_page = {} def _page_to_node(page): # EmptyTitle is used to prevent the cms from trying # to find a translation in the database page.title_cache = blank_title_cache.copy() for trans in page.filtered_translations: page.title_cache[trans.language] = trans menu_node = get_menu_node_for_page( self.renderer, page, language=lang, fallbacks=fallbacks, ) return menu_node menu_nodes = [] for page in pages: node = page.node parent_id = node_id_to_page.get(node.parent_id) if node.parent_id and not parent_id: # If the parent page is not available (unpublished, etc..) # don't bother creating menu nodes for its descendants. continue menu_node = _page_to_node(page) cut_homepage = homepage and not homepage.in_navigation if cut_homepage and parent_id == homepage.pk: # When the homepage is hidden from navigation, # we need to cut all its direct children from it. menu_node.parent_id = None else: menu_node.parent_id = parent_id node_id_to_page[node.pk] = page.pk menu_nodes.append(menu_node) return menu_nodes
def get_nodes(self, request): site = self.renderer.site lang = self.renderer.language page_queryset = get_page_queryset(request) filters = { 'site': site, } if hide_untranslated(lang, site.pk): filters['title_set__language'] = lang if not self.renderer.draft_mode_active: filters['title_set__published'] = True if not self.renderer.draft_mode_active: page_queryset = page_queryset.published() pages = page_queryset.filter(**filters).order_by("path") ids = {} nodes = [] home = None actual_pages = [] # cache view perms visible_pages = frozenset(get_visible_pages(request, pages, site)) for page in pages: # Pages are ordered by path, therefore the first page is the root # of the page tree (a.k.a "home") if page.pk not in visible_pages: # Don't include pages the user doesn't have access to continue if not home: home = page ids[page.id] = page actual_pages.append(page) page.title_cache = {} langs = [lang] if not hide_untranslated(lang): langs.extend(get_fallback_languages(lang)) titles = get_title_queryset(request).filter(page__in=ids, language__in=langs) for title in titles.iterator(): # add the title and slugs and some meta data page = ids[title.page_id] page.title_cache[title.language] = title renderer = self.renderer _page_to_node = functools.partial( page_to_node, renderer=renderer, home=home, language=lang, ) for page in actual_pages: if page.title_cache: node = _page_to_node(page=page) nodes.append(node) return nodes
def get_nodes(self, request): site = self.renderer.site lang = self.renderer.language page_queryset = get_page_queryset(request) filters = { 'site': site, } if hide_untranslated(lang, site.pk): filters['title_set__language'] = lang if not self.renderer.draft_mode_active: filters['title_set__published'] = True if not self.renderer.draft_mode_active: page_queryset = page_queryset.published() pages = page_queryset.filter(**filters).order_by("path") ids = {} nodes = [] home = None actual_pages = [] # cache view perms visible_pages = frozenset(get_visible_pages(request, pages, site)) for page in pages: # Pages are ordered by path, therefore the first page is the root # of the page tree (a.k.a "home") if page.pk not in visible_pages: # Don't include pages the user doesn't have access to continue if not home: home = page ids[page.id] = page actual_pages.append(page) page.title_cache = {} langs = [lang] if not hide_untranslated(lang): langs.extend(get_fallback_languages(lang)) titles = get_title_queryset(request).filter(page__in=ids, language__in=langs) for title in titles.iterator( ): # add the title and slugs and some meta data page = ids[title.page_id] page.title_cache[title.language] = title renderer = self.renderer _page_to_node = functools.partial( page_to_node, renderer=renderer, home=home, language=lang, ) for page in actual_pages: if page.title_cache: node = _page_to_node(page=page) nodes.append(node) return nodes