| 1 |
import datetime |
| 2 |
from dataclasses import dataclass |
| 3 |
from functools import wraps |
| 4 |
|
| 5 |
from django.contrib.sites.shortcuts import get_current_site |
| 6 |
from django.core.paginator import EmptyPage, PageNotAnInteger |
| 7 |
from django.http import Http404 |
| 8 |
from django.template.response import TemplateResponse |
| 9 |
from django.urls import reverse |
| 10 |
from django.utils import timezone |
| 11 |
from django.utils.http import http_date |
| 12 |
|
| 13 |
|
| 14 |
@dataclass |
| 15 |
class SitemapIndexItem: |
| 16 |
location: str |
| 17 |
last_mod: bool = None |
| 18 |
|
| 19 |
|
| 20 |
def x_robots_tag(func): |
| 21 |
@wraps(func) |
| 22 |
def inner(request, *args, **kwargs): |
| 23 |
response = func(request, *args, **kwargs) |
| 24 |
response.headers["X-Robots-Tag"] = "noindex, noodp, noarchive" |
| 25 |
return response |
| 26 |
|
| 27 |
return inner |
| 28 |
|
| 29 |
|
| 30 |
def _get_latest_lastmod(current_lastmod, new_lastmod): |
| 31 |
""" |
| 32 |
Returns the latest `lastmod` where `lastmod` can be either a date or a |
| 33 |
datetime. |
| 34 |
""" |
| 35 |
if not isinstance(new_lastmod, datetime.datetime): |
| 36 |
new_lastmod = datetime.datetime.combine(new_lastmod, datetime.time.min) |
| 37 |
if timezone.is_naive(new_lastmod): |
| 38 |
new_lastmod = timezone.make_aware(new_lastmod, datetime.timezone.utc) |
| 39 |
return new_lastmod if current_lastmod is None else max(current_lastmod, new_lastmod) |
| 40 |
|
| 41 |
|
| 42 |
@x_robots_tag |
| 43 |
def index( |
| 44 |
request, |
| 45 |
sitemaps, |
| 46 |
template_name="sitemap_index.xml", |
| 47 |
content_type="application/xml", |
| 48 |
sitemap_url_name="django.contrib.sitemaps.views.sitemap", |
| 49 |
): |
| 50 |
req_protocol = request.scheme |
| 51 |
req_site = get_current_site(request) |
| 52 |
|
| 53 |
sites = [] # all sections' sitemap URLs |
| 54 |
all_indexes_lastmod = True |
| 55 |
latest_lastmod = None |
| 56 |
for section, site in sitemaps.items(): |
| 57 |
# For each section label, add links of all pages of its sitemap |
| 58 |
# (usually generated by the `sitemap` view). |
| 59 |
if callable(site): |
| 60 |
site = site() |
| 61 |
protocol = req_protocol if site.protocol is None else site.protocol |
| 62 |
sitemap_url = reverse(sitemap_url_name, kwargs={"section": section}) |
| 63 |
absolute_url = "%s://%s%s" % (protocol, req_site.domain, sitemap_url) |
| 64 |
site_lastmod = site.get_latest_lastmod() |
| 65 |
if all_indexes_lastmod: |
| 66 |
if site_lastmod is not None: |
| 67 |
latest_lastmod = _get_latest_lastmod(latest_lastmod, site_lastmod) |
| 68 |
else: |
| 69 |
all_indexes_lastmod = False |
| 70 |
sites.append(SitemapIndexItem(absolute_url, site_lastmod)) |
| 71 |
# Add links to all pages of the sitemap. |
| 72 |
for page in range(2, site.paginator.num_pages + 1): |
| 73 |
sites.append( |
| 74 |
SitemapIndexItem("%s?p=%s" % (absolute_url, page), site_lastmod) |
| 75 |
) |
| 76 |
# If lastmod is defined for all sites, set header so as |
| 77 |
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED |
| 78 |
if all_indexes_lastmod and latest_lastmod: |
| 79 |
headers = {"Last-Modified": http_date(latest_lastmod.timestamp())} |
| 80 |
else: |
| 81 |
headers = None |
| 82 |
return TemplateResponse( |
| 83 |
request, |
| 84 |
template_name, |
| 85 |
{"sitemaps": sites}, |
| 86 |
content_type=content_type, |
| 87 |
headers=headers, |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
@x_robots_tag |
| 92 |
def sitemap( |
| 93 |
request, |
| 94 |
sitemaps, |
| 95 |
section=None, |
| 96 |
template_name="sitemap.xml", |
| 97 |
content_type="application/xml", |
| 98 |
): |
| 99 |
req_protocol = request.scheme |
| 100 |
req_site = get_current_site(request) |
| 101 |
|
| 102 |
if section is not None: |
| 103 |
if section not in sitemaps: |
| 104 |
raise Http404("No sitemap available for section: %r" % section) |
| 105 |
maps = [sitemaps[section]] |
| 106 |
else: |
| 107 |
maps = sitemaps.values() |
| 108 |
page = request.GET.get("p", 1) |
| 109 |
|
| 110 |
lastmod = None |
| 111 |
all_sites_lastmod = True |
| 112 |
urls = [] |
| 113 |
for site in maps: |
| 114 |
try: |
| 115 |
if callable(site): |
| 116 |
site = site() |
| 117 |
urls.extend(site.get_urls(page=page, site=req_site, protocol=req_protocol)) |
| 118 |
if all_sites_lastmod: |
| 119 |
site_lastmod = getattr(site, "latest_lastmod", None) |
| 120 |
if site_lastmod is not None: |
| 121 |
lastmod = _get_latest_lastmod(lastmod, site_lastmod) |
| 122 |
else: |
| 123 |
all_sites_lastmod = False |
| 124 |
except EmptyPage: |
| 125 |
raise Http404("Page %s empty" % page) |
| 126 |
except PageNotAnInteger: |
| 127 |
raise Http404("No page '%s'" % page) |
| 128 |
# If lastmod is defined for all sites, set header so as |
| 129 |
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED |
| 130 |
if all_sites_lastmod: |
| 131 |
headers = {"Last-Modified": http_date(lastmod.timestamp())} if lastmod else None |
| 132 |
else: |
| 133 |
headers = None |
| 134 |
return TemplateResponse( |
| 135 |
request, |
| 136 |
template_name, |
| 137 |
{"urlset": urls}, |
| 138 |
content_type=content_type, |
| 139 |
headers=headers, |
| 140 |
) |