2014-09-10 01:34:40 +08:00
|
|
|
import datetime
|
2015-01-28 20:35:27 +08:00
|
|
|
from calendar import timegm
|
2013-05-04 19:29:40 +08:00
|
|
|
from functools import wraps
|
2013-01-31 20:39:29 +08:00
|
|
|
|
2014-01-26 04:54:25 +08:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2008-07-26 13:07:16 +08:00
|
|
|
from django.core.paginator import EmptyPage, PageNotAnInteger
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.http import Http404
|
2011-06-28 18:17:01 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2013-07-23 22:25:21 +08:00
|
|
|
from django.utils.http import http_date
|
2011-06-28 18:17:01 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-05-04 18:08:15 +08:00
|
|
|
def x_robots_tag(func):
|
|
|
|
@wraps(func)
|
|
|
|
def inner(request, *args, **kwargs):
|
|
|
|
response = func(request, *args, **kwargs)
|
|
|
|
response['X-Robots-Tag'] = 'noindex, noodp, noarchive'
|
|
|
|
return response
|
|
|
|
return inner
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-05-04 18:08:15 +08:00
|
|
|
@x_robots_tag
|
2012-01-29 17:30:28 +08:00
|
|
|
def index(request, sitemaps,
|
2013-01-31 20:39:29 +08:00
|
|
|
template_name='sitemap_index.xml', content_type='application/xml',
|
2013-06-29 07:50:36 +08:00
|
|
|
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
|
2013-01-31 20:39:29 +08:00
|
|
|
|
2013-10-09 02:30:29 +08:00
|
|
|
req_protocol = request.scheme
|
2012-01-30 03:24:32 +08:00
|
|
|
req_site = get_current_site(request)
|
|
|
|
|
2006-09-01 07:13:59 +08:00
|
|
|
sites = []
|
2008-07-26 13:07:16 +08:00
|
|
|
for section, site in sitemaps.items():
|
|
|
|
if callable(site):
|
2012-01-30 03:24:32 +08:00
|
|
|
site = site()
|
|
|
|
protocol = req_protocol if site.protocol is None else site.protocol
|
2015-12-30 23:51:16 +08:00
|
|
|
sitemap_url = reverse(sitemap_url_name, kwargs={'section': section})
|
2012-01-30 03:24:32 +08:00
|
|
|
absolute_url = '%s://%s%s' % (protocol, req_site.domain, sitemap_url)
|
|
|
|
sites.append(absolute_url)
|
|
|
|
for page in range(2, site.paginator.num_pages + 1):
|
|
|
|
sites.append('%s?p=%s' % (absolute_url, page))
|
|
|
|
|
|
|
|
return TemplateResponse(request, template_name, {'sitemaps': sites},
|
2013-01-31 20:39:29 +08:00
|
|
|
content_type=content_type)
|
2006-09-01 07:13:59 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2013-05-04 18:08:15 +08:00
|
|
|
@x_robots_tag
|
2012-01-29 17:30:28 +08:00
|
|
|
def sitemap(request, sitemaps, section=None,
|
2013-06-29 07:50:36 +08:00
|
|
|
template_name='sitemap.xml', content_type='application/xml'):
|
2013-01-31 20:39:29 +08:00
|
|
|
|
2013-10-09 02:30:29 +08:00
|
|
|
req_protocol = request.scheme
|
2012-01-30 03:24:32 +08:00
|
|
|
req_site = get_current_site(request)
|
|
|
|
|
2006-09-01 07:13:59 +08:00
|
|
|
if section is not None:
|
2007-04-26 21:30:48 +08:00
|
|
|
if section not in sitemaps:
|
2006-09-01 07:13:59 +08:00
|
|
|
raise Http404("No sitemap available for section: %r" % section)
|
2012-01-30 03:24:32 +08:00
|
|
|
maps = [sitemaps[section]]
|
2006-09-01 07:13:59 +08:00
|
|
|
else:
|
2015-04-17 22:25:11 +08:00
|
|
|
maps = sitemaps.values()
|
2008-07-26 13:07:16 +08:00
|
|
|
page = request.GET.get("p", 1)
|
2012-01-30 03:24:32 +08:00
|
|
|
|
2015-12-27 02:01:25 +08:00
|
|
|
lastmod = None
|
|
|
|
all_sites_lastmod = True
|
2012-01-30 03:24:32 +08:00
|
|
|
urls = []
|
2006-09-01 07:13:59 +08:00
|
|
|
for site in maps:
|
2008-07-26 13:07:16 +08:00
|
|
|
try:
|
|
|
|
if callable(site):
|
2011-06-28 18:16:34 +08:00
|
|
|
site = site()
|
2012-01-30 03:24:32 +08:00
|
|
|
urls.extend(site.get_urls(page=page, site=req_site,
|
|
|
|
protocol=req_protocol))
|
2015-12-27 02:01:25 +08:00
|
|
|
if all_sites_lastmod:
|
|
|
|
site_lastmod = getattr(site, 'latest_lastmod', None)
|
|
|
|
if site_lastmod is not None:
|
|
|
|
site_lastmod = (
|
|
|
|
site_lastmod.utctimetuple() if isinstance(site_lastmod, datetime.datetime)
|
|
|
|
else site_lastmod.timetuple()
|
|
|
|
)
|
|
|
|
lastmod = site_lastmod if lastmod is None else max(lastmod, site_lastmod)
|
|
|
|
else:
|
|
|
|
all_sites_lastmod = False
|
2008-07-26 13:07:16 +08:00
|
|
|
except EmptyPage:
|
|
|
|
raise Http404("Page %s empty" % page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
raise Http404("No page '%s'" % page)
|
2013-07-23 22:25:21 +08:00
|
|
|
response = TemplateResponse(request, template_name, {'urlset': urls},
|
|
|
|
content_type=content_type)
|
2015-12-27 02:01:25 +08:00
|
|
|
if all_sites_lastmod and lastmod is not None:
|
|
|
|
# if lastmod is defined for all sites, set header so as
|
2013-07-23 22:25:21 +08:00
|
|
|
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED
|
2015-12-27 02:01:25 +08:00
|
|
|
response['Last-Modified'] = http_date(timegm(lastmod))
|
2013-07-23 22:25:21 +08:00
|
|
|
return response
|