root / snf-cyclades-app / synnefo / helpdesk / templatetags / helpdesk_tags.py @ 91884d63
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or without
|
4 |
# modification, are permitted provided that the following conditions
|
5 |
# are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above copyright
|
8 |
# notice, this list of conditions and the following disclaimer.
|
9 |
#
|
10 |
# 2. Redistributions in binary form must reproduce the above copyright
|
11 |
# notice, this list of conditions and the following disclaimer in the
|
12 |
# documentation and/or other materials provided with the distribution.
|
13 |
#
|
14 |
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 |
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 |
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 |
# SUCH DAMAGE.
|
25 |
#
|
26 |
# The views and conclusions contained in the software and documentation are
|
27 |
# those of the authors and should not be interpreted as representing official
|
28 |
# policies, either expressed or implied, of GRNET S.A.
|
29 |
|
30 |
from django import template |
31 |
|
32 |
register = template.Library() |
33 |
|
34 |
|
35 |
@register.filter(name="vm_public_ip") |
36 |
def vm_public_ip(vm): |
37 |
"""
|
38 |
Identify if vm is connected to ``public`` network and return the ipv4
|
39 |
address
|
40 |
"""
|
41 |
try:
|
42 |
return vm.nics.filter(network__public=True)[0].ipv4_address |
43 |
except IndexError: |
44 |
return "No public ip" |
45 |
|
46 |
|
47 |
VM_STATE_CSS_MAP = { |
48 |
'BUILD': 'warning', |
49 |
'PENDING': 'warning', |
50 |
'ERROR': 'important', |
51 |
'STOPPED': 'notice', |
52 |
'STARTED': 'success', |
53 |
'ACTIVE': 'success', |
54 |
'DESTROYED': 'inverse' |
55 |
} |
56 |
|
57 |
|
58 |
@register.filter(name="object_status_badge", is_safe=True) |
59 |
def object_status_badge(vm_or_net): |
60 |
"""
|
61 |
Return a span badge styled based on the vm current status
|
62 |
"""
|
63 |
state = vm_or_net.operstate if hasattr(vm_or_net, 'operstate') else \ |
64 |
vm_or_net.state |
65 |
state_cls = VM_STATE_CSS_MAP.get(state, 'notice')
|
66 |
badge_cls = "badge badge-%s" % state_cls
|
67 |
|
68 |
deleted_badge = ""
|
69 |
if vm_or_net.deleted:
|
70 |
deleted_badge = '<span class="badge badge-important">Deleted</span>'
|
71 |
return '%s\n<span class="%s">%s</span>' % (deleted_badge, badge_cls, state) |
72 |
|
73 |
|
74 |
@register.filter(name="network_deleted_badge", is_safe=True) |
75 |
def network_deleted_badge(network): |
76 |
"""
|
77 |
Return a span badge styled based on the vm current status
|
78 |
"""
|
79 |
deleted_badge = ""
|
80 |
if network.deleted:
|
81 |
deleted_badge = '<span class="badge badge-important">Deleted</span>'
|
82 |
return deleted_badge
|
83 |
|
84 |
|
85 |
@register.filter(name="get_os", is_safe=True) |
86 |
def get_os(vm): |
87 |
try:
|
88 |
return vm.metadata.filter(meta_key="OS").get().meta_value |
89 |
except:
|
90 |
return "unknown" |
91 |
|
92 |
|
93 |
@register.filter(name="network_vms", is_safe=True) |
94 |
def network_vms(network, account, show_deleted=False): |
95 |
vms = [] |
96 |
nics = network.nics.filter(machine__userid=account) |
97 |
if not show_deleted: |
98 |
nics = nics.filter(machine__deleted=False).distinct()
|
99 |
for nic in nics: |
100 |
vms.append(nic.machine) |
101 |
return vms
|
102 |
|
103 |
|
104 |
@register.filter(name="network_nics") |
105 |
def network_nics(network, account, show_deleted=False): |
106 |
vms = [] |
107 |
nics = network.nics.filter(machine__userid=account) |
108 |
if not show_deleted: |
109 |
nics = nics.filter(machine__deleted=False).distinct()
|
110 |
return nics
|
111 |
|
112 |
|
113 |
@register.filter(name="backend_info", is_safe=True) |
114 |
def backend_info(vm): |
115 |
content = ""
|
116 |
backend = vm.backend |
117 |
excluded = ['password_hash', 'hash', 'username'] |
118 |
if not vm.backend: |
119 |
content = "No backend"
|
120 |
return content
|
121 |
|
122 |
for field in vm.backend._meta.fields: |
123 |
if field.name in excluded: |
124 |
continue
|
125 |
content += '<dt>Backend ' + field.name + '</dt><dd>' + \ |
126 |
str(getattr(backend, field.name)) + '</dd>' |
127 |
return content
|