Bläddra i källkod

Add sitemap (#572)

add sitemap.xml
Kyle Maas 1 år sedan
förälder
incheckning
92c0ff579a
6 ändrade filer med 91 tillägg och 0 borttagningar
  1. 3 0
      cms/settings.py
  2. 8 0
      docs/admins_docs.md
  3. 3 0
      files/urls.py
  4. 10 0
      files/views.py
  5. 1 0
      templates/robots.txt
  6. 66 0
      templates/sitemap.xml

+ 3 - 0
cms/settings.py

@@ -93,6 +93,9 @@ ALLOW_MENTION_IN_COMMENTS = False  # allowing to mention other users with @ in t
 # valid options: content, author
 RELATED_MEDIA_STRATEGY = "content"
 
+# Whether or not to generate a sitemap.xml listing the pages on the site (default: False)
+GENERATE_SITEMAP = False
+
 USE_I18N = True
 USE_L10N = True
 USE_TZ = True

+ 8 - 0
docs/admins_docs.md

@@ -470,6 +470,14 @@ ADMINS_NOTIFICATIONS = {
 - Make the portal workflow public, but at the same time set `GLOBAL_LOGIN_REQUIRED = True` so that only logged in users can see content.
 - You can either set `REGISTER_ALLOWED = False` if you want to add members yourself or checkout options on "django-allauth settings" that affects registration in `cms/settings.py`. Eg set the portal invite only, or set email confirmation as mandatory, so that you control who registers.
 
+### 5.24 Enable the sitemap
+
+Whether or not to enable generation of a sitemap file at http://your_installation/sitemap.xml (default: False)
+
+```
+GENERATE_SITEMAP = False
+```
+
 ## 6. Manage pages
 to be written
 

+ 3 - 0
files/urls.py

@@ -89,3 +89,6 @@ urlpatterns = [
     re_path(r"^manage/media$", views.manage_media, name="manage_media"),
     re_path(r"^manage/users$", views.manage_users, name="manage_users"),
 ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+
+if hasattr(settings, "GENERATE_SITEMAP") and settings.GENERATE_SITEMAP:
+    urlpatterns.append(path("sitemap.xml", views.sitemap, name="sitemap"))

+ 10 - 0
files/views.py

@@ -292,6 +292,16 @@ def search(request):
     return render(request, "cms/search.html", context)
 
 
+def sitemap(request):
+    """Sitemap"""
+
+    context = {}
+    context["media"] = list(Media.objects.filter(Q(listable=True)).order_by("-add_date"))
+    context["playlists"] = list(Playlist.objects.filter().order_by("-add_date"))
+    context["users"] = list(User.objects.filter())
+    return render(request, "sitemap.xml", context, content_type="application/xml")
+
+
 def tags(request):
     """List tags view"""
 

+ 1 - 0
templates/robots.txt

@@ -1,2 +1,3 @@
 User-Agent: *
 Allow: /
+Sitemap: {{ FRONTEND_HOST }}/sitemap.xml

+ 66 - 0
templates/sitemap.xml

@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+{% load static %}
+	<url>
+		<loc>{{ FRONTEND_HOST }}</loc>
+		<changefreq>always</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/featured</loc>
+		<changefreq>daily</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/recommended</loc>
+		<changefreq>always</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/latest</loc>
+		<changefreq>hourly</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/members</loc>
+		<changefreq>daily</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/tags</loc>
+		<changefreq>daily</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/categories</loc>
+		<changefreq>weekly</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/history</loc>
+		<changefreq>always</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/liked</loc>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/about</loc>
+		<changefreq>monthly</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/tos</loc>
+		<changefreq>monthly</changefreq>
+	</url>
+	<url>
+		<loc>{{ FRONTEND_HOST }}/contact</loc>
+		<changefreq>never</changefreq>
+	</url>
+	{% for media_object in media %}
+	<url>
+		<loc>{{ FRONTEND_HOST}}/view?m={{ media_object.friendly_token }}</loc>
+	</url>
+	{% endfor %}
+	{% for playlist_object in playlists %}
+	<url>
+		<loc>{{ FRONTEND_HOST}}/playlists/{{ playlist_object.friendly_token }}</loc>
+	</url>
+	{% endfor %}
+	{% for user_object in users %}
+	<url>
+		<loc>{{ FRONTEND_HOST}}/user/{{ user_object.username }}/</loc>
+	</url>
+	{% endfor %}
+</urlset>