From c23d86208d9bd6dc197493fd34274a7049ffb139 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 31 Oct 2015 01:19:47 +0100 Subject: [PATCH] Add all languages to sitemap. Use configurable options --- cms_helper.py | 1 + djangocms_blog/sitemaps/__init__.py | 24 ++++++++++++++++++--- tests/test_utils/urls.py | 8 +++++++ tests/test_views.py | 33 ++++++++++++++++++++++++++++- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/cms_helper.py b/cms_helper.py index a74b8a1..0a95a18 100755 --- a/cms_helper.py +++ b/cms_helper.py @@ -14,6 +14,7 @@ HELPER_SETTINGS = dict( 'meta', 'meta_mixin', 'easy_thumbnails', + 'django.contrib.sitemaps', 'djangocms_text_ckeditor', 'cmsplugin_filer_image', 'taggit', diff --git a/djangocms_blog/sitemaps/__init__.py b/djangocms_blog/sitemaps/__init__.py index 76bd2ba..9d43d6d 100644 --- a/djangocms_blog/sitemaps/__init__.py +++ b/djangocms_blog/sitemaps/__init__.py @@ -1,17 +1,35 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, print_function, unicode_literals +from cms.utils import get_language_list from django.contrib.sitemaps import Sitemap +from parler.utils.context import smart_override from ..models import Post +from ..settings import get_setting class BlogSitemap(Sitemap): - changefreq = 'never' - priority = 0.5 + + def priority(self, obj): + if obj and obj.app_config: + return obj.app_config.sitemap_priority + return get_setting('SITEMAP_PRIORITY_DEFAULT') + + def changefreq(self, obj): + if obj and obj.app_config: + return obj.app_config.sitemap_changefreq + return get_setting('SITEMAP_CHANGEFREQ_DEFAULT') + + def location(self, obj): + with smart_override(obj.get_current_language()): + return obj.get_absolute_url(obj.get_current_language()) def items(self): - return Post.objects.published() + items = [] + for lang in get_language_list(): + items.extend(Post.objects.translated(lang).language(lang).published()) + return items def lastmod(self, obj): return obj.date_modified diff --git a/tests/test_utils/urls.py b/tests/test_utils/urls.py index b15d02a..4954a90 100644 --- a/tests/test_utils/urls.py +++ b/tests/test_utils/urls.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function, unicode_literals import sys +from cms.sitemaps import CMSSitemap from cms.utils.conf import get_cms_setting from django.conf import settings from django.conf.urls import include, patterns, url @@ -10,6 +11,8 @@ from django.conf.urls.i18n import i18n_patterns from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns +from djangocms_blog.sitemaps import BlogSitemap + admin.autodiscover() urlpatterns = patterns( @@ -20,6 +23,11 @@ urlpatterns = patterns( {'document_root': get_cms_setting('MEDIA_ROOT'), 'show_indexes': True}), url(r'^jsi18n/(?P\S+?)/$', 'django.views.i18n.javascript_catalog'), url(r'^taggit_autosuggest/', include('taggit_autosuggest.urls')), + url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', + {'sitemaps': { + 'cmspages': CMSSitemap, 'blog': BlogSitemap, + } + }), ) urlpatterns += staticfiles_urlpatterns() diff --git a/tests/test_views.py b/tests/test_views.py index 6bb8a4c..39022d1 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -17,6 +17,7 @@ from parler.utils.context import smart_override, switch_language from djangocms_blog.feeds import LatestEntriesFeed, TagFeed from djangocms_blog.models import BLOG_CURRENT_NAMESPACE +from djangocms_blog.settings import get_setting from djangocms_blog.sitemaps import BlogSitemap from djangocms_blog.views import ( AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView, @@ -347,9 +348,39 @@ class ViewTest(BaseTest): posts[0].set_current_language('en') sitemap = BlogSitemap() - self.assertEqual(sitemap.items().count(), 3) + self.assertEqual(len(sitemap.items()), 6) for item in sitemap.items(): self.assertTrue(sitemap.lastmod(item).date(), now().today()) + self.assertTrue( + sitemap.priority(item), get_setting('SITEMAP_PRIORITY_DEFAULT') + ) + self.assertTrue( + sitemap.priority(item), get_setting('SITEMAP_CHANGEFREQ_DEFAULT') + ) + + def test_sitemap_config(self): + posts = self.get_posts() + self.app_config_1.app_data.config.sitemap_changefreq = 'daily' + self.app_config_1.app_data.config.sitemap_priority = '0.2' + + sitemap = BlogSitemap() + self.assertEqual(len(sitemap.items()), 4) + for item in sitemap.items(): + self.assertTrue(sitemap.lastmod(item).date(), now().today()) + if item.app_config == self.app_config_1: + self.assertTrue( + sitemap.priority(item), '0.2' + ) + self.assertTrue( + sitemap.changefreq(item), 'daily' + ) + else: + self.assertTrue( + sitemap.priority(item), get_setting('SITEMAP_PRIORITY_DEFAULT') + ) + self.assertTrue( + sitemap.changefreq(item), get_setting('SITEMAP_CHANGEFREQ_DEFAULT') + ) def test_templates(self): posts = self.get_posts()