Skip to content

Commit 44eb378

Browse files
authored
Merge pull request #1308 from makeabilitylab/1252-robots-followup
Serve sitemap via static robots.txt; drop dead Django robots view (#1252)
2 parents c4e488c + 2be25e1 commit 44eb378

5 files changed

Lines changed: 18 additions & 75 deletions

File tree

makeabilitylab/urls.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@
2626
from django.views.static import serve
2727
from django.conf import settings
2828

29-
from website import views
3029
from website.sitemaps import sitemaps
3130

3231
urlpatterns = [
3332

3433
re_path(r'^admin/', admin.site.urls),
3534

36-
# SEO endpoints (issue #1252). Declared before the website.urls include so
37-
# the app's patterns can't shadow them. The sitemap is generated from our
38-
# querysets (see website/sitemaps.py); robots.txt is environment-aware.
35+
# Dynamic sitemap (issue #1252), generated from our querysets (see
36+
# website/sitemaps.py). Declared before the website.urls include so the
37+
# app's patterns can't shadow it.
38+
#
39+
# NOTE: robots.txt is intentionally NOT routed here. On the servers Apache
40+
# serves the static ./robots.txt from the project checkout and never
41+
# forwards /robots.txt to Django, so a view here would be dead code. Edit
42+
# the top-level ./robots.txt to change crawler rules or the Sitemap line.
3943
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
40-
path('robots.txt', views.robots_txt, name='robots_txt'),
4144

4245
#Info on how to route root to website was found here http://stackoverflow.com/questions/7580220/django-urls-howto-map-root-to-app
4346
re_path(r'', include('website.urls')),

robots.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
User-agent: *
2-
Disallow:
1+
User-agent: *
2+
Disallow:
3+
4+
Sitemap: https://makeabilitylab.cs.washington.edu/sitemap.xml

website/tests/test_sitemap.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
2-
Regression tests for the dynamic sitemap and robots.txt (issue #1252).
2+
Regression tests for the dynamic sitemap (issue #1252).
33
4-
Both endpoints are exercised through the real URL/view stack so a routing or
5-
queryset regression is caught. See website/sitemaps.py and
6-
website/views/robots.py.
4+
The sitemap is exercised through the real URL/view stack so a routing or
5+
queryset regression is caught. See website/sitemaps.py.
6+
7+
Note: robots.txt is a static file (./robots.txt) served by Apache on the
8+
servers, not a Django view, so it isn't covered here.
79
"""
810

9-
import os
1011
from datetime import date
11-
from unittest import mock
1212

1313
from website.tests.base import DatabaseTestCase
1414

@@ -58,23 +58,3 @@ def test_sitemap_includes_news_item(self):
5858
news = self.make_news_item(title="Big Lab News")
5959
body = self.client.get("/sitemap.xml").content.decode()
6060
self.assertIn(f"/news/{news.slug}/", body)
61-
62-
63-
class RobotsTxtTests(DatabaseTestCase):
64-
def test_robots_is_plain_text(self):
65-
resp = self.client.get("/robots.txt")
66-
self.assertEqual(resp.status_code, 200)
67-
self.assertEqual(resp["Content-Type"], "text/plain")
68-
69-
@mock.patch.dict(os.environ, {"DJANGO_ENV": "PROD"})
70-
def test_robots_prod_allows_and_advertises_sitemap(self):
71-
body = self.client.get("/robots.txt").content.decode()
72-
self.assertIn("Allow: /", body)
73-
self.assertIn("Sitemap:", body)
74-
self.assertIn("/sitemap.xml", body)
75-
76-
@mock.patch.dict(os.environ, {"DJANGO_ENV": "TEST"})
77-
def test_robots_non_prod_disallows_all(self):
78-
body = self.client.get("/robots.txt").content.decode()
79-
self.assertIn("Disallow: /", body)
80-
self.assertNotIn("Allow: /", body)

website/views/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
from .view_project_people import *
1010
from .serve_pdf import *
1111
from .awards import awards
12-
from .robots import robots_txt
1312

website/views/robots.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)