diff --git a/CHANGELOG.rst b/CHANGELOG.rst index dfcb70b48..ce0ae80a5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,5 +1,11 @@ Release history ===================================== +0.1.2 +------------------------------------- +2014-10-11 + +- Simple theme fixes: Fix for making the theme work in Django 1.5. + 0.1.1 ------------------------------------- 2014-10-11 diff --git a/setup.py b/setup.py index 3b24950d2..87295c68e 100644 --- a/setup.py +++ b/setup.py @@ -56,7 +56,7 @@ for locale_dir in locale_dirs: locale_files += [os.path.join(locale_dir, f) for f in os.listdir(locale_dir)] -version = '0.1.1' +version = '0.1.2' install_requires = [ 'Pillow>=2.0.0', diff --git a/src/fobi/__init__.py b/src/fobi/__init__.py index 1f2f9f9f7..92abda9d1 100644 --- a/src/fobi/__init__.py +++ b/src/fobi/__init__.py @@ -1,6 +1,6 @@ __title__ = 'django-fobi' -__version__ = '0.1.1' -__build__ = 0x000002 +__version__ = '0.1.2' +__build__ = 0x000003 __author__ = 'Artur Barseghyan ' __copyright__ = 'Copyright (c) 2014 Artur Barseghyan' __license__ = 'GPL 2.0/LGPL 2.1' diff --git a/src/fobi/contrib/themes/simple/templates/simple/base_edit.html b/src/fobi/contrib/themes/simple/templates/simple/base_edit.html index cce070a01..1ea5975e2 100644 --- a/src/fobi/contrib/themes/simple/templates/simple/base_edit.html +++ b/src/fobi/contrib/themes/simple/templates/simple/base_edit.html @@ -1,4 +1,4 @@ -{% load admin_static %}{% load firstof from future %} +{% load admin_static future_compat %}{#% load firstof from future %#} {% block page-title %}{% endblock page-title %} | {% block site-title %}{% endblock site-title %} diff --git a/src/fobi/contrib/themes/simple/templates/simple/base_view.html b/src/fobi/contrib/themes/simple/templates/simple/base_view.html index 17004979a..d0ce01d8d 100644 --- a/src/fobi/contrib/themes/simple/templates/simple/base_view.html +++ b/src/fobi/contrib/themes/simple/templates/simple/base_view.html @@ -1,4 +1,4 @@ -{% load static %}{% load firstof from future %} +{% load static future_compat %}{#% load firstof from future %#} {% block page-title %}{% endblock page-title %} | {% block site-title %}{% endblock site-title %} diff --git a/src/fobi/templatetags/future_compat.py b/src/fobi/templatetags/future_compat.py new file mode 100644 index 000000000..9b6d74292 --- /dev/null +++ b/src/fobi/templatetags/future_compat.py @@ -0,0 +1,101 @@ +__title__ = 'fobi.templatetags.future_compat' +__author__ = 'Artur Barseghyan ' +__copyright__ = 'Copyright (c) 2014 Artur Barseghyan' +__license__ = 'GPL 2.0/LGPL 2.1' +__all__ = ('firstof',) + +try: + # We're using the Django 1.6 admin templates, that make use of new + # things. One of the new additions (changed) was the ``firstof`` + # template tag. If we can't import it, we simply define it outselves + from django.template.deafulttags import firstof +except ImportError: + import warnings + + from django.template.base import ( + Node, TemplateSyntaxError, Library + ) + from django.utils.timezone import template_localtime + from django.utils.formats import localize + from django.utils.encoding import force_text + from django.utils.html import escape + from django.utils.safestring import mark_safe, EscapeData, SafeData + + register = Library() + + def render_value_in_context(value, context): + """ + Converts any value to a string to become part of a rendered template. This + means escaping, if required, and conversion to a unicode object. If value + is a string, it is expected to have already been translated. + """ + value = template_localtime(value, use_tz=context.use_tz) + value = localize(value, use_l10n=context.use_l10n) + value = force_text(value) + if ((context.autoescape and not isinstance(value, SafeData)) or + isinstance(value, EscapeData)): + return escape(value) + else: + return value + + class FirstOfNode(Node): + def __init__(self, variables, escape=False): + self.vars = variables + self.escape = escape # only while the "future" version exists + + def render(self, context): + for var in self.vars: + value = var.resolve(context, True) + if value: + if not self.escape: + value = mark_safe(value) + return render_value_in_context(value, context) + return '' + + + @register.tag + def firstof(parser, token, escape=False): + """ + Outputs the first variable passed that is not False, without escaping. + + Outputs nothing if all the passed variables are False. + + Sample usage:: + + {% firstof var1 var2 var3 %} + + This is equivalent to:: + + {% if var1 %} + {{ var1|safe }} + {% elif var2 %} + {{ var2|safe }} + {% elif var3 %} + {{ var3|safe }} + {% endif %} + + but obviously much cleaner! + + You can also use a literal string as a fallback value in case all + passed variables are False:: + + {% firstof var1 var2 var3 "fallback value" %} + + If you want to escape the output, use a filter tag:: + + {% filter force_escape %} + {% firstof var1 var2 var3 "fallback value" %} + {% endfilter %} + + """ + if not escape: + warnings.warn( + "'The `firstof` template tag is changing to escape its arguments; " + "the non-autoescaping version is deprecated. Load it " + "from the `future` tag library to start using the new behavior.", + DeprecationWarning, stacklevel=2) + + bits = token.split_contents()[1:] + if len(bits) < 1: + raise TemplateSyntaxError("'firstof' statement requires at least one argument") + return FirstOfNode([parser.compile_filter(bit) for bit in bits], escape=escape)