Commit 727abc55 authored by Alex Pyrgiotis's avatar Alex Pyrgiotis Committed by Athina Bekakou

admin: Update template tags

Add Django template tag that returns the filter type for a given filter
and add a choice template filter that returns a list with the available
choices for a filter (only for MultipleChoiceFilter types).

Also, use an array of filters instead of generator in the templatetags
context, since the latter cannot be re-iterated.
parent d5b11222
......@@ -93,7 +93,7 @@ def catalog(request):
"""List view for Cyclades ip log."""
context = {}
context['action_dict'] = None
context['filter_dict'] = IPLogFilterSet().filters.itervalues()
context['filter_dict'] = IPLogFilterSet().filters.values()
context['columns'] = ["Address", "Server ID", "Network ID",
"Allocation date", "Release date", "Active", ""]
context['item_type'] = 'ip_log'
......
......@@ -171,7 +171,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = IPFilterSet().filters.itervalues()
context['filter_dict'] = IPFilterSet().filters.values()
context['columns'] = ["ID", "Address", "Floating",
"Creation date", "User ID", ""]
context['item_type'] = 'ip'
......
......@@ -163,7 +163,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = NetworkFilterSet().filters.itervalues()
context['filter_dict'] = NetworkFilterSet().filters.values()
context['columns'] = ["ID", "Name", "Status", "Public",
"Drained", ""]
context['item_type'] = 'network'
......
......@@ -212,7 +212,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = ProjectFilterSet().filters.itervalues()
context['filter_dict'] = ProjectFilterSet().filters.values()
context['columns'] = ["ID", "Name", "Owner Name", "Project Status",
"Application Status", "Creation date", "End date",
""]
......
......@@ -182,7 +182,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = UserFilterSet().filters.itervalues()
context['filter_dict'] = UserFilterSet().filters.values()
context['columns'] = ["ID", "E-mail", "First Name", "Last Name", "Active",
"Rejected", "Moderated", "Verified", ""]
context['item_type'] = 'user'
......
......@@ -190,7 +190,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = VMFilterSet().filters.itervalues()
context['filter_dict'] = VMFilterSet().filters.values()
context['columns'] = ["ID", "Name", "State", "Suspended", ""]
context['item_type'] = 'vm'
......
......@@ -185,7 +185,7 @@ def catalog(request):
context = {}
context['action_dict'] = get_permitted_actions(cached_actions,
request.user)
context['filter_dict'] = VolumeFilterSet().filters.itervalues()
context['filter_dict'] = VolumeFilterSet().filters.values()
context['columns'] = ["ID", "Name", "Status", "Size (GB)", "Disk template",
"VM ID", "Created at", "Updated at", ""]
context['item_type'] = 'volume'
......
......@@ -203,11 +203,8 @@ def get_details_template(type):
@register.filter
def get_filter_template(filter):
"""Get the correct flter template according to the filter type.
This only works for filters that are instances of django-filter's Filter.
"""
def get_filter_type(filter):
"""Get the flter type according to the filter class."""
if isinstance(filter, django_filters.NumberFilter):
type = "number"
elif isinstance(filter, django_filters.CharFilter):
......@@ -220,10 +217,26 @@ def get_filter_template(filter):
type = "multichoice"
else:
raise Exception("Unknown filter type: %s", filter)
return type
@register.filter
def get_filter_template(filter):
"""Get the correct flter template according to the filter type.
This only works for filters that are instances of django-filter's Filter.
"""
type = get_filter_type(filter)
template = 'admin/filter_' + type + '.html'
return template
@register.filter
def choices(filter):
"""Return an iterator with the available choices for a filter."""
return [choice for choice, _ in filter.field.choices]
@register.filter
def id(item):
try:
......@@ -399,7 +412,7 @@ def label_to_icon(filter_name, filter_label):
if icon_cls:
label = '<span class="%s"></span>' % icon_cls
else:
label = filter_label
label = filter_label + ":"
return label
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment