Skip to content

Commit

Permalink
add postlists, index, atom, and sitemap to sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 17, 2024
1 parent 1d85f1f commit c0f9587
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 3 additions & 1 deletion jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
25 changes: 20 additions & 5 deletions jssg/sitemaps.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
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) :
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))
Expand All @@ -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"
return "/posts/category/page" + str(postlist["page"]) + ".html"
4 changes: 2 additions & 2 deletions jssg/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
Expand Down Expand Up @@ -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",
)
]

0 comments on commit c0f9587

Please sign in to comment.