3 from django
import template
4 from django
.utils
.safestring
import mark_safe
5 from django
.utils
.translation
import gettext
6 from django
.utils
.safestring
import mark_safe
7 from django
.urls
import reverse
8 from django
.utils
.html
import strip_tags
9 from django
.contrib
.staticfiles
.storage
import staticfiles_storage
11 from mygpo
.users
.models
import Client
, SyncGroup
12 from mygpo
.users
.views
.device
import show
16 register
= template
.Library()
18 # Create a dictionary of device_type -> caption mappings
19 DEVICE_TYPES_DICT
= dict(Client
.TYPES
)
21 # This dictionary maps device types to their icon files
23 "desktop": "computer.png",
24 "laptop": "stock_notebook.png",
25 "mobile": "stock_cell-phone.png",
26 "server": "server.png",
27 "other": "audio-x-generic.png",
32 def device_type(device
):
33 return DEVICE_TYPES_DICT
.get(device
.type, _("Unknown"))
37 def device_icon(device
):
39 ua_str
= (device
.user_agent
or "").lower()
41 # TODO: better client detection
42 if "gpodder" in ua_str
:
45 elif "amarok" in ua_str
:
48 elif "podax" in ua_str
:
53 device_type
= device
.type
54 icon
= DEVICE_TYPE_ICONS
.get(device_type
, None)
55 caption
= DEVICE_TYPES_DICT
.get(device_type
, None)
57 if icon
is not None and caption
is not None:
58 caption
= gettext(caption
)
59 html
= '<img src="%(icon)s" alt="%(caption)s" class="device_icon"/>' % dict(
60 icon
=staticfiles_storage
.url(os
.path
.join("clients", icon
)), caption
=caption
62 return mark_safe(html
)
68 def target_uid(client
):
69 if isinstance(client
, SyncGroup
):
70 clients
= client
.client_set
.all()
77 def device_list(devices
):
78 links
= map(device_link
, devices
)
79 return mark_safe("".join(links
))
82 def device_link(device
):
83 return '<a href="{link}" title="{name}">{icon}</a>'.format(
84 link
=reverse(show
, args
=[device
.uid
]),
86 icon
=device_icon(device
),
91 def device_name(client
):
92 """returns the name of a single device"""
93 return strip_tags(client
.display_name
)
97 def devices_name(devices
):
98 """returns the name of a single device, or of a list of devices"""
99 devices
= devices
if isinstance(devices
, (list, tuple)) else [devices
]
100 return ", ".join(device_name(device
) for device
in devices
)
104 def is_syncgroup(obj
):
105 return isinstance(obj
, SyncGroup
)
109 def devices_uids(client
):
110 """returns a comma-separated list of UIDs of one or more devices"""
111 if isinstance(client
, SyncGroup
):
112 clients
= client
.client_set
.all()
113 elif isinstance(client
, Client
):
115 return ",".join(client
.uid
for client
in clients
)