3 from django import template
 
   4 from django.utils.encoding import smart_unicode
 
   5 from django.utils.safestring import mark_safe
 
   7 from filebrowser.fb_settings import SELECT_FORMATS
 
   9 register = template.Library()
 
  11 @register.inclusion_tag('filebrowser/include/_response.html', takes_context = True)
 
  12 def query_string(context, add = None, remove = None):
 
  14     Allows the addition and removal of query string parameters.
 
  16     _response.html is just {{ response }}
 
  19     http://www.url.com/{% query_string "param_to_add=value, param_to_add=value" "param_to_remove, params_to_remove" %}
 
  20     http://www.url.com/{% query_string "" "filter" %}filter={{new_filter}}
 
  21     http://www.url.com/{% query_string "sort=value" "sort" %}
 
  23     # Written as an inclusion tag to simplify getting the context.
 
  24     add = string_to_dict(add)
 
  25     remove = string_to_list(remove)
 
  26     params = context['query'].copy()
 
  27     response = get_query_string(params, add, remove)
 
  28     return {'response': smart_unicode(response) }
 
  31 def query_helper(query, add = None, remove = None):
 
  33     Helper Function for use within views.
 
  35     add = string_to_dict(add)
 
  36     remove = string_to_list(remove)
 
  38     return get_query_string(params, add, remove)
 
  41 def get_query_string(p, new_params = None, remove = None):
 
  43     Add and remove query parameters. From `django.contrib.admin`.
 
  45     if new_params is None: new_params = {}
 
  46     if remove is None: remove = []
 
  51     for k, v in new_params.items():
 
  52         if k in p and v is None:
 
  56     return mark_safe('?' + '&'.join([u'%s=%s' % (k, v) for k, v in p.items()]).replace(' ', '%20'))
 
  59 def string_to_dict(string):
 
  63         {{ url|thumbnail:"width=10,height=20" }}
 
  64         {{ url|thumbnail:"width=10" }}
 
  65         {{ url|thumbnail:"height=20" }}
 
  71             # ensure at least one ','
 
  73         for arg in string.split(','):
 
  75             if arg == '': continue
 
  76             kw, val = arg.split('=', 1)
 
  81 def string_to_list(string):
 
  85         {{ url|thumbnail:"width,height" }}
 
  91             # ensure at least one ','
 
  93         for arg in string.split(','):
 
  95             if arg == '': continue
 
 100 class SelectableNode(template.Node):
 
 101     def __init__(self, filetype, format):
 
 102         self.filetype = template.Variable(filetype)
 
 103         self.format = template.Variable(format)
 
 105     def render(self, context):
 
 107             filetype = self.filetype.resolve(context)
 
 108         except template.VariableDoesNotExist:
 
 111             format = self.format.resolve(context)
 
 112         except template.VariableDoesNotExist:
 
 114         if filetype and format and filetype in SELECT_FORMATS[format]:
 
 116         elif filetype and format and filetype not in SELECT_FORMATS[format]:
 
 120         context['selectable'] = selectable
 
 124 def selectable(parser, token):
 
 127         tag, filetype, format = token.split_contents()
 
 129         raise TemplateSyntaxError, "%s tag requires 2 arguments" % token.contents.split()[0]
 
 131     return SelectableNode(filetype, format)
 
 133 register.tag(selectable)