feat: add sitemap

This commit is contained in:
Vasily Zubarev 2023-01-17 10:23:23 +01:00
parent df71c18d3c
commit f713da9b58
3 changed files with 22 additions and 2 deletions

16
posts/sitemaps.py Normal file
View File

@ -0,0 +1,16 @@
from django.contrib.sitemaps import Sitemap
from posts.models import Post
class BlogPostsSitemap(Sitemap):
def items(self):
return Post.visible_objects()
def lastmod(self, obj: Post):
return obj.updated_at
sitemaps = {
"posts": BlogPostsSitemap,
}

View File

@ -24,9 +24,10 @@ def profile(request):
def robots(request):
lines = [
"User-agent: *",
"Host: https://vas3k.blog",
f"Host: https://{request.get_host()}",
f"Sitemap: https://{request.get_host()}/sitemap.xml",
"Disallow: /clickers/",
"Disallow: /auth/",
"Clean-param: comment_order&goto /",
"Clean-param: comment_order&goto&preview /",
]
return HttpResponse("\n".join(lines), content_type="text/plain")

View File

@ -1,11 +1,13 @@
from django.conf import settings
from django.contrib import admin
from django.contrib.sitemaps.views import sitemap
from django.urls import path, include
from django.views.generic import RedirectView
from clickers.views import click_comment, click_block
from comments.views import delete_comment, create_comment
from inside.views import donate, subscribe, confirm, unsubscribe
from posts.sitemaps import sitemaps
from posts.views import index, show_post, list_posts, edit_post
from rss.feeds import FullFeed, PublicFeed, PrivateFeed
from users.views import profile, robots
@ -40,6 +42,7 @@ urlpatterns = [
path(r"comments/create/", create_comment, name="create_comment"),
path(r"comments/<str:comment_id>/delete/", delete_comment, name="delete_comment"),
path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
path("robots.txt", robots, name="robots"),
path(r"<str:post_type>/<str:post_slug>/", show_post, name="show_post"),