Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (1.9 kB)

1
from django import template
2

    
3
register = template.Library()
4

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

    
16

    
17
VM_STATE_CSS_MAP = {
18
        'BUILD': 'warning',
19
        'ERROR': 'important',
20
        'STOPPED': 'notice',
21
        'STARTED': 'success',
22
        'DESTROYED': 'inverse'
23
}
24
@register.filter(name="vm_status_badge")
25
def vm_status_badge(vm):
26
    """
27
    Return a span badge styled based on the vm current status
28
    """
29
    state_cls = VM_STATE_CSS_MAP[vm.operstate]
30
    badge_cls = "badge badge-%s" % state_cls
31

    
32
    deleted_badge = ""
33
    if vm.deleted:
34
        deleted_badge = '<span class="badge badge-important">Deleted</span>'
35
    return '%s\n<span class="%s">%s</span>' % (deleted_badge, badge_cls,
36
            vm.operstate)
37

    
38
vm_status_badge.is_safe = True
39

    
40
@register.filter(name="network_deleted_badge")
41
def network_deleted_badge(network):
42
    """
43
    Return a span badge styled based on the vm current status
44
    """
45
    deleted_badge = ""
46
    if network.deleted:
47
        deleted_badge = '<span class="badge badge-important">Deleted</span>'
48
    return deleted_badge
49

    
50
network_deleted_badge.is_safe = True
51

    
52
@register.filter(name="get_os")
53
def get_os(vm):
54
    try:
55
        return vm.metadata.filter(meta_key="OS").get().meta_value
56
    except:
57
        return "unknown"
58

    
59
get_os.is_safe = True
60

    
61
@register.filter(name="network_vms")
62
def network_vms(network, account):
63
    vms = []
64
    for nic in network.nics.filter(machine__userid=account):
65
        vms.append(nic.machine)
66
    return vms
67

    
68
network_vms.is_safe = True
69

    
70
@register.filter(name="network_nics")
71
def network_nics(network, account):
72
    vms = []
73
    for nic in network.nics.filter(machine__userid=account):
74
        vms.append(nic)
75
    return vms
76

    
77
network_nics.is_safe = True