Ticket #10944: site_url.2.diff

File site_url.2.diff, 4.8 KB (added by laurentluce, 14 years ago)

add get_url() to RequestSite. Add support for protocol to get_url() and template tag site_url. Update docs.

  • django/contrib/sites/tests.py

     
    33from django.core.exceptions import ObjectDoesNotExist
    44from django.http import HttpRequest
    55from django.test import TestCase
     6from django.template import Template, Context
    67
    78
    89class SitesFrameworkTests(TestCase):
     
    5455        site = get_current_site(request)
    5556        self.assert_(isinstance(site, RequestSite))
    5657        self.assertEqual(site.name, u"example.com")
     58   
     59    def test_site_get_url(self):
     60        # Test absolute URLs builder using relative path for Site
     61        site = Site.objects.get_current()
     62        self.assertEqual(u"http://example.com/test", site.get_url("/test"))
     63        self.assertEqual(u"https://example.com/test", site.get_url("/test", protocol="https"))
     64
     65    def test_request_site_get_url(self):
     66        # Test absolute URLs builder using relative path for RequestSite
     67        Site._meta.installed = False
     68        site = Site.objects.get_current()
     69        self.assertEqual(u"http://example.com/test", site.get_url("/test"))
     70        self.assertEqual(u"https://example.com/test", site.get_url("/test", protocol="https"))
     71
     72    def test_get_site_url_tag(self):
     73        out = Template(
     74                "{% load sites %}"
     75                "{% site_url '/test' %}"
     76            ).render(Context())
     77        self.assertEquals(out, "http://example.com/test")
     78        out = Template(
     79                "{% load sites %}"
     80                "{% site_url '/test' 'https' %}"
     81            ).render(Context())
     82        self.assertEquals(out, "https://example.com/test")
     83
     84    def test_get_request_site_url_tag(self):
     85        Site._meta.installed = False
     86        out = Template(
     87                "{% load sites %}"
     88                "{% site_url '/test' %}"
     89            ).render(Context())
     90        self.assertEquals(out, "http://example.com/test")
     91        out = Template(
     92                "{% load sites %}"
     93                "{% site_url '/test' 'https' %}"
     94            ).render(Context())
     95        self.assertEquals(out, "https://example.com/test")
     96
  • django/contrib/sites/models.py

     
    6161        except KeyError:
    6262            pass
    6363
     64    def get_url(self, path, protocol='http'):
     65        """Returns absolute URLs based on a protocol and given relative path"""
     66        return protocol + '://' + self.domain + path
    6467
    6568class RequestSite(object):
    6669    """
     
    8184
    8285    def delete(self):
    8386        raise NotImplementedError('RequestSite cannot be deleted.')
     87   
     88    def get_url(self, path, protocol='http'):
     89        """Returns absolute URLs based on a protocol and given relative path"""
     90        return protocol + '://' + self.domain + path
    8491
    85 
    8692def get_current_site(request):
    8793    """
    8894    Checks if contrib.sites is installed and returns either the current
  • django/contrib/sites/templatetags/sites.py

     
     1from django import template
     2from django.contrib.sites.models import Site
     3from django.conf import settings
     4
     5register = template.Library()
     6
     7def site_url(path, scheme='http'):
     8    site = Site.objects.get_current()
     9    return site.get_url(path, scheme)
     10
     11register.simple_tag(site_url)
  • docs/ref/contrib/sites.txt

     
    237237    >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())
    238238    'http://example.com/mymodel/objects/3/'
    239239
     240.. versionadded:: 1.3
     241
     242A :class:`~django.contrib.sites.models.Site` object has the method
     243:meth:`~django.contrib.sites.Site.get_url()` to help you build absolute
     244URLs based on a protocol, a domain and a relative path.
     245
     246    >>> Site.objects.get_current().get_url(obj.get_absolute_url())
     247    'http://example.com/mymodel/objects/3/'
     248    >>> Site.objects.get_current().get_url(obj.get_absolute_url(), protocol='https')
     249    'https://example.com/mymodel/objects/3/'
     250
     251The template tag ``site_url`` allows you to build absolute URLs inside a
     252template by specifying a relative path and a protocol.
     253
     254Sample usages::
     255
     256    {% site_url '/test' %}
     257    {% site_url '/test' 'https' %}
     258
    240259Caching the current ``Site`` object
    241260===================================
    242261
Back to Top