From 92c0ff579a9811224f29fc9b007d7f6863b9b205 Mon Sep 17 00:00:00 2001 From: Kyle Maas Date: Fri, 10 Nov 2023 08:03:36 -0500 Subject: [PATCH] Add sitemap (#572) add sitemap.xml --- cms/settings.py | 3 ++ docs/admins_docs.md | 8 ++++++ files/urls.py | 3 ++ files/views.py | 10 +++++++ templates/robots.txt | 1 + templates/sitemap.xml | 66 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+) create mode 100644 templates/sitemap.xml diff --git a/cms/settings.py b/cms/settings.py index 32060cc..33a3cee 100644 --- a/cms/settings.py +++ b/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 diff --git a/docs/admins_docs.md b/docs/admins_docs.md index 3c39965..1dac780 100644 --- a/docs/admins_docs.md +++ b/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 diff --git a/files/urls.py b/files/urls.py index 943b3b7..adcdc27 100644 --- a/files/urls.py +++ b/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")) diff --git a/files/views.py b/files/views.py index ce90196..19505a0 100644 --- a/files/views.py +++ b/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""" diff --git a/templates/robots.txt b/templates/robots.txt index f6e6d1d..11e1ccc 100644 --- a/templates/robots.txt +++ b/templates/robots.txt @@ -1,2 +1,3 @@ User-Agent: * Allow: / +Sitemap: {{ FRONTEND_HOST }}/sitemap.xml diff --git a/templates/sitemap.xml b/templates/sitemap.xml new file mode 100644 index 0000000..98f7e50 --- /dev/null +++ b/templates/sitemap.xml @@ -0,0 +1,66 @@ + + +{% load static %} + + {{ FRONTEND_HOST }} + always + + + {{ FRONTEND_HOST }}/featured + daily + + + {{ FRONTEND_HOST }}/recommended + always + + + {{ FRONTEND_HOST }}/latest + hourly + + + {{ FRONTEND_HOST }}/members + daily + + + {{ FRONTEND_HOST }}/tags + daily + + + {{ FRONTEND_HOST }}/categories + weekly + + + {{ FRONTEND_HOST }}/history + always + + + {{ FRONTEND_HOST }}/liked + + + {{ FRONTEND_HOST }}/about + monthly + + + {{ FRONTEND_HOST }}/tos + monthly + + + {{ FRONTEND_HOST }}/contact + never + + {% for media_object in media %} + + {{ FRONTEND_HOST}}/view?m={{ media_object.friendly_token }} + + {% endfor %} + {% for playlist_object in playlists %} + + {{ FRONTEND_HOST}}/playlists/{{ playlist_object.friendly_token }} + + {% endfor %} + {% for user_object in users %} + + {{ FRONTEND_HOST}}/user/{{ user_object.username }}/ + + {% endfor %} +