apply new navigation to publisher pages
[mygpo.git] / mygpo / web / templatetags / devices.py
blobaeb0c67d9336a086b6ae2b3e4611ce776eb6960c
1 from django import template
2 from django.utils.safestring import mark_safe
3 from django.utils.translation import ugettext
5 _ = ugettext
7 from mygpo.api.models import DEVICE_TYPES
9 register = template.Library()
11 # Create a dictionary of device_type -> caption mappings
12 DEVICE_TYPES_DICT = dict(DEVICE_TYPES)
14 # This dictionary maps device types to their icon files
15 DEVICE_TYPE_ICONS = {
16 'desktop': 'computer.png',
17 'laptop': 'stock_notebook.png',
18 'mobile': 'stock_cell-phone.png',
19 'server': 'server.png',
20 'other': 'audio-x-generic.png',
23 @register.filter
24 def device_type(device):
25 return DEVICE_TYPES_DICT.get(device.type, _('Unknown'))
27 @register.filter
28 def device_icon(device, size=16):
29 icon = DEVICE_TYPE_ICONS.get(device.type, None)
30 caption = DEVICE_TYPES_DICT.get(device.type, None)
32 if icon is not None and caption is not None:
33 caption = ugettext(caption)
34 html = ('<img src="/media/%(size)dx%(size)d/%(icon)s" '+
35 'alt="%(caption)s" class="device_icon"/>') % locals()
36 return mark_safe(html)
38 return ''
40 @register.filter
41 def device_list(devices):
42 return mark_safe('<br/>'.join([ '<a href="/device/%s">%s&nbsp;%s</a>' % (d.id, device_icon(d), d.name.replace(' ', '&nbsp;')) for d in devices]))