[Web] handle SyncGroups as Clients
[mygpo.git] / mygpo / web / templatetags / devices.py
blob570e4d70ec6c8e56d004ed6fab537aaf17fa8bfb
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, SyncGroup
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(client):
67 if isinstance(client, SyncGroup):
68 clients = client.client_set.all()
69 return clients[0].uid
70 else:
71 return client.uid
74 @register.filter
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]),
82 name = device.name,
83 icon = device_icon(device),
87 @register.filter
88 def device_name(client):
89 """ returns the name of a single device """
90 return strip_tags(client.display_name)
93 @register.filter
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)
100 @register.filter
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):
106 clients = [client]
107 return ','.join(client.uid for client in clients)