utils.py 4.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

from django.core.exceptions import ObjectDoesNotExist

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
21
from astakos.im.models import Project
22
23
24
from astakos.im.quotas import get_project_quota

from synnefo.util import units
25
from synnefo_admin.admin.exceptions import AdminHttp404
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
26
from synnefo_admin.admin.utils import is_resource_useful
27
28


29
30
31
32
33
34
35
36
37
def get_actual_owner(inst):
    if inst.owner:
        return inst.owner
    try:
        return inst.members.all()[0]
    except IndexError:
        return None


38
39
def get_project_or_404(query):
    # Get by UUID
40
    try:
41
42
43
44
45
46
47
48
49
50
        return Project.objects.get(uuid=query)
    except ObjectDoesNotExist:
        pass

    # Get by ID
    try:
        return Project.objects.get(id=query)
    except (ObjectDoesNotExist, ValueError):
        raise AdminHttp404(
            "No Project was found that matches this query: %s\n" % query)
51
52


53
def get_contact_email(inst):
54
55
56
    owner = get_actual_owner(inst)
    if owner:
        return owner.email
57
58
59


def get_contact_name(inst):
60
61
62
    owner = get_actual_owner(inst)
    if owner:
        return owner.realname
63
64
65


def get_contact_id(inst):
66
67
68
    owner = get_actual_owner(inst)
    if owner:
        return owner.uuid
69
70


Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
71
def get_policies(inst):
72
    policies = inst.projectresourcequota_set.all().prefetch_related('resource')
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
73
74
75
76
77
78
79
80
81
82
83
84
85
    policy_list = []

    for p in policies:
        r = p.resource
        if not is_resource_useful(r, p.project_capacity):
            continue
        policy_list.append(p)

    return policy_list


def get_project_usage(inst):
    """Return requested project quota type.
86
87
88

    Accepted stats are: 'project_limit', 'project_pending', 'project_usage'.
    Note that the output is sanitized, meaning that stats that correspond
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
89
    to infinite or zero limits will not be returned.
90
91
92
    """
    resource_list = []
    quota_dict = get_project_quota(inst)
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
93
94
    if not quota_dict:
        return []
95

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
96
97
98
99
100
101
102
103
104
105
106
107
108
    policies = get_policies(inst)
    for p in policies:
        r = p.resource
        value = units.show(quota_dict[r.name]['project_usage'], r.unit)
        resource_list.append((r.report_desc, value))

    return resource_list


def get_project_quota_category(inst, category):
    """Get the quota for project member"""
    resource_list = []
    policies = get_policies(inst)
109

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
110
111
112
    for p in policies:
        r = p.resource
        # Get human-readable (resource name, member capacity) tuple
113
        if category == "member":
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
114
            resource_list.append((r.report_desc, p.display_member_capacity()))
115
        elif category == "limit":
Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
116
117
118
119
120
121
122
            resource_list.append((r.report_desc, p.display_project_capacity()))

    return resource_list


def display_quota_horizontally(resource_list):
    """Display resource lists in one line."""
123
124
125
126
127
    if not resource_list:
        return "-"
    return ', '.join((': '.join(pair) for pair in resource_list))


Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
128
129
130
131
def display_project_usage_horizontally(inst):
    """Display the requested project stats in a one-line string."""
    resource_list = get_project_usage(inst)
    return display_quota_horizontally(resource_list)
132
133


Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
134
135
136
137
def display_member_quota_horizontally(inst):
    """Display project resources (member or total) in one line."""
    resource_list = get_project_quota_category(inst, "member")
    return display_quota_horizontally(resource_list)
138

Alex Pyrgiotis's avatar
Alex Pyrgiotis committed
139
140
141
142
143

def display_project_limit_horizontally(inst):
    """Display project resources (member or total) in one line."""
    resource_list = get_project_quota_category(inst, "limit")
    return display_quota_horizontally(resource_list)