Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / web / templatetags / devices.py
blob5a700f5b5fd5f7fd249a6342b4e7be77816f025b
1 import os.path
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
15 _ = gettext
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
22 DEVICE_TYPE_ICONS = {
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",
31 @register.filter
32 def device_type(device):
33 return DEVICE_TYPES_DICT.get(device.type, _("Unknown"))
36 @register.filter()
37 def device_icon(device):
39 ua_str = (device.user_agent or "").lower()
41 # TODO: better client detection
42 if "gpodder" in ua_str:
43 icon = "gpodder.png"
44 caption = "gPodder"
45 elif "amarok" in ua_str:
46 icon = "amarok.png"
47 caption = "Amarok"
48 elif "podax" in ua_str:
49 icon = "podax.png"
50 caption = "Podax"
52 else:
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)
64 return ""
67 @register.filter
68 def target_uid(client):
69 if isinstance(client, SyncGroup):
70 clients = client.client_set.all()
71 return clients[0].uid
72 else:
73 return client.uid
76 @register.filter()
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]),
85 name=device.name,
86 icon=device_icon(device),
90 @register.filter
91 def device_name(client):
92 """returns the name of a single device"""
93 return strip_tags(client.display_name)
96 @register.filter
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)
103 @register.filter
104 def is_syncgroup(obj):
105 return isinstance(obj, SyncGroup)
108 @register.filter
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):
114 clients = [client]
115 return ",".join(client.uid for client in clients)