2013-07-23 22:25:21 +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
|
|
|
|
2011-12-17 11:53:25 +08:00
|
|
|
from django.contrib.sites.models import get_current_site
|
2006-09-01 07:13:59 +08:00
|
|
|
from django.core import urlresolvers
|
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
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2013-07-23 22:25:21 +08:00
|
|
|
from django.utils.http import http_date
|
2011-06-28 18:17:01 +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
|
|
|
|
|
|
|
|
@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
|
|
|
|
sitemap_url = urlresolvers.reverse(
|
2013-10-20 07:33:10 +08:00
|
|
|
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-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:
|
2012-07-21 03:14:27 +08:00
|
|
|
maps = list(six.itervalues(sitemaps))
|
2008-07-26 13:07:16 +08:00
|
|
|
page = request.GET.get("p", 1)
|
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))
|
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)
|
|
|
|
if hasattr(site, 'latest_lastmod'):
|
|
|
|
# if latest_lastmod is defined for site, set header so as
|
|
|
|
# ConditionalGetMiddleware is able to send 304 NOT MODIFIED
|
|
|
|
response['Last-Modified'] = http_date(
|
|
|
|
timegm(site.latest_lastmod.utctimetuple()))
|
|
|
|
return response
|