root / snf-astakos-app / astakos / im / management / commands / quota-list.py @ def409e0
History | View | Annotate | Download (4.1 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
from django.db import transaction |
36 |
|
37 |
from astakos.im.models import AstakosUser |
38 |
from astakos.im.quotas import list_user_quotas |
39 |
from snf_django.management.commands import SynnefoCommand, CommandError |
40 |
from snf_django.management import utils |
41 |
from astakos.im.management.commands import _common as common |
42 |
from astakos.im.management.commands import _filtering as filtering |
43 |
from django.db.models import Q, F |
44 |
|
45 |
import logging |
46 |
logger = logging.getLogger(__name__) |
47 |
|
48 |
|
49 |
class Command(SynnefoCommand): |
50 |
help = "List user quota"
|
51 |
|
52 |
option_list = SynnefoCommand.option_list + ( |
53 |
make_option('--unit-style',
|
54 |
default='mb',
|
55 |
help=("Specify display unit for resource values "
|
56 |
"(one of %s); defaults to mb") %
|
57 |
common.style_options), |
58 |
make_option('--overlimit',
|
59 |
action='store_true',
|
60 |
help="Show quota that is over limit"),
|
61 |
make_option('--filter-by',
|
62 |
help="Filter by field; "
|
63 |
"e.g. \"user=uuid,usage>=10M,base_quota<inf\""),
|
64 |
make_option('--displayname',
|
65 |
action='store_true',
|
66 |
help="Show user display name"),
|
67 |
) |
68 |
|
69 |
QHFLT = { |
70 |
"limit": ("limit", filtering.parse_with_unit), |
71 |
"usage": ("usage_max", filtering.parse_with_unit), |
72 |
"user": ("holder", lambda x: x), |
73 |
"resource": ("resource", lambda x: x), |
74 |
"source": ("source", lambda x: x), |
75 |
} |
76 |
|
77 |
@transaction.commit_on_success
|
78 |
def handle(self, *args, **options): |
79 |
output_format = options["output_format"]
|
80 |
displayname = bool(options["displayname"]) |
81 |
unit_style = options["unit_style"]
|
82 |
common.check_style(unit_style) |
83 |
|
84 |
filteropt = options["filter_by"]
|
85 |
if filteropt is not None: |
86 |
filters = filteropt.split(",")
|
87 |
else:
|
88 |
filters = [] |
89 |
|
90 |
QHQ = Q() |
91 |
for flt in filters: |
92 |
q = filtering.make_query(flt, self.QHFLT)
|
93 |
if q is not None: |
94 |
QHQ &= q |
95 |
|
96 |
overlimit = bool(options["overlimit"]) |
97 |
if overlimit:
|
98 |
QHQ &= Q(usage_max__gt=F("limit"))
|
99 |
|
100 |
users = AstakosUser.objects.accepted() |
101 |
qh_quotas = list_user_quotas( |
102 |
users, qhflt=QHQ) |
103 |
|
104 |
if displayname:
|
105 |
info = {} |
106 |
for user in users: |
107 |
info[user.uuid] = user.email |
108 |
else:
|
109 |
info = None
|
110 |
|
111 |
print_data, labels = common.show_quotas( |
112 |
qh_quotas, info, style=unit_style) |
113 |
utils.pprint_table(self.stdout, print_data, labels, output_format)
|