Device Selection as List when subscribing to Podcast (bug 784)
[mygpo.git] / mygpo / web / templatetags / devices.py
blob5b6ba4e1b797b742c381de501dd3466392ca97d3
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
10 _ = ugettext
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
17 DEVICE_TYPE_ICONS = {
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',
25 @register.filter
26 def device_type(device):
27 return DEVICE_TYPES_DICT.get(device.type, _('Unknown'))
29 @register.filter
30 def device_icon(device):
32 ua_str = (device.user_agent or '').lower()
34 # TODO: better client detection
35 if 'gpodder' in ua_str:
36 icon = 'gpodder.png'
37 caption = 'gPodder'
38 elif 'amarok' in ua_str:
39 icon = 'amarok.png'
40 caption = 'Amarok'
41 elif 'podax' in ua_str:
42 icon = 'podax.png'
43 caption = 'Podax'
45 else:
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)
57 return ''
60 @register.filter
61 def target_uid(devices):
62 if isinstance(devices, list):
63 return devices[0].uid
64 else:
65 return devices.uid
68 @register.filter
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]),
76 name = device.name,
77 icon = device_icon(device),