Commits

Anonymous committed 2297e2a

added LocaleurlSitemap

Comments (0)

Files changed (3)

           <input type="submit" value="Set" />
       </noscript>
   </form>
+
+Sitemaps
+========
+
+Localeurl supplies a ``LocaleurlSitemap`` class for more convenient
+creation of sitemaps that include URLs in all available languages,
+based on `this snippet`_.
+
+.. _`this snippet`: http://www.djangosnippets.org/snippets/1620/
+
+To use, just inherit your sitemap classes from
+``localeurl.sitemaps.LocaleurlSitemap`` instead of
+``django.contrib.sitemaps.Sitemap``, and instantiate one for each
+language in your sitemaps dictionary.
+
+Example
+~~~~~~~
+
+The following show how might create a multilingual sitemap::
+
+    from localeurl.sitemaps import LocaleurlSitemap
+
+    # example Sitemap
+    class AdvertisementsSitemap(LocaleurlSitemap):
+        def items(self):
+            return Advertisement.active_objects.all()
+
+    # create each section in all languages
+    sitemaps = {
+        'advertisements-sk': sitemaps.AdvertisementsSitemap('sk'),
+        'advertisements-cs': sitemaps.AdvertisementsSitemap('cs'),
+    }
+
+    # add sitemap into urls
+    urlpatterns = patterns('',
+        url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
+    )   

localeurl/sitemaps.py

+from django.contrib.sitemaps import Sitemap
+
+from localeurl.templatetags.localeurl_tags import chlocale
+
+class LocaleurlSitemap(Sitemap):
+    """
+    From a `snippet by tomas`_.
+
+    .. _`snippet by tomas`: http://www.djangosnippets.org/snippets/1620/
+    """
+    def __init__(self, language):
+        self.language = language
+        
+    def location(self, obj):
+        return chlocale(obj.get_absolute_url(), self.language)

localeurl/tests.py

 from localeurl import middleware
 from localeurl import test_utils
 from localeurl import utils
+from localeurl.sitemaps import LocaleurlSitemap
 from localeurl.templatetags import localeurl_tags
 
 from django.core import urlresolvers
 
         self.assertEqual('/fr/dummy/', self.render_template(
                 '{{"/nl-nl/dummy/"|chlocale:"fr"}}'))
+
+class DummyModel(object):
+    def __init__(self, num):
+        self.num = num
+
+    def get_absolute_url(self):
+        return '/dummy/%s/' % self.num
+
+class DummySitemap(LocaleurlSitemap):
+    def items(self):
+        return [DummyModel(i) for i in range(3)]
+
+class SitemapTestCase(LocaleurlTestCase):
+    def setUp(self):
+        super(SitemapTestCase, self).setUp()
+        class DummySite(object):
+            domain = 'www.example.com'
+        from django.contrib.sites.models import Site
+        self._orig_get_current = Site.objects.get_current
+        Site.objects.get_current = lambda: DummySite()
+    
+    def test_localeurl_sitemap(self):
+        sitemap = DummySitemap('fr')
+        self.assertEqual(sitemap.get_urls()[0]['location'],
+                         'http://www.example.com/fr/dummy/0/')
+
+    def tearDown(self):
+        super(SitemapTestCase, self).tearDown()
+        from django.contrib.sites.models import Site
+        Site.objects.get_current = self._orig_get_current