From d0ba4d607fa246a17c38eb836171d92e8a3cccd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 17 Jul 2024 15:56:52 +0200 Subject: [PATCH] Use sitemap django app --- Dockerfile | 1 + content/templates/jinja2/allwidgets.html | 2 +- content/templates/jinja2/sitemap.html | 2 +- jssg/settings.py | 7 ++++-- jssg/sitemaps.py | 28 ++++++++++++++++++++++++ jssg/urls.py | 7 ++++-- tsconfig.json | 19 +++++++++++++++- 7 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 jssg/sitemaps.py diff --git a/Dockerfile b/Dockerfile index 82b7ff0..46007d1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,7 @@ COPY common-content/ common-content/ COPY galae-content/ galae-content/ # Build +RUN ./manage.py migrate RUN npm run build \ && ./manage.py distill-local --collectstatic --force dist diff --git a/content/templates/jinja2/allwidgets.html b/content/templates/jinja2/allwidgets.html index dcc18fc..166ffd4 100644 --- a/content/templates/jinja2/allwidgets.html +++ b/content/templates/jinja2/allwidgets.html @@ -19,7 +19,7 @@ diff --git a/content/templates/jinja2/sitemap.html b/content/templates/jinja2/sitemap.html index e0cd7d8..693438e 100644 --- a/content/templates/jinja2/sitemap.html +++ b/content/templates/jinja2/sitemap.html @@ -9,7 +9,7 @@ {% endfor %} {% for p in object.posts_slugs %} - {{ object.domain }}/pages/{{ p }}.html + {{ object.domain }}/posts/{{ p }}.html {% endfor %} diff --git a/jssg/settings.py b/jssg/settings.py index a0ce03b..386c824 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -46,20 +46,23 @@ runserver.default_addr = '127.0.0.1' # JSSG -JFME_DOMAIN = "https://www.galae.net" +JFME_DOMAIN = "www.galae.net" JFME_CONTENT_DIRS = [BASE_DIR / "content"] + [BASE_DIR / "galae-content"] + [BASE_DIR / "common-content"] JFME_PAGES_DIRS = [path / "pages" for path in JFME_CONTENT_DIRS] JFME_POSTS_DIRS = [path / "posts" for path in JFME_CONTENT_DIRS] JFME_TEMPLATES_DIRS = [path / "templates" for path in JFME_CONTENT_DIRS] JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS] - +#Django sites and sitemap app +SITE_ID = 1 # Application definition INSTALLED_APPS = [ "jssg", "django.contrib.contenttypes", + "django.contrib.sites", + "django.contrib.sitemaps", "whitenoise.runserver_nostatic", "django.contrib.staticfiles", "django_distill", diff --git a/jssg/sitemaps.py b/jssg/sitemaps.py new file mode 100644 index 0000000..0c1b8bc --- /dev/null +++ b/jssg/sitemaps.py @@ -0,0 +1,28 @@ +from typing import Any +from django.contrib.sitemaps import Sitemap +from django.contrib.sites.models import Site +from jssg.models import Page, Post +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 PageSitemap(MySitemap) : + def items(self) : + return list(Page.load_glob(all = True)) + def location(self, page) -> str: + if page.rel_folder_path != '' : + return "/" + page.rel_folder_path + "/" + page.slug + ".html" + else : + return "/" + page.slug + ".html" + +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" + else : + return "posts/articles/" + page.slug + ".html" \ No newline at end of file diff --git a/jssg/urls.py b/jssg/urls.py index 513dc1a..04c37ca 100644 --- a/jssg/urls.py +++ b/jssg/urls.py @@ -18,6 +18,8 @@ from jssg.models import Page, Post from jssg import settings +from django.contrib.sitemaps.views import sitemap +from jssg.sitemaps import PageSitemap, PostSitemap # print([p for p in Page.get_pages()]) @@ -53,7 +55,8 @@ ), distill_path( "sitemap.xml", - views.SitemapView.as_view(), - name = "sitemap" + sitemap, + {"sitemaps": {"page": PageSitemap, "posts": PostSitemap}}, + name="django.contrib.sitemaps.views.sitemap", ) ] diff --git a/tsconfig.json b/tsconfig.json index 984c649..2abd10b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,24 @@ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "paths": { + "@/*": [ + "./*" + ], + "@s:jssg/*": [ + "./jssg/static/jssg/*" + ], + "@t:jssg/*": [ + "./jssg/templates/jssg/*" + ], + "@s:django_distill/*": [ + "./env/lib/python3.9/site-packages/django_distill/static/django_distill/*" + ], + "@t:django_distill/*": [ + "./env/lib/python3.9/site-packages/django_distill/templates/django_distill/*" + ] + } }, "include": [ "content/front/"