Skip to content

Commit

Permalink
Fix: Sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Nov 11, 2024
1 parent 26c96aa commit 3ab96c4
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions djangocms_blog/sitemaps/__init__.py
Original file line number Diff line number Diff line change
@@ -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


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

0 comments on commit 3ab96c4

Please sign in to comment.