Ticket #10944: 10944_v3.diff
File 10944_v3.diff, 7.6 KB (added by , 12 years ago) |
---|
-
new file django/contrib/sites/templatetags/__init__.py
diff --git a/django/contrib/sites/templatetags/__init__.py b/django/contrib/sites/templatetags/__init__.py new file mode 100644 index 0000000..8b13789
- + 1 -
new file django/contrib/sites/templatetags/sites.py
diff --git a/django/contrib/sites/templatetags/sites.py b/django/contrib/sites/templatetags/sites.py new file mode 100644 index 0000000..cc408aa
- + 1 from django import template 2 from django.contrib.sites.models import Site 3 from django.conf import settings 4 from django.template.base import Node, Token 5 from django.template.defaulttags import url 6 7 8 register = template.Library() 9 10 11 @register.tag 12 def site_url(parser, token): 13 #import ipdb; ipdb.set_trace() 14 bits = token.split_contents() 15 if len(bits) < 2: 16 raise TemplateSyntaxError("'%s' needs an argument describing " 17 "path to a view." % bits[0]) 18 site = parser.compile_filter(bits[1]) 19 20 # handle "site_url site using https" 21 protocol = 'http' 22 if len(bits) > 2 and bits[2] == 'using': 23 if len(bits) < 4: 24 raise TemplateSyntaxError("'%s' needs a protocol after " 25 "'using' word." % bits[0]) 26 protocol = bits[3] 27 del bits[2:4] # delete "using <protocol>" 28 del bits[1] # delete site argument 29 30 content = " ".join(bits) 31 new_token = Token(token.token_type, content) 32 url_node = url(parser, new_token) 33 return SiteURLNode(protocol, site, url_node) 34 35 36 class SiteURLNode(Node): 37 def __init__(self, protocol, site, url_node): 38 self._protocol = protocol 39 self._url_node = url_node 40 self._site = site 41 42 def render(self, context): 43 relative_path = self._url_node.render(context) # "/view" 44 domain = self._site.resolve(context, True).domain # "example.com" 45 return self._protocol + "://" + domain + relative_path 46 -
docs/ref/contrib/sites.txt
diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt index 8fc434b..e7e2dd3 100644
a b fallback for cases where it is not installed. 174 174 .. function:: get_current_site(request) 175 175 176 176 Checks if contrib.sites is installed and returns either the current 177 :class:`~django.contrib.sites.models.Site` object or a 177 :class:`~django.contrib.sites.models.Site` object or a 178 178 :class:`~django.contrib.sites.models.RequestSite` object based on 179 179 the request. 180 180 … … gives you more flexibility, but it's also more complex. 240 240 It's a good idea to exploit the :class:`~django.contrib.sites.models.Site` 241 241 objects as much as possible, to remove unneeded complexity and redundancy. 242 242 243 Getting the current domain for full URLs 244 ---------------------------------------- 243 ``site_url`` tag for building full URLs 244 --------------------------------------- 245 246 Django's ``get_absolute_url()`` convention is nice for getting your 247 objects' URL without the domain name, but in some cases you might want 248 to display the full URL -- with ``http://`` and the domain and 249 everything -- for an object. To do this, you can use ``site_url`` tag 250 which behave in similar way to ``url`` tag except there must be site 251 specified as first argument:: 252 253 {% site_url site 'myapp.views.viewname' %} 245 254 246 Django's ``get_absolute_url()`` convention is nice for getting your objects' 247 URL without the domain name, but in some cases you might want to display the 248 full URL -- with ``http://`` and the domain and everything -- for an object. 249 To do this, you can use the sites framework. A simple example:: 255 The result of rendering that template is 256 ``http://example.com/path/to/view``. 257 258 If you want you can also specify another protocol (the default is 259 ``http``). To do this add ``using`` and protocol after site argument, 260 see example:: 261 262 {% site_url site using https 'myapp.views.viewname' %} 250 263 251 >>> from django.contrib.sites.models import Site252 >>> obj = MyModel.objects.get(id=3)253 >>> obj.get_absolute_url()254 '/mymodel/objects/3/'255 >>> Site.objects.get_current().domain256 'example.com'257 >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url())258 'http://example.com/mymodel/objects/3/'259 264 260 265 Caching the current ``Site`` object 261 266 =================================== … … fallback when the database-backed sites framework is not available. 437 442 438 443 Sets the ``name`` and ``domain`` attributes to the value of 439 444 :meth:`~django.http.HttpRequest.get_host`. 440 445 441 446 442 447 A :class:`~django.contrib.sites.models.RequestSite` object has a similar 443 448 interface to a normal :class:`~django.contrib.sites.models.Site` object, except -
tests/regressiontests/sites_framework/tests.py
diff --git a/tests/regressiontests/sites_framework/tests.py b/tests/regressiontests/sites_framework/tests.py index 8e664fd..764dac2 100644
a b from __future__ import absolute_import 2 2 3 3 from django.conf import settings 4 4 from django.contrib.sites.models import Site 5 from django.template import Context, Template 5 6 from django.test import TestCase 6 7 7 8 from .models import (SyndicatedArticle, ExclusiveArticle, CustomArticle, … … from .models import (SyndicatedArticle, ExclusiveArticle, CustomArticle, 9 10 10 11 11 12 class SitesFrameworkTestCase(TestCase): 13 urls = 'regressiontests.sites_framework.urls' 14 12 15 def setUp(self): 13 16 Site.objects.get_or_create(id=settings.SITE_ID, domain="example.com", name="example.com") 14 17 Site.objects.create(id=settings.SITE_ID+1, domain="example2.com", name="example2.com") … … class SitesFrameworkTestCase(TestCase): 36 39 def test_invalid_field_type(self): 37 40 article = ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID) 38 41 self.assertRaises(TypeError, ConfusedArticle.on_site.all) 42 43 def test_site_url_tag(self): 44 site = Site.objects.get(domain='example2.com') 45 template = Template( 46 "{% load sites %}" 47 "{% site_url site 'regressiontests.sites_framework.views.view' %}") 48 rendered = template.render(Context({'site':site})) 49 self.assertEquals(rendered, "http://example2.com/lala") 50 51 def test_site_url_tag_without_sites_framework_installed(self): 52 Site._meta.installed = False 53 self.test_site_url_tag() 54 55 def test_site_url_tag_with_https(self): 56 site = Site.objects.get(domain='example2.com') 57 template = Template( 58 "{% load sites %}" 59 "{% site_url site using https " 60 "'regressiontests.sites_framework.views.view' %}") 61 rendered = template.render(Context({'site':site})) 62 self.assertEquals(rendered, "https://example2.com/lala") 63 64 def test_site_url_tag_with_https_without_sites_framework_installed(self): 65 Site._meta.installed = False 66 self.test_site_url_tag_with_https() -
new file tests/regressiontests/sites_framework/urls.py
diff --git a/tests/regressiontests/sites_framework/urls.py b/tests/regressiontests/sites_framework/urls.py new file mode 100644 index 0000000..4eccd1e
- + 1 # coding: utf-8 2 from __future__ import absolute_import, unicode_literals 3 from django.conf.urls import patterns 4 5 from . import views 6 7 8 urlpatterns = patterns('', 9 # Test urls for testing reverse lookups 10 (r'^lala$', views.view), 11 ) 12 -
new file tests/regressiontests/sites_framework/views.py
diff --git a/tests/regressiontests/sites_framework/views.py b/tests/regressiontests/sites_framework/views.py new file mode 100644 index 0000000..2c1e8e8
- + 1 def view(request): 2 pass