From 8d23b696398f03ebff81a18828f262c67b55aa68 Mon Sep 17 00:00:00 2001 From: Andrei Fokau Date: Thu, 7 Jun 2018 20:39:38 +0200 Subject: [PATCH 1/5] Support default values of viewlet functions. --- viewlet/compat.py | 12 ++++++++++++ viewlet/models.py | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/viewlet/compat.py b/viewlet/compat.py index d35deca..175a5e0 100644 --- a/viewlet/compat.py +++ b/viewlet/compat.py @@ -45,6 +45,18 @@ def get_func_args(func): return inspect.getargspec(func).args +def get_func_defaults(func): + if PY3: + params = inspect.signature(func).parameters + return dict((k, v.default) for k, v in params.items() if not v.empty) + spec = inspect.getargspec(func) + vals = spec.defaults + if not vals: + return {} + keys = spec.args[-len(vals):] + return dict(zip(keys, vals)) + + def import_module(name): if sys.version_info < (2, 7): if django.VERSION < (1, 7): diff --git a/viewlet/models.py b/viewlet/models.py index c5249f4..014a317 100644 --- a/viewlet/models.py +++ b/viewlet/models.py @@ -4,7 +4,8 @@ import warnings from .cache import get_cache -from .compat import smart_text, smart_bytes, get_func_args, import_module +from .compat import smart_text, smart_bytes, get_func_args, \ + get_func_defaults, import_module from .conf import settings from .const import DEFAULT_TIMEOUT from .loaders import render @@ -55,6 +56,7 @@ def register(self, func): """ self.viewlet_func = func self.viewlet_func_args = get_func_args(func) + self.viewlet_func_defaults = get_func_defaults(func) self.has_args = len(self.viewlet_func_args) > 1 if not self.name: @@ -70,8 +72,16 @@ def call_with_refresh(*args, **kwargs): return call_with_refresh def _build_args(self, *args, **kwargs): - viewlet_func_kwargs = dict((self.viewlet_func_args[i], args[i]) for i in range(0, len(args))) - viewlet_func_kwargs.update(dict((k, kwargs[k]) for k in kwargs if k in self.viewlet_func_args)) + viewlet_func_kwargs = self.viewlet_func_defaults.copy() + viewlet_func_kwargs.update(dict( + (self.viewlet_func_args[i], args[i]) + for i in range(0, len(args)) + )) + viewlet_func_kwargs.update(dict( + (k, kwargs[k]) + for k in kwargs + if k in self.viewlet_func_args + )) return [viewlet_func_kwargs.get(arg) for arg in self.viewlet_func_args] def _build_cache_key(self, *args): From 699d4486b9ec486d140ef0a5d8cfd1f549eda425 Mon Sep 17 00:00:00 2001 From: Andrei Fokau Date: Thu, 7 Jun 2018 20:43:37 +0200 Subject: [PATCH 2/5] Add test for default value. --- viewlet/tests/test_viewlet.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/viewlet/tests/test_viewlet.py b/viewlet/tests/test_viewlet.py index d5de614..5d4a5c9 100644 --- a/viewlet/tests/test_viewlet.py +++ b/viewlet/tests/test_viewlet.py @@ -164,6 +164,11 @@ def test_render_tag(self): template = self.get_django_template("

{% viewlet hello_world name= %}

") self.assertRaises(TemplateSyntaxError, self.render, template) + def test_default_value(self): + template = self.get_django_template("

{% viewlet hello_nocache %}

") + html = self.render(template) + self.assertEqual(html.strip(), u'

Hello wurld!\n

') + def test_cached_tag(self): template = self.get_django_template("

{% viewlet hello_cached_timestamp 'world' %}

") html1 = self.render(template) From f829d249d89044b9e1cc15d4749ae08a72b25033 Mon Sep 17 00:00:00 2001 From: Andrei Fokau Date: Fri, 8 Jun 2018 07:46:16 +0200 Subject: [PATCH 3/5] Fix func defaults for py3. --- viewlet/compat.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/viewlet/compat.py b/viewlet/compat.py index 175a5e0..41c56a3 100644 --- a/viewlet/compat.py +++ b/viewlet/compat.py @@ -48,7 +48,9 @@ def get_func_args(func): def get_func_defaults(func): if PY3: params = inspect.signature(func).parameters - return dict((k, v.default) for k, v in params.items() if not v.empty) + return dict((k, v.default) + for k, v in params.items() + if v.default != v.empty) spec = inspect.getargspec(func) vals = spec.defaults if not vals: From 7909d8f28c519dc004a9cc4ca1d66a0544fa7fbe Mon Sep 17 00:00:00 2001 From: Andrei Fokau Date: Fri, 8 Jun 2018 10:47:06 +0200 Subject: [PATCH 4/5] Skip testing for Python 3.3. --- .travis.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4160d2d..10e2f3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,14 +45,14 @@ matrix: env: TOXENV=py36-django111 - python: 3.6 env: TOXENV=py36-django20 - - python: 3.3 - env: TOXENV=py33-django15 - - python: 3.3 - env: TOXENV=py33-django16 - - python: 3.3 - env: TOXENV=py33-django17 - - python: 3.3 - env: TOXENV=py33-django18 +# - python: 3.3 +# env: TOXENV=py33-django15 +# - python: 3.3 +# env: TOXENV=py33-django16 +# - python: 3.3 +# env: TOXENV=py33-django17 +# - python: 3.3 +# env: TOXENV=py33-django18 - python: 3.4 # i.e. skip installing 3.5 env: TOXENV=lint - python: 3.4 From e49cf9a7c3c43a53be44ad3dc10fc3b1839f6b27 Mon Sep 17 00:00:00 2001 From: Andrei Fokau Date: Fri, 8 Jun 2018 11:16:24 +0200 Subject: [PATCH 5/5] Remove 3.3 from tox as well. --- tox.ini | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tox.ini b/tox.ini index d8d7049..8722ac6 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ [tox] envlist = py26-django{ 13, 14, 15, 16 }, py27-django{ 14, 15, 16, 17, 18, 19, 110, 111 }, - py33-django{ 15, 16, 17, 18 }, +# py33-django{ 15, 16, 17, 18 }, py34-django{ 17, 18, 19, 110, 111, 20 }, py35-django{ 18, 19, 110, 111, 20 }, py36-django{ 111, 20 } @@ -33,7 +33,7 @@ deps = django12: Django>=1.2,<1.3 [testenv:coverage] skip_install = true passenv = COVERAGE_FILE -basepython = python3.4 +basepython = python2.7 commands = make coverage deps = @@ -41,7 +41,7 @@ deps = [testenv:lint] -basepython = python3.4 +basepython = python2.7 skip_install = true commands = make lint