utils.py 2.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import logging

19
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
20

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
21
from astakos.im.models import AstakosUser
22
from synnefo.db.models import IPAddress, IPAddressLog
23

24
from synnefo_admin.admin.exceptions import AdminHttp404
25
from synnefo_admin.admin.utils import create_details_href
26
27


28
29
30
31
32
def get_ip_or_404(query):
    try:
        return IPAddress.objects.get(address=query)
    except ObjectDoesNotExist:
        pass
33
34
35
    except MultipleObjectsReturned:
        raise AdminHttp404("""Hm, that's interesting. There are more than one
                           entries for this address: %s""" % query)
36
37
38
39
40
41

    try:
        return IPAddress.objects.get(pk=int(query))
    except (ObjectDoesNotExist, ValueError):
        # Check the IPAddressLog and inform the user that the IP existed at
        # sometime.
42
        msg = "No IP was found that matches this query: %s" % query
43
44
        try:
            if IPAddressLog.objects.filter(address=query).exists():
45
46
                msg = """This IP was deleted. Check the "IP History" tab for
                more details."""
47
48
        except ObjectDoesNotExist:
            pass
49
        raise AdminHttp404(msg)
50
51


52
def get_contact_email(inst):
53
    if inst.userid:
54
        return AstakosUser.objects.get(uuid=inst.userid).email
55
56
57
58


def get_contact_name(inst):
    if inst.userid:
59
        return AstakosUser.objects.get(uuid=inst.userid).realname
60
61


62
63
64
def get_user_details_href(ip):
    if ip.userid:
        user = AstakosUser.objects.get(uuid=ip.userid)
65
        return create_details_href('user', user.realname, user.email)
66
67
    else:
        return "-"
68
69


70
71
72
73
74
75
76
77
def get_vm_details_href(ip):
    if ip.in_use():
        vm = ip.nic.machine
        return create_details_href('vm', vm.name, vm.pk)
    else:
        return "-"


78
79
80
81
82
83
def get_network_details_href(ip):
    if ip.in_use():
        network = ip.nic.network
        return create_details_href('network', network.name, network.pk)
    else:
        return "-"