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

improve support for multilanguage sitemaps #1557

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cms/models/managers.py
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very bad and will break a lot of things

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Expand Down
16 changes: 10 additions & 6 deletions cms/sitemaps/cms_sitemap.py
@@ -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):
"""
Expand All @@ -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()
46 changes: 46 additions & 0 deletions cms/sitemaps/utils.py
@@ -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