From 3ab96c4ab0d81555a69df58da6aea761d3ef1ad9 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Mon, 11 Nov 2024 17:58:06 +0100 Subject: [PATCH] Fix: Sitemap --- djangocms_blog/sitemaps/__init__.py | 30 ++++++++++++----------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/djangocms_blog/sitemaps/__init__.py b/djangocms_blog/sitemaps/__init__.py index bea10b9e..d439f0a3 100644 --- a/djangocms_blog/sitemaps/__init__.py +++ b/djangocms_blog/sitemaps/__init__.py @@ -1,9 +1,8 @@ from cms.utils import get_language_list from django.contrib.sitemaps import Sitemap from django.urls.exceptions import NoReverseMatch -from parler.utils.context import smart_override -from ..models import Post +from ..models import Post, PostContent from ..settings import get_setting @@ -12,40 +11,35 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.url_cache = {} - def priority(self, obj): + def priority(self, obj: PostContent): if obj and obj.app_config: return obj.app_config.sitemap_priority return get_setting("SITEMAP_PRIORITY_DEFAULT") - def changefreq(self, obj): + def changefreq(self, obj: PostContent): 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 self.url_cache[obj.get_current_language()][obj] + def location(self, obj: PostContent) -> str: + return self.url_cache[obj] - def items(self): + def items(self) -> list[PostContent]: items = [] self.url_cache.clear() for lang in get_language_list(): - self.url_cache[lang] = {} - posts = Post.objects.translated(lang).language(lang).published() - for post in posts: + postcontents = PostContent.objects.filter(language=lang) + for postcontent in postcontents: # check if the post actually has a url before appending # if a post is published but the associated app config is not # then this post will not have a url try: - with smart_override(post.get_current_language()): - self.url_cache[lang][post] = post.get_absolute_url() + self.url_cache[postcontent] = postcontent.get_absolute_url() except NoReverseMatch: # couldn't determine the url of the post so pass on it continue - - items.append(post) - + items.append(postcontent) return items - def lastmod(self, obj): - return obj.date_modified + def lastmod(self, obj: PostContent): + return obj.post.date_modified