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
, SyncGroup
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(client
):
67 if isinstance(client
, SyncGroup
):
68 clients
= client
.client_set
.all()
75 def device_list(devices
):
76 links
= map(device_link
, devices
)
77 return mark_safe(''.join(links
))
79 def device_link(device
):
80 return u
'<a href="{link}" title="{name}">{icon}</a>'.format(
81 link
= reverse(show
, args
=[device
.uid
]),
83 icon
= device_icon(device
),
88 def device_name(client
):
89 """ returns the name of a single device """
90 return strip_tags(client
.display_name
)
94 def devices_name(devices
):
95 """ returns the name of a single device, or of a list of devices """
96 devices
= devices
if isinstance(devices
, (list, tuple)) else [devices
]
97 return ', '.join(device_name(device
) for device
in devices
)
101 def devices_uids(client
):
102 """ returns a comma-separated list of UIDs of one or more devices """
103 if isinstance(client
, SyncGroup
):
104 clients
= client
.client_set
.all()
105 elif isinstance(client
, Client
):
107 return ','.join(client
.uid
for client
in clients
)