Ticket #10909: sitemaps_custom_domain_docs_tests.diff
File sitemaps_custom_domain_docs_tests.diff, 7.5 KB (added by , 15 years ago) |
---|
-
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): 27 27 if sitemap_url is None: 28 28 raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") 29 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}) 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}) 34 35 urllib.urlopen("%s?%s" % (ping_url, params)) 35 36 36 37 class Sitemap(object): … … class Sitemap(object): 60 61 paginator = property(_get_paginator) 61 62 62 63 def get_urls(self, page=1): 63 from django.contrib.sites.models import Site64 current_site = Site.objects.get_current()65 64 urls = [] 66 65 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)) 68 68 url_info = { 69 69 'location': loc, 70 70 'lastmod': self.__get('lastmod', item, None), … … class Sitemap(object): 74 74 urls.append(url_info) 75 75 return urls 76 76 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 77 85 class FlatPageSitemap(Sitemap): 78 86 def items(self): 79 87 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 208 208 page is ``0.5``. See the `sitemaps.org documentation`_ for more. 209 209 210 210 .. _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'` 211 229 212 230 Shortcuts 213 231 ========= -
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
- + 1 from django.db import models 2 3 class 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
- + 1 from xml.dom import minidom 2 from django.test import TestCase 3 4 5 class 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
- + 1 from django.conf.urls.defaults import * 2 from django.contrib.sitemaps import Sitemap 3 from regressiontests.sitemaps.models import Entry 4 5 class EntrySitemap(Sitemap): 6 def items(self): 7 return Entry.objects.all().order_by('-date') 8 9 class CustomDomainSite(EntrySitemap): 10 def get_domain(self): 11 return 'djangoproject.com' 12 13 sitemaps = { 14 'standard': EntrySitemap, 15 'custom': CustomDomainSite, 16 } 17 18 urlpatterns = 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 = [ 25 25 'django.contrib.contenttypes', 26 26 'django.contrib.auth', 27 27 'django.contrib.sites', 28 'django.contrib.sitemaps', 28 29 'django.contrib.flatpages', 29 30 'django.contrib.redirects', 30 31 'django.contrib.sessions', -
tests/urls.py
diff --git a/tests/urls.py b/tests/urls.py index 01d6408..893836a 100644
a b urlpatterns = patterns('', 29 29 (r'widget_admin/', include('regressiontests.admin_widgets.urls')), 30 30 31 31 (r'^utils/', include('regressiontests.utils.urls')), 32 32 33 33 # test urlconf for syndication tests 34 34 (r'^syndication/', include('regressiontests.syndication.urls')), 35 35