Add all languages to sitemap. Use configurable options

This commit is contained in:
Iacopo Spalletti 2015-10-31 01:19:47 +01:00
parent 4ad7197980
commit c23d86208d
4 changed files with 62 additions and 4 deletions

View file

@ -14,6 +14,7 @@ HELPER_SETTINGS = dict(
'meta',
'meta_mixin',
'easy_thumbnails',
'django.contrib.sitemaps',
'djangocms_text_ckeditor',
'cmsplugin_filer_image',
'taggit',

View file

@ -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

View file

@ -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<packages>\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()

View file

@ -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()