Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / helpdesk / templatetags / helpdesk_tags.py @ 88329243

History | View | Annotate | Download (2.7 kB)

1
from django import template
2

    
3
register = template.Library()
4

    
5

    
6
@register.filter(name="vm_public_ip")
7
def vm_public_ip(vm):
8
    """
9
    Identify if vm is connected to ``public`` network and return the ipv4
10
    address
11
    """
12
    try:
13
        return vm.nics.filter(network__public=True)[0].ipv4_address
14
    except IndexError:
15
        return "No public ip"
16

    
17

    
18
VM_STATE_CSS_MAP = {
19
        'BUILD': 'warning',
20
        'PENDING': 'warning',
21
        'ERROR': 'important',
22
        'STOPPED': 'notice',
23
        'STARTED': 'success',
24
        'ACTIVE': 'success',
25
        'DESTROYED': 'inverse'
26
}
27

    
28

    
29
@register.filter(name="object_status_badge", is_safe=True)
30
def object_status_badge(vm_or_net):
31
    """
32
    Return a span badge styled based on the vm current status
33
    """
34
    state = vm_or_net.operstate if hasattr(vm_or_net, 'operstate') else \
35
        vm_or_net.state
36
    state_cls = VM_STATE_CSS_MAP.get(state, 'notice')
37
    badge_cls = "badge badge-%s" % state_cls
38

    
39
    deleted_badge = ""
40
    if vm_or_net.deleted:
41
        deleted_badge = '<span class="badge badge-important">Deleted</span>'
42
    return '%s\n<span class="%s">%s</span>' % (deleted_badge, badge_cls, state)
43

    
44

    
45
@register.filter(name="network_deleted_badge", is_safe=True)
46
def network_deleted_badge(network):
47
    """
48
    Return a span badge styled based on the vm current status
49
    """
50
    deleted_badge = ""
51
    if network.deleted:
52
        deleted_badge = '<span class="badge badge-important">Deleted</span>'
53
    return deleted_badge
54

    
55

    
56
@register.filter(name="get_os", is_safe=True)
57
def get_os(vm):
58
    try:
59
        return vm.metadata.filter(meta_key="OS").get().meta_value
60
    except:
61
        return "unknown"
62

    
63

    
64
@register.filter(name="network_vms", is_safe=True)
65
def network_vms(network, account, show_deleted=False):
66
    vms = []
67
    nics = network.nics.filter(machine__userid=account)
68
    if not show_deleted:
69
        nics = nics.filter(machine__deleted=False).distinct()
70
    for nic in nics:
71
        vms.append(nic.machine)
72
    return vms
73

    
74

    
75
@register.filter(name="network_nics")
76
def network_nics(network, account, show_deleted=False):
77
    vms = []
78
    nics = network.nics.filter(machine__userid=account)
79
    if not show_deleted:
80
        nics = nics.filter(machine__deleted=False).distinct()
81
    return nics
82

    
83

    
84
@register.filter(name="backend_info", is_safe=True)
85
def backend_info(vm):
86
    content = ""
87
    backend = vm.backend
88
    excluded = ['password_hash', 'hash', 'username']
89
    if not vm.backend:
90
        content = "No backend"
91
        return content
92

    
93
    for field in vm.backend._meta.fields:
94
        if field.name in excluded:
95
            continue
96
        content += '<dt>Backend ' + field.name + '</dt><dd>' + \
97
                   str(getattr(backend, field.name)) + '</dd>'
98
    return content