From 60ebb616a91a6993a42492dab043024a2ca38030 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 31 Aug 2006 23:44:26 +0000 Subject: Renamed django.contrib.sitemap to django.contrib.sitemaps, to be more consistent with our plural form for these sorts of things. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3699 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/sitemap/__init__.py | 90 ---------------------- django/contrib/sitemap/templates/sitemap.xml | 11 --- django/contrib/sitemap/templates/sitemap_index.xml | 8 -- django/contrib/sitemap/views.py | 30 -------- django/contrib/sitemaps/__init__.py | 90 ++++++++++++++++++++++ django/contrib/sitemaps/templates/sitemap.xml | 11 +++ .../contrib/sitemaps/templates/sitemap_index.xml | 8 ++ django/contrib/sitemaps/views.py | 30 ++++++++ docs/sitemaps.txt | 28 +++---- 9 files changed, 153 insertions(+), 153 deletions(-) delete mode 100644 django/contrib/sitemap/__init__.py delete mode 100644 django/contrib/sitemap/templates/sitemap.xml delete mode 100644 django/contrib/sitemap/templates/sitemap_index.xml delete mode 100644 django/contrib/sitemap/views.py create mode 100644 django/contrib/sitemaps/__init__.py create mode 100644 django/contrib/sitemaps/templates/sitemap.xml create mode 100644 django/contrib/sitemaps/templates/sitemap_index.xml create mode 100644 django/contrib/sitemaps/views.py diff --git a/django/contrib/sitemap/__init__.py b/django/contrib/sitemap/__init__.py deleted file mode 100644 index 50f60b821e..0000000000 --- a/django/contrib/sitemap/__init__.py +++ /dev/null @@ -1,90 +0,0 @@ -from django.core import urlresolvers -import urllib - -PING_URL = "http://www.google.com/webmasters/sitemaps/ping" - -class SitemapNotFound(Exception): - pass - -def ping_google(sitemap_url=None, ping_url=PING_URL): - """ - Alerts Google that the sitemap for the current site has been updated. - If sitemap_url is provided, it should be an absolute path to the sitemap - for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this - function will attempt to deduce it by using urlresolvers.reverse(). - """ - if sitemap_url is None: - try: - # First, try to get the "index" sitemap URL. - sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.index') - except urlresolvers.NoReverseMatch: - try: - # Next, try for the "global" sitemap URL. - sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.sitemap') - except urlresolvers.NoReverseMatch: - pass - - if sitemap_url is None: - raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") - - from django.contrib.sites.models import Site - current_site = Site.objects.get_current() - url = "%s%s" % (current_site.domain, sitemap) - params = urllib.urlencode({'sitemap':url}) - urllib.urlopen("%s?%s" % (ping_url, params)) - -class Sitemap: - def __get(self, name, obj, default=None): - try: - attr = getattr(self, name) - except AttributeError: - return default - if callable(attr): - return attr(obj) - return attr - - def items(self): - return [] - - def location(self, obj): - return obj.get_absolute_url() - - def get_urls(self): - from django.contrib.sites.models import Site - current_site = Site.objects.get_current() - urls = [] - for item in self.items(): - loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) - url_info = { - 'location': loc, - 'lastmod': self.__get('lastmod', item, None), - 'changefreq': self.__get('changefreq', item, None), - 'priority': self.__get('priority', item, None) - } - urls.append(url_info) - return urls - -class FlatPageSitemap(Sitemap): - def items(self): - from django.contrib.sites.models import Site - current_site = Site.objects.get_current() - return current_site.flatpage_set.all() - -class GenericSitemap(Sitemap): - priority = None - changefreq = None - - def __init__(self, info_dict, priority=None, changefreq=None): - self.queryset = info_dict['queryset'] - self.date_field = info_dict.get('date_field', None) - self.priority = priority - self.changefreq = changefreq - - def items(self): - # Make sure to return a clone; we don't want premature evaluation. - return self.queryset.filter() - - def lastmod(self, item): - if self.date_field is not None: - return getattr(item, self.date_field) - return None diff --git a/django/contrib/sitemap/templates/sitemap.xml b/django/contrib/sitemap/templates/sitemap.xml deleted file mode 100644 index 3ee4f914f7..0000000000 --- a/django/contrib/sitemap/templates/sitemap.xml +++ /dev/null @@ -1,11 +0,0 @@ - - -{% for url in urlset %} - - {{ url.location|escape }} - {% if url.lastmod %}{{ url.lastmod|date:"Y-m-d" }}{% endif %} - {% if url.changefreq %}{{ url.changefreq }}{% endif %} - {% if url.priority %}{{ url.priority }}{% endif %} - -{% endfor %} - diff --git a/django/contrib/sitemap/templates/sitemap_index.xml b/django/contrib/sitemap/templates/sitemap_index.xml deleted file mode 100644 index e9d722ac7f..0000000000 --- a/django/contrib/sitemap/templates/sitemap_index.xml +++ /dev/null @@ -1,8 +0,0 @@ - - -{% for location in sitemaps %} - - {{ location|escape }} - -{% endfor %} - diff --git a/django/contrib/sitemap/views.py b/django/contrib/sitemap/views.py deleted file mode 100644 index 8a4592c3e4..0000000000 --- a/django/contrib/sitemap/views.py +++ /dev/null @@ -1,30 +0,0 @@ -from django.http import HttpResponse, Http404 -from django.template import loader -from django.contrib.sites.models import Site -from django.core import urlresolvers - -def index(request, sitemaps): - current_site = Site.objects.get_current() - sites = [] - protocol = request.is_secure() and 'https' or 'http' - for section in sitemaps.keys(): - sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.sitemap', kwargs={'section': section}) - sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) - xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) - return HttpResponse(xml, mimetype='application/xml') - -def sitemap(request, sitemaps, section=None): - maps, urls = [], [] - if section is not None: - if not sitemaps.has_key(section): - raise Http404("No sitemap available for section: %r" % section) - maps.append(sitemaps[section]) - else: - maps = sitemaps.values() - for site in maps: - if callable(site): - urls.extend(site().get_urls()) - else: - urls.extend(site.get_urls()) - xml = loader.render_to_string('sitemap.xml', {'urlset': urls}) - return HttpResponse(xml, mimetype='application/xml') diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py new file mode 100644 index 0000000000..50f60b821e --- /dev/null +++ b/django/contrib/sitemaps/__init__.py @@ -0,0 +1,90 @@ +from django.core import urlresolvers +import urllib + +PING_URL = "http://www.google.com/webmasters/sitemaps/ping" + +class SitemapNotFound(Exception): + pass + +def ping_google(sitemap_url=None, ping_url=PING_URL): + """ + Alerts Google that the sitemap for the current site has been updated. + If sitemap_url is provided, it should be an absolute path to the sitemap + for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this + function will attempt to deduce it by using urlresolvers.reverse(). + """ + if sitemap_url is None: + try: + # First, try to get the "index" sitemap URL. + sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.index') + except urlresolvers.NoReverseMatch: + try: + # Next, try for the "global" sitemap URL. + sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.sitemap') + except urlresolvers.NoReverseMatch: + pass + + if sitemap_url is None: + raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") + + from django.contrib.sites.models import Site + current_site = Site.objects.get_current() + url = "%s%s" % (current_site.domain, sitemap) + params = urllib.urlencode({'sitemap':url}) + urllib.urlopen("%s?%s" % (ping_url, params)) + +class Sitemap: + def __get(self, name, obj, default=None): + try: + attr = getattr(self, name) + except AttributeError: + return default + if callable(attr): + return attr(obj) + return attr + + def items(self): + return [] + + def location(self, obj): + return obj.get_absolute_url() + + def get_urls(self): + from django.contrib.sites.models import Site + current_site = Site.objects.get_current() + urls = [] + for item in self.items(): + loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) + url_info = { + 'location': loc, + 'lastmod': self.__get('lastmod', item, None), + 'changefreq': self.__get('changefreq', item, None), + 'priority': self.__get('priority', item, None) + } + urls.append(url_info) + return urls + +class FlatPageSitemap(Sitemap): + def items(self): + from django.contrib.sites.models import Site + current_site = Site.objects.get_current() + return current_site.flatpage_set.all() + +class GenericSitemap(Sitemap): + priority = None + changefreq = None + + def __init__(self, info_dict, priority=None, changefreq=None): + self.queryset = info_dict['queryset'] + self.date_field = info_dict.get('date_field', None) + self.priority = priority + self.changefreq = changefreq + + def items(self): + # Make sure to return a clone; we don't want premature evaluation. + return self.queryset.filter() + + def lastmod(self, item): + if self.date_field is not None: + return getattr(item, self.date_field) + return None diff --git a/django/contrib/sitemaps/templates/sitemap.xml b/django/contrib/sitemaps/templates/sitemap.xml new file mode 100644 index 0000000000..3ee4f914f7 --- /dev/null +++ b/django/contrib/sitemaps/templates/sitemap.xml @@ -0,0 +1,11 @@ + + +{% for url in urlset %} + + {{ url.location|escape }} + {% if url.lastmod %}{{ url.lastmod|date:"Y-m-d" }}{% endif %} + {% if url.changefreq %}{{ url.changefreq }}{% endif %} + {% if url.priority %}{{ url.priority }}{% endif %} + +{% endfor %} + diff --git a/django/contrib/sitemaps/templates/sitemap_index.xml b/django/contrib/sitemaps/templates/sitemap_index.xml new file mode 100644 index 0000000000..e9d722ac7f --- /dev/null +++ b/django/contrib/sitemaps/templates/sitemap_index.xml @@ -0,0 +1,8 @@ + + +{% for location in sitemaps %} + + {{ location|escape }} + +{% endfor %} + diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py new file mode 100644 index 0000000000..8a4592c3e4 --- /dev/null +++ b/django/contrib/sitemaps/views.py @@ -0,0 +1,30 @@ +from django.http import HttpResponse, Http404 +from django.template import loader +from django.contrib.sites.models import Site +from django.core import urlresolvers + +def index(request, sitemaps): + current_site = Site.objects.get_current() + sites = [] + protocol = request.is_secure() and 'https' or 'http' + for section in sitemaps.keys(): + sitemap_url = urlresolvers.reverse('django.contrib.sitemap.views.sitemap', kwargs={'section': section}) + sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) + xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) + return HttpResponse(xml, mimetype='application/xml') + +def sitemap(request, sitemaps, section=None): + maps, urls = [], [] + if section is not None: + if not sitemaps.has_key(section): + raise Http404("No sitemap available for section: %r" % section) + maps.append(sitemaps[section]) + else: + maps = sitemaps.values() + for site in maps: + if callable(site): + urls.extend(site().get_urls()) + else: + urls.extend(site.get_urls()) + xml = loader.render_to_string('sitemap.xml', {'urlset': urls}) + return HttpResponse(xml, mimetype='application/xml') diff --git a/docs/sitemaps.txt b/docs/sitemaps.txt index c23e170d22..fec65572f2 100644 --- a/docs/sitemaps.txt +++ b/docs/sitemaps.txt @@ -31,7 +31,7 @@ Installation To install the sitemap app, follow these steps: - 1. Add ``'django.contrib.sitemap'`` to your INSTALLED_APPS_ setting. + 1. Add ``'django.contrib.sitemaps'`` to your INSTALLED_APPS_ setting. 2. Make sure ``'django.template.loaders.app_directories.load_template_source'`` is in your TEMPLATE_LOADERS_ setting. It's in there by default, so you'll only need to change this if you've changed that setting. @@ -51,7 +51,7 @@ Initialization To activate sitemap generation on your Django site, add this line to your URLconf_: - (r'^sitemap.xml$', 'django.contrib.sitemap.views.sitemap', {'sitemaps': sitemaps}) + (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) This tells Django to build a sitemap when a client accesses ``/sitemap.xml``. @@ -82,7 +82,7 @@ In the simplest case, all these sections get lumped together into one sitemap index that references individual sitemap files, one per section. (See `Creating a sitemap index`_ below.) -``Sitemap`` classes must subclass ``django.contrib.sitemap.Sitemap``. They can +``Sitemap`` classes must subclass ``django.contrib.sitemaps.Sitemap``. They can live anywhere in your codebase. A simple example @@ -92,7 +92,7 @@ Let's assume you have a blog system, with an ``Entry`` model, and you want your sitemap to include all the links to your individual blog entries. Here's how your sitemap class might look:: - from django.contrib.sitemap import Sitemap + from django.contrib.sitemaps import Sitemap from mysite.blog.models import Entry class BlogSitemap(Sitemap): @@ -239,7 +239,7 @@ Example Here's an example of a URLconf_ using both:: from django.conf.urls.defaults import * - from django.contrib.sitemap import FlatPageSitemap, GenericSitemap + from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap from mysite.blog.models import Entry info_dict = { @@ -257,7 +257,7 @@ Here's an example of a URLconf_ using both:: # ... # the sitemap - (r'^sitemap.xml$', 'django.contrib.sitemap.views.sitemap', {'sitemaps': sitemaps}) + (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) ) .. _URLconf: http://www.djangoproject.com/documentation/url_dispatch/ @@ -269,15 +269,15 @@ The sitemap framework also has the ability to create a sitemap index that references individual sitemap files, one per each section defined in your ``sitemaps`` dictionary. The only differences in usage are: - * You use two views in your URLconf: ``django.contrib.sitemap.views.index`` - and ``django.contrib.sitemap.views.sitemap``. - * The ``django.contrib.sitemap.views.sitemap`` view should take a + * You use two views in your URLconf: ``django.contrib.sitemaps.views.index`` + and ``django.contrib.sitemaps.views.sitemap``. + * The ``django.contrib.sitemaps.views.sitemap`` view should take a ``section`` keyword argument. Here is what the relevant URLconf lines would look like for the example above:: - (r'^sitemap.xml$', 'django.contrib.sitemap.views.index', {'sitemaps': sitemaps}) - (r'^sitemap-(?P
.+).xml$', 'django.contrib.sitemap.views.sitemap', {'sitemaps': sitemaps}) + (r'^sitemap.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps}) + (r'^sitemap-(?P
.+).xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}) This will automatically generate a ``sitemap.xml`` file that references both ``sitemap-flatpages.xml`` and ``sitemap-blog.xml``. The ``Sitemap`` @@ -288,7 +288,7 @@ Pinging Google You may want to "ping" Google when your sitemap changes, to let it know to reindex your site. The framework provides a function to do just that: -``django.contrib.sitemap.ping_google()``. +``django.contrib.sitemaps.ping_google()``. ``ping_google()`` takes an optional argument, ``sitemap_url``, which should be the absolute URL of your site's sitemap (e.g., ``'/sitemap.xml'``). If this @@ -296,12 +296,12 @@ argument isn't provided, ``ping_google()`` will attempt to figure out your sitemap by performing a reverse looking in your URLconf. ``ping_google()`` raises the exception -``django.contrib.sitemap.SitemapNotFound`` if it cannot determine your sitemap +``django.contrib.sitemaps.SitemapNotFound`` if it cannot determine your sitemap URL. One useful way to call ``ping_google()`` is from a model's ``save()`` method:: - from django.contrib.sitemap import ping_google + from django.contrib.sitemaps import ping_google class Entry(models.Model): # ... -- cgit v1.3