Skip to content

Commit

Permalink
add lastmod in sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 17, 2024
1 parent c0f9587 commit 613ebbb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
26 changes: 12 additions & 14 deletions content/templates/jinja2/sitemap.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

{% for p in object.pages_slugs %}
<url>
<loc>{{ object.domain }}/pages/{{ p }}.html</loc>
</url>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{% for url in urlset %}
<url>
<loc>{{ url.location }}</loc>
{% if url.lastmod %}<lastmod>{{ url.lastmod.strftime(url.item.lastmod_format) }}</lastmod>{% endif %}
{% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
{% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
{% for alternate in url.alternates %}
<xhtml:link rel="alternate" hreflang="{{ alternate.lang_code }}" href="{{ alternate.location }}"/>
{% endfor %}
{% for p in object.posts_slugs %}
<url>
<loc>{{ object.domain }}/posts/{{ p }}.html</loc>
</url>
{% endfor %}

</urlset>
</url>
{% endfor %}
</urlset>
1 change: 1 addition & 0 deletions jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class Page(Document):
"""A webpage, with a title and some content."""

BASE_DIR = settings.JFME_PAGES_DIRS
lastmod_format = settings.JFME_SITEMAP_LASTMOD_DATETIME_FORMAT

def __init__(self, content: str, **metadata) -> None:
"""Create a new page.
Expand Down
1 change: 1 addition & 0 deletions jssg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
JFME_TEMPLATES_DIRS = [path / "templates" for path in JFME_CONTENT_DIRS]
JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS]
JFME_NUMBER_OF_POSTS_BY_PAGE = 3
JFME_SITEMAP_LASTMOD_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z" # %Y:YYYY, %m:MM, %d:DD, %z:+/-HHMM

#Django sites and sitemap app
SITE_ID = 1
Expand Down
6 changes: 6 additions & 0 deletions jssg/sitemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from django.contrib.sites.models import Site
from jssg.models import Page, Post, PostList
from django.conf import settings
from datetime import datetime

class MySitemap(Sitemap) :
#Overriding get_url() to specify the domain name
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)
Expand All @@ -23,6 +25,8 @@ def location(self, page) -> str:
return "/" + page.rel_folder_path + "/" + page.slug + ".html"
else :
return "/" + page.slug + ".html"
def lastmod(self, post) :
return datetime.fromtimestamp(post.path.lstat().st_mtime)

class PostSitemap(MySitemap) :
def items(self) :
Expand All @@ -32,6 +36,8 @@ def location(self, post) -> str:
return "/posts/article/" + post.rel_folder_path + "/" + post.slug + ".html"
else :
return "/posts/articles/" + post.slug + ".html"
def lastmod(self, post) :
return datetime.fromtimestamp(post.path.lstat().st_mtime)

class PostListSitemap(MySitemap) :
def items(self) :
Expand Down
5 changes: 4 additions & 1 deletion jssg/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
distill_path(
"sitemap.xml",
sitemap,
{"sitemaps": {"constant-url": ConstantUrlSitemap, "page": PageSitemap, "posts": PostSitemap, "postlist":PostListSitemap}},
{
"sitemaps": {"constant-url": ConstantUrlSitemap, "page": PageSitemap, "posts": PostSitemap, "postlist":PostListSitemap},
"template_name":"sitemap.html"
},
name="django.contrib.sitemaps.views.sitemap",
)
]

0 comments on commit 613ebbb

Please sign in to comment.