3 from django
import template
4 from django
.utils
.safestring
import mark_safe
5 from django
.utils
.translation
import ugettext
6 from django
.core
.urlresolvers
import reverse
7 from django
.utils
.html
import strip_tags
8 from django
.contrib
.staticfiles
.storage
import staticfiles_storage
10 from mygpo
.users
.models
import Client
11 from mygpo
.web
.views
.device
import show
15 register
= template
.Library()
17 # Create a dictionary of device_type -> caption mappings
18 DEVICE_TYPES_DICT
= dict(Client
.TYPES
)
20 # This dictionary maps device types to their icon files
22 'desktop': 'computer.png',
23 'laptop': 'stock_notebook.png',
24 'mobile': 'stock_cell-phone.png',
25 'server': 'server.png',
26 'other': 'audio-x-generic.png',
30 def device_type(device
):
31 return DEVICE_TYPES_DICT
.get(device
.type, _('Unknown'))
34 def device_icon(device
):
36 ua_str
= (device
.user_agent
or '').lower()
38 # TODO: better client detection
39 if 'gpodder' in ua_str
:
42 elif 'amarok' in ua_str
:
45 elif 'podax' in ua_str
:
50 device_type
= device
.type
51 icon
= DEVICE_TYPE_ICONS
.get(device_type
, None)
52 caption
= DEVICE_TYPES_DICT
.get(device_type
, None)
55 if icon
is not None and caption
is not None:
56 caption
= ugettext(caption
)
57 html
= '<img src="%(icon)s" alt="%(caption)s" class="device_icon"/>' \
58 % dict(icon
=staticfiles_storage
.url(os
.path
.join('clients', icon
)),
60 return mark_safe(html
)
66 def target_uid(devices
):
67 if isinstance(devices
, list):
74 def device_list(devices
):
75 links
= map(device_link
, devices
)
76 return mark_safe(''.join(links
))
78 def device_link(device
):
79 return u
'<a href="{link}" title="{name}">{icon}</a>'.format(
80 link
= reverse(show
, args
=[device
.uid
]),
82 icon
= device_icon(device
),
87 def device_name(device
):
88 """ returns the name of a single device """
89 return strip_tags(device
.name
or device
.uid
)
93 def devices_name(devices
):
94 """ returns the name of a single device, or of a list of devices """
95 devices
= devices
if isinstance(devices
, (list, tuple)) else [devices
]
96 return ', '.join(device_name(device
) for device
in devices
)
100 def devices_uids(devices
):
101 """ returns a comma-separated list of UIDs of one or more devices """
102 devices
= devices
if isinstance(devices
, (list, tuple)) else [devices
]
103 return ','.join(device
.uid
for device
in devices
)