def test_django_to_jinja(): env = Environment() env.filters.update({ 'urlize': django_filter_to_jinja2(urlize), 'upper': django_filter_to_jinja2(upper), }) assert env.from_string( '{{ "foo"|upper }} {% filter urlize %}http://example.com{% endfilter %}' ).render() == 'FOO <a href="http://example.com" rel="nofollow">http://example.com</a>'
def floatformat(value, arg=-1): """Builds on top of Django's own version, but adds strict error checking, staying with the philosophy. """ from django.template.defaultfilters import floatformat from coffin.interop import django_filter_to_jinja2 arg = int(arg) # raise exception result = django_filter_to_jinja2(floatformat)(value, arg) if result == '': # django couldn't handle the value raise ValueError(value) return result
def _register_filter(self, name, func, jinja2_only=None): filter_type, can_be_ported = guess_filter_type(func) if filter_type == JINJA2 and not can_be_ported: self.jinja2_filters[name] = func return func elif filter_type == DJANGO and not can_be_ported: if jinja2_only: raise ValueError('This filter cannot be ported to Jinja2.') self.filters[name] = func return func elif jinja2_only: func = django_filter_to_jinja2(func) self.jinja2_filters[name] = func return func else: # register the filter with both engines django_func = jinja2_filter_to_django(func) jinja2_func = django_filter_to_jinja2(func) self.filters[name] = django_func self.jinja2_filters[name] = jinja2_func return (django_func, jinja2_func)
def _register_filter(self, name, func, type=None, jinja2_only=None): assert type in ( None, JINJA2, DJANGO, ) # The user might not specify the language the filter was written # for, but sometimes we can auto detect it. filter_type, can_be_ported = guess_filter_type(func) assert not (filter_type and type) or filter_type == type, \ "guessed filter type (%s) not matching claimed type (%s)" % ( filter_type, type, ) if not filter_type and type: filter_type = type if filter_type == JINJA2: self.jinja2_filters[name] = func if can_be_ported and not jinja2_only: self.filters[name] = jinja2_filter_to_django(func) return func elif filter_type == DJANGO: self.filters[name] = func if not can_be_ported and jinja2_only: raise ValueError('This filter cannot be ported to Jinja2.') if can_be_ported: self.jinja2_filters[name] = django_filter_to_jinja2(func) return func else: django_func = jinja2_filter_to_django(func) jinja2_func = django_filter_to_jinja2(func) if jinja2_only: self.jinja2_filters[name] = jinja2_func return jinja2_func else: # register the filter with both engines self.filters[name] = django_func self.jinja2_filters[name] = jinja2_func return (django_func, jinja2_func)
def _register_filter(self, name, func, type=None, jinja2_only=None): assert type in (None, JINJA2, DJANGO,) # The user might not specify the language the filter was written # for, but sometimes we can auto detect it. filter_type, can_be_ported = guess_filter_type(func) assert not (filter_type and type) or filter_type == type, \ "guessed filter type (%s) not matching claimed type (%s)" % ( filter_type, type, ) if not filter_type and type: filter_type = type if filter_type == JINJA2: self.jinja2_filters[name] = func if can_be_ported and not jinja2_only: self.filters[name] = jinja2_filter_to_django(func) return func elif filter_type == DJANGO: self.filters[name] = func if not can_be_ported and jinja2_only: raise ValueError('This filter cannot be ported to Jinja2.') if can_be_ported: self.jinja2_filters[name] = django_filter_to_jinja2(func) return func else: django_func = jinja2_filter_to_django(func) jinja2_func = django_filter_to_jinja2(func) if jinja2_only: self.jinja2_filters[name] = jinja2_func return jinja2_func else: # register the filter with both engines self.filters[name] = django_func self.jinja2_filters[name] = jinja2_func return (django_func, jinja2_func)