From 662dd6a467824b6b0b73329f6606ed3dc3066714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 1 Jan 2018 13:48:35 +0100 Subject: [PATCH 2/2] misc: update Context usage in template strings (#20936) --- tests/test_variadic_url.py | 49 +++++++++++++++++++++++++++------------------- wcs/qommon/emails.py | 3 +-- wcs/qommon/misc.py | 4 ++-- wcs/qommon/template.py | 5 ++--- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/tests/test_variadic_url.py b/tests/test_variadic_url.py index 52353856..e8f274f4 100644 --- a/tests/test_variadic_url.py +++ b/tests/test_variadic_url.py @@ -3,12 +3,21 @@ import pytest from wcs.qommon.misc import get_variadic_url from wcs.qommon.ezt import EZTException +from utilities import create_temporary_pub, clean_temporary_pub -def test_url_unchanged(): +@pytest.fixture +def pub(request): + return create_temporary_pub() + +def teardown_module(module): + clean_temporary_pub() + + +def test_url_unchanged(pub): assert get_variadic_url('http://www.example.net/foobar', {}) == 'http://www.example.net/foobar' -def test_url_scheme(): +def test_url_scheme(pub): assert get_variadic_url('http[https]://www.example.net/foobar', {'https': 's'}) == 'https://www.example.net/foobar' assert get_variadic_url('http[https]://www.example.net/foobar', @@ -32,7 +41,7 @@ def test_url_scheme(): {'https': ''}) == 'http://www.example.net/foo/bar' -def test_url_netloc(): +def test_url_netloc(pub): assert get_variadic_url('http://[hostname]/foobar', {'hostname': 'www.example.net'}) == 'http://www.example.net/foobar' assert get_variadic_url('http://[hostname]/foobar', @@ -44,14 +53,14 @@ def test_url_netloc(): {'hostname': 'www.example.com'}) == 'http://www.example.com/foobar' -def test_url_netloc_port(): +def test_url_netloc_port(pub): assert get_variadic_url('http://www.example.net:[port]/foobar', {'port': '80'}) == 'http://www.example.net:80/foobar' assert get_variadic_url('http://www.example.net:{{ port }}/foobar', {'port': '80'}) == 'http://www.example.net:80/foobar' -def test_url_path(): +def test_url_path(pub): assert get_variadic_url('http://www.example.net/[path]', {'path': 'foobar'}) == 'http://www.example.net/foobar' assert get_variadic_url('http://www.example.net/[path]', @@ -65,7 +74,7 @@ def test_url_path(): {'path': 'foo bar'}) == 'http://www.example.net/foo%20bar' -def test_url_query_variable(): +def test_url_query_variable(pub): assert get_variadic_url('http://www.example.net/foobar?hello=[world]', {'world': 'world'}) == 'http://www.example.net/foobar?hello=world' assert get_variadic_url('http://www.example.net/foobar?hello=[world]', @@ -91,7 +100,7 @@ def test_url_query_variable(): {'world': 'a=b'}) == 'http://www.example.net/foobar?hello=a%3Db' -def test_url_query_key(): +def test_url_query_key(pub): assert get_variadic_url('http://www.example.net/foobar?[hello]=world', {'hello': 'hello'}) == 'http://www.example.net/foobar?hello=world' assert get_variadic_url('http://www.example.net/foobar?[hello]=world', @@ -117,7 +126,7 @@ def test_url_query_key(): {'hello': 'a=b'}) == 'http://www.example.net/foobar?a%3Db=world' -def test_url_query_whole(): +def test_url_query_whole(pub): assert get_variadic_url('http://www.example.net/foobar?[hello]', {'hello': 'hello=world'}) == 'http://www.example.net/foobar?hello=world' @@ -127,7 +136,7 @@ def test_url_query_whole(): {'hello': 'hello=world'}) == 'http://www.example.net/foobar?hello%3Dworld' -def test_url_netloc_port_and_path(): +def test_url_netloc_port_and_path(pub): assert get_variadic_url('http://www.example.net:[port]/foobar/[path]', {'port': '80', 'path': 'baz'}) == 'http://www.example.net:80/foobar/baz' assert get_variadic_url('http://www.example.net:[port]/foobar/[path]', @@ -141,7 +150,7 @@ def test_url_netloc_port_and_path(): {'port': '80', 'path': 'b z'}) == 'http://www.example.net:80/foobar/b%20z' -def test_url_whole(): +def test_url_whole(pub): assert get_variadic_url('[url]', {'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar' @@ -149,7 +158,7 @@ def test_url_whole(): {'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar' -def test_url_server(): +def test_url_server(pub): for url in ('http://www.example.net', 'http://www.example.net/'): assert get_variadic_url('[url]/foobar', {'url': url}) == 'http://www.example.net/foobar' @@ -180,7 +189,7 @@ def test_url_server(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar' -def test_url_server_qs(): +def test_url_server_qs(pub): assert get_variadic_url('[url]?foo=bar', {'url': 'http://www.example.net'}) == 'http://www.example.net/?foo=bar' assert get_variadic_url('[url]?foo=bar', @@ -201,7 +210,7 @@ def test_url_server_qs(): {'url': 'http://www.example.net/'}) == 'http://www.example.net//?foo=bar' -def test_url_server_more(): +def test_url_server_more(pub): assert get_variadic_url('[url]/foobar/json?toto', {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/json?toto' assert get_variadic_url('[url]/foobar/json?toto', @@ -218,7 +227,7 @@ def test_url_server_more(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/json?toto' -def test_url_server_even_more(): +def test_url_server_even_more(pub): assert get_variadic_url('[url]/foobar/json?foo=bar', {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/json?foo=bar' assert get_variadic_url('[url]/foobar/json?foo=bar', @@ -235,7 +244,7 @@ def test_url_server_even_more(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/json?foo=bar' -def test_url_server_even_more_more(): +def test_url_server_even_more_more(pub): assert get_variadic_url('[url]/foobar/baz/json?foo=bar', {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/baz/json?foo=bar' assert get_variadic_url('[url]/foobar/baz/json?foo=bar', @@ -252,7 +261,7 @@ def test_url_server_even_more_more(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/baz/json?foo=bar' -def test_url_whole_with_qs(): +def test_url_whole_with_qs(pub): assert get_variadic_url('[url]', {'url': 'http://www.example.net/?foo=bar'}) == 'http://www.example.net/?foo=bar' @@ -260,7 +269,7 @@ def test_url_whole_with_qs(): {'url': 'http://www.example.net/?foo=bar'}) == 'http://www.example.net/?foo=bar' -def test_url_whole_with_qs_2(): +def test_url_whole_with_qs_2(pub): for url in ('[url]?bar=foo', '[url]&bar=foo', '[url]/?bar=foo'): assert get_variadic_url(url, {'url': 'http://www.example.net/?foo=bar'}) in \ ('http://www.example.net/?bar=foo&foo=bar', 'http://www.example.net/?foo=bar&bar=foo') @@ -274,7 +283,7 @@ def test_url_whole_with_qs_2(): {'url': 'http://www.example.net/'}) == 'http://www.example.net/?bar=foo' -def test_path_missing_var(): +def test_path_missing_var(pub): assert get_variadic_url('http://www.example.net/foobar/[path]', {}) == 'http://www.example.net/foobar/[path]' @@ -283,7 +292,7 @@ def test_path_missing_var(): {}) == 'http://www.example.net/foobar/' -def test_url_base_and_missing_var(): +def test_url_base_and_missing_var(pub): assert get_variadic_url('[url]/foobar/[path]', {'url': 'http://www.example.net'}) == 'http://www.example.net/foobar/[path]' assert get_variadic_url('[url]foobar/[path]', @@ -294,7 +303,7 @@ def test_url_base_and_missing_var(): assert get_variadic_url('{{ url }}foobar/{{ path }}', {'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/' -def test_url_bad_syntax(): +def test_url_bad_syntax(pub): with pytest.raises(EZTException): get_variadic_url('[if-any form_avr]https://example.net/[foo]/', {'foo': 'bar'}) diff --git a/wcs/qommon/emails.py b/wcs/qommon/emails.py index 985282da..4b39723e 100644 --- a/wcs/qommon/emails.py +++ b/wcs/qommon/emails.py @@ -39,7 +39,6 @@ try: except ImportError: docutils = None -from django.template import Context from django.template.loader import render_to_string from django.utils.safestring import mark_safe @@ -168,7 +167,7 @@ def email(subject, mail_body, email_rcpt, replyto = None, bcc = None, except IndexError: pass - context = Context(get_publisher().get_substitution_variables()) + context = get_publisher().get_substitution_variables() context['email_signature'] = footer if text_body: diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 527ba6e8..db1ff2c7 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -37,7 +37,7 @@ except ImportError: from django.conf import settings from django.utils import datetime_safe -from django.template import Template, Context, TemplateSyntaxError, VariableDoesNotExist +from django.template import engines, TemplateSyntaxError, VariableDoesNotExist from quixote import get_publisher, get_response, get_request from quixote.html import htmltext @@ -338,7 +338,7 @@ def get_variadic_url(url, variables, encode_query=True): # django template if '{{' in url or '{%' in url: try: - return Template(url).render(Context(variables)) + return Template(url).render(variables) except (TemplateSyntaxError, VariableDoesNotExist): return url diff --git a/wcs/qommon/template.py b/wcs/qommon/template.py index 7e904f65..a6aff987 100644 --- a/wcs/qommon/template.py +++ b/wcs/qommon/template.py @@ -20,7 +20,7 @@ import glob import xml.etree.ElementTree as ET import django.template -from django.template import (Context as DjangoContext, Template as DjangoTemplate, +from django.template import (engines, TemplateSyntaxError as DjangoTemplateSyntaxError, VariableDoesNotExist as DjangoVariableDoesNotExist) from django.template.loader import render_to_string @@ -460,7 +460,7 @@ class Template(object): if ('{{' in value or '{%' in value) and not ezt_only: # Django template self.render = self.django_render try: - self.template = DjangoTemplate(value) + self.template = engines['django'].from_string(value) except DjangoTemplateSyntaxError as e: if raises: from qommon import _ @@ -484,7 +484,6 @@ class Template(object): return str(self.value) def django_render(self, context={}): - context = DjangoContext(context) try: rendered = self.template.render(context) except (DjangoTemplateSyntaxError, DjangoVariableDoesNotExist) as e: -- 2.15.1