Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default values of viewlet functions. #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -33,15 +33,15 @@ 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 =
coverage


[testenv:lint]
basepython = python3.4
basepython = python2.7
skip_install = true
commands =
make lint
Expand Down
14 changes: 14 additions & 0 deletions viewlet/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ 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 v.default != 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):
Expand Down
16 changes: 13 additions & 3 deletions viewlet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions viewlet/tests/test_viewlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ def test_render_tag(self):
template = self.get_django_template("<h1>{% viewlet hello_world name= %}</h1>")
self.assertRaises(TemplateSyntaxError, self.render, template)

def test_default_value(self):
template = self.get_django_template("<h1>{% viewlet hello_nocache %}</h1>")
html = self.render(template)
self.assertEqual(html.strip(), u'<h1>Hello wurld!\n</h1>')

def test_cached_tag(self):
template = self.get_django_template("<h1>{% viewlet hello_cached_timestamp 'world' %}</h1>")
html1 = self.render(template)
Expand Down