Ticket #22782: i18nsitemap.2.diff

File i18nsitemap.2.diff, 7.6 KB (added by luanpab, 10 years ago)
  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index 472d53c..c86c734 100644
    a b answer newbie questions, and generally made Django that much better:  
    483483    oggie rob <oz.robharvey@gmail.com>
    484484    oggy <ognjen.maric@gmail.com>
    485485    Tomek Paczkowski <tomek@hauru.eu>
     486    Luan Pablo <luanpab@gmail.com>
    486487    Jens Page
    487488    Guillaume Pannatier <guillaume.pannatier@gmail.com>
    488489    Jay Parlar <parlar@gmail.com>
  • django/contrib/sitemaps/__init__.py

    diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py
    index 1100712..f4f73ee 100644
    a b  
    11from django.apps import apps as django_apps
     2from django.conf import settings
    23from django.core import urlresolvers, paginator
    34from django.core.exceptions import ImproperlyConfigured
     5from django.utils import translation
    46from django.utils.six.moves.urllib.parse import urlencode
    57from django.utils.six.moves.urllib.request import urlopen
    68
    class Sitemap(object):  
    8688                except Site.DoesNotExist:
    8789                    pass
    8890            if site is None:
    89                 raise ImproperlyConfigured("To use sitemaps, either enable the sites framework or pass a Site/RequestSite object in your view.")
     91                raise ImproperlyConfigured(
     92                    "To use sitemaps, either enable the sites framework or "
     93                    "pass a Site/RequestSite object in your view."
     94                )
    9095        domain = site.domain
    9196
     97        if getattr(self, 'i18n', False):
     98            urls = []
     99            current_lang_code = translation.get_language()
     100            for lang_code, lang_name in settings.LANGUAGES:
     101                translation.activate(lang_code)
     102                urls += self._urls(page, protocol, domain)
     103            translation.activate(current_lang_code)
     104        else:
     105            urls = self._urls(page, protocol, domain)
     106
     107        return urls
     108
     109    def _urls(self, page, protocol, domain):
    92110        urls = []
    93111        latest_lastmod = None
    94112        all_items_lastmod = True  # track if all items have a lastmod
  • django/contrib/sitemaps/tests/base.py

    diff --git a/django/contrib/sitemaps/tests/base.py b/django/contrib/sitemaps/tests/base.py
    index 2d0a7bf..54446b3 100644
    a b  
    11from django.apps import apps
    22from django.core.cache import cache
     3from django.core.urlresolvers import reverse
    34from django.db import models
    45from django.test import TestCase, override_settings
    56
    class TestModel(models.Model):  
    1718        return '/testmodel/%s/' % self.id
    1819
    1920
     21class I18nTestModel(models.Model):
     22    name = models.CharField(max_length=100)
     23
     24    class Meta:
     25        app_label = 'sitemaps'
     26
     27    def get_absolute_url(self):
     28        return reverse('i18n_testmodel', args=[self.id])
     29
     30
    2031@override_settings(ROOT_URLCONF='django.contrib.sitemaps.tests.urls.http')
    2132class SitemapTestsBase(TestCase):
    2233    protocol = 'http'
    class SitemapTestsBase(TestCase):  
    2839        cache.clear()
    2940        # Create an object for sitemap content.
    3041        TestModel.objects.create(name='Test Object')
     42        I18nTestModel.objects.create(name='Test Object')
  • django/contrib/sitemaps/tests/test_http.py

    diff --git a/django/contrib/sitemaps/tests/test_http.py b/django/contrib/sitemaps/tests/test_http.py
    index 2f9ebd8..bce276d 100644
    a b class HTTPSitemapTests(SitemapTestsBase):  
    171171    def test_empty_sitemap(self):
    172172        response = self.client.get('/empty/sitemap.xml')
    173173        self.assertEqual(response.status_code, 200)
     174
     175    @override_settings(LANGUAGES=(('en', 'English'), ('pt', 'Portuguese')))
     176    def test_simple_i18nsitemap_index(self):
     177        "A simple i18n sitemap index can be rendered"
     178        response = self.client.get('/simple/i18n.xml')
     179        expected_content = """<?xml version="1.0" encoding="UTF-8"?>
     180<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     181<url><loc>{0}/en/i18n/testmodel/1/</loc><changefreq>never</changefreq><priority>0.5</priority></url><url><loc>{0}/pt/i18n/testmodel/1/</loc><changefreq>never</changefreq><priority>0.5</priority></url>
     182</urlset>
     183""".format(self.base_url)
     184        self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
  • django/contrib/sitemaps/tests/urls/http.py

    diff --git a/django/contrib/sitemaps/tests/urls/http.py b/django/contrib/sitemaps/tests/urls/http.py
    index 1ec292c..d7bc829 100644
    a b  
    11from datetime import datetime
    22from django.conf.urls import url
     3from django.conf.urls.i18n import i18n_patterns
    34from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views
     5from django.http import HttpResponse
    46from django.views.decorators.cache import cache_page
    57
    6 from django.contrib.sitemaps.tests.base import TestModel
     8from django.contrib.sitemaps.tests.base import I18nTestModel, TestModel
    79
    810
    911class SimpleSitemap(Sitemap):
    class SimpleSitemap(Sitemap):  
    1618        return [object()]
    1719
    1820
     21class SimpleI18nSitemap(Sitemap):
     22    changefreq = "never"
     23    priority = 0.5
     24    i18n = True
     25
     26    def items(self):
     27        return I18nTestModel.objects.all()
     28
     29
    1930class EmptySitemap(Sitemap):
    2031    changefreq = "never"
    2132    priority = 0.5
    class FixedLastmodMixedSitemap(Sitemap):  
    4253        return [o1, o2]
    4354
    4455
     56def testmodelview(request, id):
     57    return HttpResponse()
     58
     59
    4560simple_sitemaps = {
    4661    'simple': SimpleSitemap,
    4762}
    4863
     64simple_i18nsitemaps = {
     65    'simple': SimpleI18nSitemap,
     66}
     67
    4968empty_sitemaps = {
    5069    'empty': EmptySitemap,
    5170}
    urlpatterns = [  
    7493    url(r'^simple/sitemap-(?P<section>.+)\.xml$', views.sitemap,
    7594        {'sitemaps': simple_sitemaps}),
    7695    url(r'^simple/sitemap\.xml$', views.sitemap, {'sitemaps': simple_sitemaps}),
     96    url(r'^simple/i18n\.xml$', views.sitemap, {'sitemaps': simple_i18nsitemaps}),
    7797    url(r'^simple/custom-sitemap\.xml$', views.sitemap,
    7898        {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
    7999    url(r'^empty/sitemap\.xml$', views.sitemap, {'sitemaps': empty_sitemaps}),
    urlpatterns = [  
    86106    url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap),
    87107        {'sitemaps': simple_sitemaps}, name='cached_sitemap')
    88108]
     109
     110urlpatterns += i18n_patterns(
     111    url(r'^i18n/testmodel/(?P<id>\d+)/$', testmodelview, name='i18n_testmodel'),
     112)
  • docs/ref/contrib/sitemaps.txt

    diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt
    index 12ed61b..955a325 100644
    a b Sitemap class reference  
    231231        sitemap was requested is used. If the sitemap is built outside the
    232232        context of a request, the default is ``'http'``.
    233233
     234    .. attribute:: Sitemap.i18n
     235
     236        **Optional.**
     237
     238        A boolean attribute that defines if the URLs of this class are using i18n.
     239        If ``True`` it will generate your sitemap based on all of your :setting:`LANGUAGES`,
     240        the default is ``False``.
     241
     242        .. versionadded:: 1.8
    234243
    235244Shortcuts
    236245=========
  • docs/releases/1.8.txt

    diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt
    index f716c99..df150e0 100644
    a b Minor features  
    7878:mod:`django.contrib.sitemaps`
    7979^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    8080
    81 * ...
     81* Added :attr:`~django.contrib.sitemaps.Sitemap.i18n` class attribute to
     82  :class:`django.contrib.Sitemap`. It is now possible to have all your multiple
     83  language URLs in your sitemap.
    8284
    8385:mod:`django.contrib.sites`
    8486^^^^^^^^^^^^^^^^^^^^^^^^^^^
Back to Top