diff options
author | Pami Ketolainen <pami.ketolainen@jollamobile.com> | 2013-12-12 15:13:30 +0200 |
---|---|---|
committer | Pami Ketolainen <pami.ketolainen@jollamobile.com> | 2014-03-18 15:32:08 +0200 |
commit | 925136f63c53201b64dd17a74a97fc067a4b8788 (patch) | |
tree | fbbac4748881e31135c71ba221066ace635cea87 | |
parent | 0821a96efea80729aaa871fe4e0d3710fbea0a95 (diff) | |
download | askbot-925136f63c53201b64dd17a74a97fc067a4b8788.tar.gz askbot-925136f63c53201b64dd17a74a97fc067a4b8788.tar.bz2 askbot-925136f63c53201b64dd17a74a97fc067a4b8788.zip |
Monkey patch django.utils.html.escape
Lazy strings get double escaped with django 1.5
https://code.djangoproject.com/ticket/20221
-rw-r--r-- | askbot/patches/__init__.py | 8 | ||||
-rw-r--r-- | askbot/patches/django_patches.py | 30 |
2 files changed, 38 insertions, 0 deletions
diff --git a/askbot/patches/__init__.py b/askbot/patches/__init__.py index 6145097c..947e1816 100644 --- a/askbot/patches/__init__.py +++ b/askbot/patches/__init__.py @@ -20,6 +20,14 @@ def patch_django(): if major == 1 and minor <=2: django_patches.add_render_shortcut() + if major == 1 and minor > 4: + # This shouldn't be required with django < 1.4.x + # And not after kee_lazy lands in django.utils.functional + try: + from django.utils.functional import keep_lazy + except ImportError: + django_patches.fix_lazy_double_escape() + def patch_coffin(): """coffin before version 0.3.4 does not have csrf_token template tag. diff --git a/askbot/patches/django_patches.py b/askbot/patches/django_patches.py index fe0e2fe7..5907d794 100644 --- a/askbot/patches/django_patches.py +++ b/askbot/patches/django_patches.py @@ -352,3 +352,33 @@ def add_render_shortcut(): import django.shortcuts django.shortcuts.render = render + + +from django.utils import six +from django.utils.functional import Promise +import django.utils.html + +def fix_lazy_double_escape(): + """ + Wrap django.utils.html.escape to fix the double escape issue visible at + least with field labels with localization + """ + django.utils.html.escape = wrap_escape(django.utils.html.escape) + + +def wrap_escape(func): + """ + Decorator adapted from https://github.com/django/django/pull/1007 + """ + @wraps(func) + def wrapper(*args, **kwargs): + for arg in list(args) + list(six.itervalues(kwargs)): + if isinstance(arg, Promise): + break + else: + return func(*args, **kwargs) + return lazy(func, six.text_type)(*args, **kwargs) + @wraps(wrapper) + def wrapped(*args, **kwargs): + return mark_safe(func(*args, **kwargs)) + return wrapped |