Skip to content

Commit

Permalink
fix simple theme (make it work with django 1.5); versio up 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Oct 11, 2014
1 parent d07475a commit 5d519c1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/fobi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = 'django-fobi'
__version__ = '0.1.1'
__build__ = 0x000002
__version__ = '0.1.2'
__build__ = 0x000003
__author__ = 'Artur Barseghyan <[email protected]>'
__copyright__ = 'Copyright (c) 2014 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load admin_static %}{% load firstof from future %}<!DOCTYPE html>
{% load admin_static future_compat %}{#% load firstof from future %#}<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}{% block html-attributes %}{% endblock html-attributes %}>
<head>
<title>{% block page-title %}{% endblock page-title %} | {% block site-title %}{% endblock site-title %}</title>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load static %}{% load firstof from future %}<!DOCTYPE html>
{% load static future_compat %}{#% load firstof from future %#}<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}{% block html-attributes %}{% endblock html-attributes %}>
<head>
<title>{% block page-title %}{% endblock page-title %} | {% block site-title %}{% endblock site-title %}</title>
Expand Down
101 changes: 101 additions & 0 deletions src/fobi/templatetags/future_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
__title__ = 'fobi.templatetags.future_compat'
__author__ = 'Artur Barseghyan <[email protected]>'
__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)

0 comments on commit 5d519c1

Please sign in to comment.