| 1 |
from django.core import urlresolvers, paginator |
|---|
| 2 |
import urllib |
|---|
| 3 |
|
|---|
| 4 |
PING_URL = "http://www.google.com/webmasters/tools/ping" |
|---|
| 5 |
|
|---|
| 6 |
class SitemapNotFound(Exception): |
|---|
| 7 |
pass |
|---|
| 8 |
|
|---|
| 9 |
def ping_google(sitemap_url=None, ping_url=PING_URL): |
|---|
| 10 |
""" |
|---|
| 11 |
Alerts Google that the sitemap for the current site has been updated. |
|---|
| 12 |
If sitemap_url is provided, it should be an absolute path to the sitemap |
|---|
| 13 |
for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this |
|---|
| 14 |
function will attempt to deduce it by using urlresolvers.reverse(). |
|---|
| 15 |
""" |
|---|
| 16 |
if sitemap_url is None: |
|---|
| 17 |
try: |
|---|
| 18 |
# First, try to get the "index" sitemap URL. |
|---|
| 19 |
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.index') |
|---|
| 20 |
except urlresolvers.NoReverseMatch: |
|---|
| 21 |
try: |
|---|
| 22 |
# Next, try for the "global" sitemap URL. |
|---|
| 23 |
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap') |
|---|
| 24 |
except urlresolvers.NoReverseMatch: |
|---|
| 25 |
pass |
|---|
| 26 |
|
|---|
| 27 |
if sitemap_url is None: |
|---|
| 28 |
raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") |
|---|
| 29 |
|
|---|
| 30 |
from django.contrib.sites.models import Site |
|---|
| 31 |
current_site = Site.objects.get_current() |
|---|
| 32 |
url = "http://%s%s" % (current_site.domain, sitemap_url) |
|---|
| 33 |
params = urllib.urlencode({'sitemap':url}) |
|---|
| 34 |
urllib.urlopen("%s?%s" % (ping_url, params)) |
|---|
| 35 |
|
|---|
| 36 |
class Sitemap: |
|---|
| 37 |
# This limit is defined by Google. See the index documentation at |
|---|
| 38 |
# http://sitemaps.org/protocol.php#index. |
|---|
| 39 |
limit = 50000 |
|---|
| 40 |
|
|---|
| 41 |
def __get(self, name, obj, default=None): |
|---|
| 42 |
try: |
|---|
| 43 |
attr = getattr(self, name) |
|---|
| 44 |
except AttributeError: |
|---|
| 45 |
return default |
|---|
| 46 |
if callable(attr): |
|---|
| 47 |
return attr(obj) |
|---|
| 48 |
return attr |
|---|
| 49 |
|
|---|
| 50 |
def items(self): |
|---|
| 51 |
return [] |
|---|
| 52 |
|
|---|
| 53 |
def location(self, obj): |
|---|
| 54 |
return obj.get_absolute_url() |
|---|
| 55 |
|
|---|
| 56 |
def _get_paginator(self): |
|---|
| 57 |
if not hasattr(self, "paginator"): |
|---|
| 58 |
self.paginator = paginator.Paginator(self.items(), self.limit) |
|---|
| 59 |
return self.paginator |
|---|
| 60 |
paginator = property(_get_paginator) |
|---|
| 61 |
|
|---|
| 62 |
def get_urls(self, page=1): |
|---|
| 63 |
from django.contrib.sites.models import Site |
|---|
| 64 |
current_site = Site.objects.get_current() |
|---|
| 65 |
urls = [] |
|---|
| 66 |
for item in self.paginator.page(page).object_list: |
|---|
| 67 |
loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) |
|---|
| 68 |
url_info = { |
|---|
| 69 |
'location': loc, |
|---|
| 70 |
'lastmod': self.__get('lastmod', item, None), |
|---|
| 71 |
'changefreq': self.__get('changefreq', item, None), |
|---|
| 72 |
'priority': self.__get('priority', item, None) |
|---|
| 73 |
} |
|---|
| 74 |
urls.append(url_info) |
|---|
| 75 |
return urls |
|---|
| 76 |
|
|---|
| 77 |
class FlatPageSitemap(Sitemap): |
|---|
| 78 |
def items(self): |
|---|
| 79 |
from django.contrib.sites.models import Site |
|---|
| 80 |
current_site = Site.objects.get_current() |
|---|
| 81 |
return current_site.flatpage_set.all() |
|---|
| 82 |
|
|---|
| 83 |
class GenericSitemap(Sitemap): |
|---|
| 84 |
priority = None |
|---|
| 85 |
changefreq = None |
|---|
| 86 |
|
|---|
| 87 |
def __init__(self, info_dict, priority=None, changefreq=None): |
|---|
| 88 |
self.queryset = info_dict['queryset'] |
|---|
| 89 |
self.date_field = info_dict.get('date_field', None) |
|---|
| 90 |
self.priority = priority |
|---|
| 91 |
self.changefreq = changefreq |
|---|
| 92 |
|
|---|
| 93 |
def items(self): |
|---|
| 94 |
# Make sure to return a clone; we don't want premature evaluation. |
|---|
| 95 |
return self.queryset.filter() |
|---|
| 96 |
|
|---|
| 97 |
def lastmod(self, item): |
|---|
| 98 |
if self.date_field is not None: |
|---|
| 99 |
return getattr(item, self.date_field) |
|---|
| 100 |
return None |
|---|