Ticket #10909: sitemaps_custom_domain_docs_tests.diff

File sitemaps_custom_domain_docs_tests.diff, 7.5 KB (added by daniellindsley, 15 years ago)

And now with a docstring (docs+tests).

  • django/contrib/sitemaps/__init__.py

    diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
    index f877317..c2fe3b2 100644
    a b def ping_google(sitemap_url=None, ping_url=PING_URL):  
    2727    if sitemap_url is None:
    2828        raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.")
    2929
    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})
     30    if '://' not in sitemap_url:
     31        from django.contrib.sites.models import Site
     32        current_site = Site.objects.get_current()
     33        sitemap_url = "http://%s%s" % (current_site.domain, sitemap_url)
     34    params = urllib.urlencode({'sitemap': sitemap_url})
    3435    urllib.urlopen("%s?%s" % (ping_url, params))
    3536
    3637class Sitemap(object):
    class Sitemap(object):  
    6061    paginator = property(_get_paginator)
    6162
    6263    def get_urls(self, page=1):
    63         from django.contrib.sites.models import Site
    64         current_site = Site.objects.get_current()
    6564        urls = []
    6665        for item in self.paginator.page(page).object_list:
    67             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
     66            loc = "http://%s%s" % (self.get_domain(),
     67                                   self.__get('location', item))
    6868            url_info = {
    6969                'location':   loc,
    7070                'lastmod':    self.__get('lastmod', item, None),
    class Sitemap(object):  
    7474            urls.append(url_info)
    7575        return urls
    7676
     77    def get_domain(self):
     78        """
     79        This method can be overridden to avoid the requirement of the ``sites``
     80        contrib application.
     81        """
     82        from django.contrib.sites.models import Site
     83        return Site.objects.get_current().domain
     84
    7785class FlatPageSitemap(Sitemap):
    7886    def items(self):
    7987        from django.contrib.sites.models import Site
  • docs/ref/contrib/sitemaps.txt

    diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt
    index a71f19d..eb3ed4a 100644
    a b Sitemap class reference  
    208208        page is ``0.5``. See the `sitemaps.org documentation`_ for more.
    209209
    210210        .. _sitemaps.org documentation: http://www.sitemaps.org/protocol.html#prioritydef
     211   
     212    .. method:: Sitemap.get_domain
     213
     214        **Optional.** A method.
     215
     216        It should return the applicable domain, as a string, to be applied to
     217        all URLs within the sitemap.
     218
     219        By default, this uses the ``sites`` framework and returns the domain
     220        from the current :class:`django.contrib.sites.models.Site`.
     221       
     222        If you don't want use the ``sites`` framework, you can override this
     223        method and return the domain of your choosing. This should not include
     224        the protocol or path. Examples:
     225
     226            * Good: :file:`'example.com'`
     227            * Bad: :file:`'example/'`
     228            * Bad: :file:`'http://example.com'`
    211229
    212230Shortcuts
    213231=========
  • new file tests/regressiontests/sitemaps/fixtures/sitemapdata.json

    diff --git a/tests/regressiontests/sitemaps/__init__.py b/tests/regressiontests/sitemaps/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/sitemaps/fixtures/sitemapdata.json b/tests/regressiontests/sitemaps/fixtures/sitemapdata.json
    new file mode 100644
    index 0000000..d6d5ae0
    - +  
     1[
     2  {
     3    "model": "sitemaps.entry",
     4    "pk": 1,
     5    "fields": {
     6      "title": "My first entry",
     7      "date": "2008-01-01 12:30:00"
     8    }
     9  },
     10  {
     11    "model": "sitemaps.entry",
     12    "pk": 2,
     13    "fields": {
     14      "title": "My second entry",
     15      "date": "2008-01-02 12:30:00"
     16    }
     17  },
     18  {
     19    "model": "sitemaps.entry",
     20    "pk": 3,
     21    "fields": {
     22      "title": "My third entry",
     23      "date": "2008-01-02 13:30:00"
     24    }
     25  },
     26  {
     27    "model": "sitemaps.entry",
     28    "pk": 4,
     29    "fields": {
     30      "title": "A & B < C > D",
     31      "date": "2008-01-03 13:30:00"
     32    }
     33  }
     34]
  • new file tests/regressiontests/sitemaps/models.py

    diff --git a/tests/regressiontests/sitemaps/models.py b/tests/regressiontests/sitemaps/models.py
    new file mode 100644
    index 0000000..14f5c3d
    - +  
     1from django.db import models
     2
     3class Entry(models.Model):
     4    title = models.CharField(max_length=200)
     5    date = models.DateTimeField()
     6
     7    class Meta:
     8        ordering = ('date',)
     9
     10    def __unicode__(self):
     11        return self.title
     12
     13    def get_absolute_url(self):
     14        return "/blog/%s/" % self.pk
  • new file tests/regressiontests/sitemaps/tests.py

    diff --git a/tests/regressiontests/sitemaps/tests.py b/tests/regressiontests/sitemaps/tests.py
    new file mode 100644
    index 0000000..17eee42
    - +  
     1from xml.dom import minidom
     2from django.test import TestCase
     3
     4
     5class SitemapsTestCase(TestCase):
     6    fixtures = ['sitemapdata.json']
     7    urls = 'regressiontests.sitemaps.urls'
     8   
     9    def test_standard_and_custom(self):
     10        resp = self.client.get('/sitemap.xml')
     11        self.assertEqual(resp.status_code, 200)
     12        sitemap_xml = resp.content
     13       
     14        doc = minidom.parseString(sitemap_xml)
     15        urls = doc.getElementsByTagName('url')
     16        self.assertEqual(8, len(urls))
     17       
     18        found_urls = []
     19       
     20        for url in urls:
     21            loc = url.firstChild.firstChild
     22            found_urls.append(loc.wholeText)
     23       
     24        self.assertEqual([
     25            u'http://djangoproject.com/blog/1/',
     26            u'http://djangoproject.com/blog/2/',
     27            u'http://djangoproject.com/blog/3/',
     28            u'http://djangoproject.com/blog/4/',
     29            u'http://example.com/blog/1/',
     30            u'http://example.com/blog/2/',
     31            u'http://example.com/blog/3/',
     32            u'http://example.com/blog/4/'
     33        ], sorted(found_urls))
  • new file tests/regressiontests/sitemaps/urls.py

    diff --git a/tests/regressiontests/sitemaps/urls.py b/tests/regressiontests/sitemaps/urls.py
    new file mode 100644
    index 0000000..cd3f496
    - +  
     1from django.conf.urls.defaults import *
     2from django.contrib.sitemaps import Sitemap
     3from regressiontests.sitemaps.models import Entry
     4
     5class EntrySitemap(Sitemap):
     6    def items(self):
     7        return Entry.objects.all().order_by('-date')
     8
     9class CustomDomainSite(EntrySitemap):
     10    def get_domain(self):
     11        return 'djangoproject.com'
     12
     13sitemaps = {
     14    'standard': EntrySitemap,
     15    'custom': CustomDomainSite,
     16}
     17
     18urlpatterns = patterns('',
     19    (r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps})
     20)
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    index 81b4424..24d3a4f 100755
    a b ALWAYS_INSTALLED_APPS = [  
    2525    'django.contrib.contenttypes',
    2626    'django.contrib.auth',
    2727    'django.contrib.sites',
     28    'django.contrib.sitemaps',
    2829    'django.contrib.flatpages',
    2930    'django.contrib.redirects',
    3031    'django.contrib.sessions',
  • tests/urls.py

    diff --git a/tests/urls.py b/tests/urls.py
    index 01d6408..893836a 100644
    a b urlpatterns = patterns('',  
    2929    (r'widget_admin/', include('regressiontests.admin_widgets.urls')),
    3030
    3131    (r'^utils/', include('regressiontests.utils.urls')),
    32 
     32   
    3333    # test urlconf for syndication tests
    3434    (r'^syndication/', include('regressiontests.syndication.urls')),
    3535
Back to Top