[Migration] use migrated users
[mygpo.git] / mygpo / web / templatetags / devices.py
blob148f53b00d7b1c910febe4ffa3fc9ea5fecd44d8
1 import os.path
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
14 _ = ugettext
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
21 DEVICE_TYPE_ICONS = {
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',
29 @register.filter
30 def device_type(device):
31 return DEVICE_TYPES_DICT.get(device.type, _('Unknown'))
33 @register.filter
34 def device_icon(device):
36 ua_str = (device.user_agent or '').lower()
38 # TODO: better client detection
39 if 'gpodder' in ua_str:
40 icon = 'gpodder.png'
41 caption = 'gPodder'
42 elif 'amarok' in ua_str:
43 icon = 'amarok.png'
44 caption = 'Amarok'
45 elif 'podax' in ua_str:
46 icon = 'podax.png'
47 caption = 'Podax'
49 else:
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)),
59 caption=caption)
60 return mark_safe(html)
62 return ''
65 @register.filter
66 def target_uid(devices):
67 if isinstance(devices, list):
68 return devices[0].uid
69 else:
70 return devices.uid
73 @register.filter
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]),
81 name = device.name,
82 icon = device_icon(device),
86 @register.filter
87 def device_name(device):
88 """ returns the name of a single device """
89 return strip_tags(device.name or device.uid)
92 @register.filter
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)
99 @register.filter
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)