From 977464955494d038ae14e804ad4473dcb45abc80 Mon Sep 17 00:00:00 2001
From: Dorian Lesbre <dorian.lesbre@gmail.com>
Date: Wed, 6 Jan 2021 10:43:54 +0100
Subject: [PATCH] Added sitemap

---
 home/urls.py           |  7 +++++++
 home/views.py          | 19 +++++++++++++++++++
 interludes/settings.py |  3 ++-
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/home/urls.py b/home/urls.py
index 0a7f59e..bf87cf7 100644
--- a/home/urls.py
+++ b/home/urls.py
@@ -1,11 +1,18 @@
+from django.contrib.sitemaps.views import sitemap
 from django.views.generic import RedirectView
 from django.urls import path
 from . import views
 
+sitemaps = {"static_pages": views.StaticViewSitemap}
+
 urlpatterns = [
 	path('', views.static_view, {"slug":"home"}, name = 'home'),
 	path('inscription/', views.static_view, {"slug":"inscription"}, name = 'inscription'),
 	path('activites/', views.static_view, {"slug":"activites"}, name = 'activites'),
 	path('faq/', views.static_view, {"slug":"faq"}, name = 'FAQ'),
 	path('favicon.ico', RedirectView.as_view(url='/static/imgs/favicon.ico')),
+	path(
+		'sitemap.xml', sitemap, {'sitemaps': sitemaps},
+		name='django.contrib.sitemaps.views.sitemap'
+	),
 ]
\ No newline at end of file
diff --git a/home/views.py b/home/views.py
index 4029562..c310d1d 100644
--- a/home/views.py
+++ b/home/views.py
@@ -1,4 +1,23 @@
+from django.contrib.sitemaps import Sitemap
 from django.shortcuts import render
+from django.urls import reverse
 
 def static_view(request, slug):
 	return render(request, slug+'.html', {'slug': slug})
+
+
+class StaticViewSitemap(Sitemap):
+	changefreq = 'monthly'
+
+	def items(self):
+		return ["home", "inscription", "activites", "FAQ"]
+
+	def location(self, item):
+		return reverse(item)
+
+	def priority(self, obj):
+		# Priorize home page over the rest in search results
+		if obj == "home" or obj == "":
+				return 0.8
+		else:
+			return None # defaults to 0.5 when unset
\ No newline at end of file
diff --git a/interludes/settings.py b/interludes/settings.py
index dd8962d..67b227c 100644
--- a/interludes/settings.py
+++ b/interludes/settings.py
@@ -37,7 +37,8 @@ INSTALLED_APPS = [
 	'django.contrib.sessions',
 	'django.contrib.messages',
 	'django.contrib.staticfiles',
-  'home.apps.HomeConfig'
+	'django.contrib.sitemaps',
+  'home.apps.HomeConfig',
 ]
 
 MIDDLEWARE = [
-- 
GitLab