From c0f9587ac7c70ee6180860cf506b62df5216abde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 17 Jul 2024 16:24:07 +0200 Subject: [PATCH] add postlists, index, atom, and sitemap to sitemap --- jssg/models.py | 4 +++- jssg/sitemaps.py | 25 ++++++++++++++++++++----- jssg/urls.py | 4 ++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/jssg/models.py b/jssg/models.py index f0a6450..eeb83fe 100644 --- a/jssg/models.py +++ b/jssg/models.py @@ -332,7 +332,9 @@ def get_categories_and_pages(cls) : t += [{"category": category, "page":page} for page in range(1, cls(category).nb_pages + 1)] return t - + def get_postlists(cls) : + return cls.get_categories_and_pages() + cls.get_pages() + @classmethod def get_pages(cls) : return [{"page": page} for page in range(1, cls().nb_pages+1)] diff --git a/jssg/sitemaps.py b/jssg/sitemaps.py index 0c1b8bc..9715649 100644 --- a/jssg/sitemaps.py +++ b/jssg/sitemaps.py @@ -1,7 +1,7 @@ from typing import Any from django.contrib.sitemaps import Sitemap from django.contrib.sites.models import Site -from jssg.models import Page, Post +from jssg.models import Page, Post, PostList from django.conf import settings class MySitemap(Sitemap) : @@ -9,6 +9,12 @@ def get_urls(self, site=None, **kwargs): site = Site(domain=settings.JFME_DOMAIN, name=settings.JFME_DOMAIN) return super(MySitemap, self).get_urls(site=site, **kwargs) +class ConstantUrlSitemap(MySitemap) : + def items(self) : + return ["/", "/atom.xml", "/sitemap.xml"] + def location(self, url) -> str: + return url + class PageSitemap(MySitemap) : def items(self) : return list(Page.load_glob(all = True)) @@ -21,8 +27,17 @@ def location(self, page) -> str: class PostSitemap(MySitemap) : def items(self) : return list(Post.load_glob(all = True)) - def location(self, page) -> str: - if page.rel_folder_path != '' : - return "/posts/article/" + page.rel_folder_path + "/" + page.slug + ".html" + def location(self, post) -> str: + if post.rel_folder_path != '' : + return "/posts/article/" + post.rel_folder_path + "/" + post.slug + ".html" + else : + return "/posts/articles/" + post.slug + ".html" + +class PostListSitemap(MySitemap) : + def items(self) : + return PostList().get_postlists() + def location(self, postlist) -> str: + if "category" in postlist : + return "/posts/category/" + postlist["category"] + "/page" + str(postlist["page"]) + ".html" else : - return "posts/articles/" + page.slug + ".html" \ No newline at end of file + return "/posts/category/page" + str(postlist["page"]) + ".html" \ No newline at end of file diff --git a/jssg/urls.py b/jssg/urls.py index 4b87916..8954378 100644 --- a/jssg/urls.py +++ b/jssg/urls.py @@ -19,7 +19,7 @@ from jssg import settings from django.contrib.sitemaps.views import sitemap -from jssg.sitemaps import PageSitemap, PostSitemap +from jssg.sitemaps import PageSitemap, PostSitemap, PostListSitemap, ConstantUrlSitemap # print([p for p in Page.get_pages()]) # print([p for p in PostList.get_categories_and_pages()]) @@ -70,7 +70,7 @@ distill_path( "sitemap.xml", sitemap, - {"sitemaps": {"page": PageSitemap, "posts": PostSitemap}}, + {"sitemaps": {"constant-url": ConstantUrlSitemap, "page": PageSitemap, "posts": PostSitemap, "postlist":PostListSitemap}}, name="django.contrib.sitemaps.views.sitemap", ) ]