improve support for multilanguage sitemaps #1557

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+57 −7
Split
View
@@ -171,7 +171,7 @@ def get_page_slug(self, slug, site=None):
# created new public method to meet test case requirement and to get a list of titles for published pages
def public(self):
- return self.get_query_set().filter(page__publisher_is_draft=False, page__published=True)
+ return self.get_query_set().filter(page__published=True)
@digi604

digi604 Dec 12, 2012

Contributor

This is very bad and will break a lot of things

@mihaisucan

mihaisucan Dec 12, 2012

Do you have better suggestions? I can simply avoid this change, entirely. I can put the full query I need in cms_sitemap.py.

def drafts(self):
return self.get_query_set().filter(page__publisher_is_draft=True)
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from django.contrib.sitemaps import Sitemap
+from django.utils.translation import get_language
+from cms.models import Title
def from_iterable(iterables):
"""
@@ -14,16 +16,18 @@ class CMSSitemap(Sitemap):
priority = 0.5
def items(self):
- from cms.utils.page_resolver import get_page_queryset
- page_queryset = get_page_queryset(None)
- all_pages = page_queryset.published().filter(login_required=False)
- return all_pages
+ titles = Title.objects.public().filter(page__login_required=False, \
+ language=get_language())
+ return titles
- def lastmod(self, page):
+ def lastmod(self, title):
+ page = title.page
modification_dates = [page.changed_date, page.publication_date]
plugins_for_placeholder = lambda placeholder: placeholder.get_plugins()
plugins = from_iterable(map(plugins_for_placeholder, page.placeholders.all()))
plugin_modification_dates = map(lambda plugin: plugin.changed_date, plugins)
modification_dates.extend(plugin_modification_dates)
return max(modification_dates)
-
+
+ def location(self, title):
+ return title.page.get_absolute_url()
View
@@ -0,0 +1,46 @@
+from django.utils import translation
+from django.conf import settings
+
+LANGUAGES = getattr(settings, 'LANGUAGES', [])
+MIDDLEWARE_CLASSES = getattr(settings, 'MIDDLEWARE_CLASSES', ())
+MULTILINGUAL_URL = \
+ 'cms.middleware.multilingual.MultilingualURLMiddleware' \
+ in MIDDLEWARE_CLASSES
+
+def GetMultilanguageSitemapClass(sitemap, language):
+ """Wrap a Sitemap class within a language-aware class"""
+ class InnerClass(sitemap):
+ language = None
+
+ def __init__(self, *args, **kwargs):
+ self.language = language
+ super(InnerClass, self).__init__(*args, **kwargs)
+
+ def items(self, *args, **kwargs):
+ translation.activate(self.language)
+ result = super(InnerClass, self).items(*args, **kwargs)
+ translation.deactivate()
+ return result
+
+ def location(self, *args, **kwargs):
+ translation.activate(self.language)
+ url = super(InnerClass, self).location(*args, **kwargs)
+ translation.deactivate()
+ return '/%s%s' % (self.language, url)
+
+ return InnerClass
+
+def MakeMultilanguageSitemap(sitemaps):
+ """Takes a sitemap dict and modify it to hold the same sitemap classes
+ for every configured language"""
+ if not MULTILINGUAL_URL:
+ return sitemaps
+
+ for name, sitemap in sitemaps.items():
+ del sitemaps[name]
+ for lang in LANGUAGES:
+ new_name = '%s_%s' % (name, lang[0])
+ sitemaps[new_name] = GetMultilanguageSitemapClass(sitemap, lang[0])
+
+ return sitemaps
+