# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import template
from django.conf import settings
from django.utils.safestring import mark_safe

register = template.Library()


@register.simple_tag(takes_context=True)
def get_setting(context, key, default_val="", as_key=None):
    """
    get val form settings and set to context
      {% load swift %}
      {% get_setting "key" default_val "as_key" %}
      {{ as_key }}
      if as_key is None, this tag will return val
    """
    if ("%s" % default_val).startswith('$.'):
        default_val = getattr(settings, default_val[2:])
    val = getattr(settings, key, default_val)
    if not as_key:
        return val
    context[as_key] = val
    return ''


@register.filter(name='boolean_icon')
def boolean_icon(v):
    if v:
        return mark_safe('<i class="fa fa-fw fa-check-circle"/>')
    return ''


@register.filter(name='display_array')
def display_array(objs):
    if not objs:
        return ''
    return ', '.join(['%s' % e for e in objs])

@@ -1029,8 +1029,325 @@
  objs%5D)%0A
+%0A%0A@register.simple_tag%0Adef getvars(request, excludes):%0A    getvars = request.GET.copy()%0A    excludes = excludes.split(',')%0A    for p in excludes:%0A        if p in getvars:%0A            del getvars%5Bp%5D%0A        if len(getvars.keys()) %3E 0:%0A            return %22&%25s%22 %25 getvars.urlencode()%0A        else:%0A            return ''%0A

add tag getvars