1 from django
import template
2 from django
.utils
.safestring
import mark_safe
3 from django
.utils
.translation
import ugettext
4 from django
.core
.urlresolvers
import reverse
6 from mygpo
.api
.constants
import DEVICE_TYPES
7 from mygpo
.web
.views
.device
import show
11 register
= template
.Library()
13 # Create a dictionary of device_type -> caption mappings
14 DEVICE_TYPES_DICT
= dict(DEVICE_TYPES
)
16 # This dictionary maps device types to their icon files
18 'desktop': 'computer.png',
19 'laptop': 'stock_notebook.png',
20 'mobile': 'stock_cell-phone.png',
21 'server': 'server.png',
22 'other': 'audio-x-generic.png',
26 def device_type(device
):
27 return DEVICE_TYPES_DICT
.get(device
.type, _('Unknown'))
30 def device_icon(device
):
32 ua_str
= (device
.user_agent
or '').lower()
34 # TODO: better client detection
35 if 'gpodder' in ua_str
:
38 elif 'amarok' in ua_str
:
41 elif 'podax' in ua_str
:
46 device_type
= device
.type
47 icon
= DEVICE_TYPE_ICONS
.get(device_type
, None)
48 caption
= DEVICE_TYPES_DICT
.get(device_type
, None)
51 if icon
is not None and caption
is not None:
52 caption
= ugettext(caption
)
53 html
= ('<img src="/media/clients/%(icon)s" '+
54 'alt="%(caption)s" class="device_icon"/>') % locals()
55 return mark_safe(html
)
61 def target_uid(devices
):
62 if isinstance(devices
, list):
69 def device_list(devices
):
70 links
= map(device_link
, devices
)
71 return mark_safe(''.join(links
))
73 def device_link(device
):
74 return u
'<a href="{link}" title="{name}">{icon}</a>'.format(
75 link
= reverse(show
, args
=[device
.uid
]),
77 icon
= device_icon(device
),