From 603cd3169193b081caecf29ee9760ad83fa350a2 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 5 Sep 2020 09:04:45 +0200 Subject: [PATCH] Add a robots.txt --- articles/context_processors.py | 10 ++++++++++ attachments/tests.py | 3 --- blog/settings.py | 2 +- blog/templates/blog/robots.txt | 2 ++ blog/tests/__init__.py | 0 blog/tests/test_robots.py | 6 ++++++ blog/urls.py | 7 +++++++ 7 files changed, 26 insertions(+), 4 deletions(-) delete mode 100644 attachments/tests.py create mode 100644 blog/templates/blog/robots.txt create mode 100644 blog/tests/__init__.py create mode 100644 blog/tests/test_robots.py diff --git a/articles/context_processors.py b/articles/context_processors.py index 0005973..7d0ff3d 100644 --- a/articles/context_processors.py +++ b/articles/context_processors.py @@ -1,13 +1,23 @@ from articles.models import Article, Page +IGNORED_PATHS = [ + "/robots.txt", +] + def pages(request): + if request.path in IGNORED_PATHS: + return {} return {"pages": Page.objects.filter(status=Article.PUBLISHED)} def drafts_count(request): + if request.path in IGNORED_PATHS: + return {} return {"drafts_count": Article.objects.filter(status=Article.DRAFT).count()} def date_format(request): + if request.path in IGNORED_PATHS: + return {} return {"CUSTOM_ISO": r"Y-m-d\TH:i:sO"} diff --git a/attachments/tests.py b/attachments/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/attachments/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/blog/settings.py b/blog/settings.py index afd89e9..b1c2c5b 100644 --- a/blog/settings.py +++ b/blog/settings.py @@ -88,7 +88,7 @@ ROOT_URLCONF = "blog.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [], + "DIRS": ["blog/templates"], "APP_DIRS": True, "OPTIONS": { "context_processors": [ diff --git a/blog/templates/blog/robots.txt b/blog/templates/blog/robots.txt new file mode 100644 index 0000000..209f856 --- /dev/null +++ b/blog/templates/blog/robots.txt @@ -0,0 +1,2 @@ +User-Agent: * +Disallow: /admin/ diff --git a/blog/tests/__init__.py b/blog/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/tests/test_robots.py b/blog/tests/test_robots.py new file mode 100644 index 0000000..36bfbf3 --- /dev/null +++ b/blog/tests/test_robots.py @@ -0,0 +1,6 @@ +def test_robots_txt(client): + res = client.get("/robots.txt") + assert res.status_code == 200 + assert res["Content-Type"] == "text/plain" + content = res.content.decode("utf-8") + assert "User-Agent" in content diff --git a/blog/urls.py b/blog/urls.py index 82c6aff..91a76e2 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -16,11 +16,18 @@ Including another URLconf from django.conf.urls.static import static from django.contrib import admin from django.urls import path +from django.views.generic import TemplateView from articles.views import feeds, html from blog import settings urlpatterns = [ + path( + "robots.txt", + TemplateView.as_view( + template_name="blog/robots.txt", content_type="text/plain" + ), + ), path("admin/", admin.site.urls), path("", html.ArticlesListView.as_view(), name="articles-list"), path("drafts/", html.DraftsListView.as_view(), name="drafts-list"),