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-01-29 17:30:28 +08:00
|
|
|
def index(request, sitemaps,
|
|
|
|
template_name='sitemap_index.xml', mimetype='application/xml',
|
|
|
|
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
|
2012-01-30 03:24:32 +08:00
|
|
|
req_protocol = 'https' if request.is_secure() else 'http'
|
|
|
|
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(
|
|
|
|
sitemap_url_name, kwargs={'section': section})
|
|
|
|
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},
|
|
|
|
content_type=mimetype)
|
2006-09-01 07:13:59 +08:00
|
|
|
|
2012-01-29 17:30:28 +08:00
|
|
|
def sitemap(request, sitemaps, section=None,
|
|
|
|
template_name='sitemap.xml', mimetype='application/xml'):
|
2012-01-30 03:24:32 +08:00
|
|
|
req_protocol = 'https' if request.is_secure() else 'http'
|
|
|
|
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:
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
2012-01-30 03:24:32 +08:00
|
|
|
return TemplateResponse(request, template_name, {'urlset': urls},
|
|
|
|
content_type=mimetype)
|