Revision 764d99c4 snf-astakos-app/astakos/im/management/commands/astakos-quota.py
b/snf-astakos-app/astakos/im/management/commands/astakos-quota.py | ||
---|---|---|
35 | 35 |
from django.core.management.base import CommandError |
36 | 36 |
|
37 | 37 |
from astakos.im.models import AstakosUser |
38 |
from astakos.im.quotas import set_user_quota, list_user_quotas |
|
38 |
from astakos.im.quotas import set_user_quota, list_user_quotas, add_base_quota
|
|
39 | 39 |
from astakos.im.functions import get_user_by_uuid |
40 | 40 |
from astakos.im.management.commands._common import is_uuid, is_email |
41 | 41 |
from snf_django.lib.db.transaction import commit_on_success_strict |
... | ... | |
70 | 70 |
metavar='<uuid or email>', |
71 | 71 |
dest='user', |
72 | 72 |
help="List quotas for a specified user"), |
73 |
make_option('--import-base-quota', |
|
74 |
dest='import_base_quota', |
|
75 |
metavar='<exported-quotas.txt>', |
|
76 |
help=("Import base quotas from file. " |
|
77 |
"The file must contain non-empty lines, and each " |
|
78 |
"line must contain a single-space-separated list " |
|
79 |
"of values: <user> <resource name> <capacity>") |
|
80 |
), |
|
73 | 81 |
) |
74 | 82 |
|
75 | 83 |
@commit_on_success_strict() |
... | ... | |
77 | 85 |
sync = options['sync'] |
78 | 86 |
verify = options['verify'] |
79 | 87 |
user_ident = options['user'] |
88 |
list_ = options['list'] |
|
89 |
import_base_quota = options['import_base_quota'] |
|
90 |
|
|
91 |
if import_base_quota: |
|
92 |
if any([sync, verify, list_]): |
|
93 |
m = "--from-file cannot be combined with other options." |
|
94 |
raise CommandError(m) |
|
95 |
self.import_from_file(import_base_quota) |
|
96 |
else: |
|
97 |
self.quotas(sync, verify, user_ident, options["output_format"]) |
|
98 |
|
|
99 |
def quotas(self, sync, verify, user_ident, output_format): |
|
80 | 100 |
list_only = not sync and not verify |
81 | 101 |
|
82 | 102 |
if user_ident is not None: |
... | ... | |
97 | 117 |
if list_only: |
98 | 118 |
print_data, labels = show_quotas(qh_quotas, astakos_i, info) |
99 | 119 |
utils.pprint_table(self.stdout, print_data, labels, |
100 |
options["output_format"])
|
|
120 |
output_format)
|
|
101 | 121 |
|
102 | 122 |
else: |
103 | 123 |
if verify: |
... | ... | |
163 | 183 |
diffs = len(diff_quotas) |
164 | 184 |
if diffs: |
165 | 185 |
self.stdout.write("Quotas differ for %d users.\n" % (diffs)) |
186 |
|
|
187 |
def import_from_file(self, location): |
|
188 |
users = set() |
|
189 |
with open(location) as f: |
|
190 |
for line in f.readlines(): |
|
191 |
try: |
|
192 |
t = line.rstrip('\n').split(' ') |
|
193 |
user = t[0] |
|
194 |
resource = t[1] |
|
195 |
capacity = t[2] |
|
196 |
except(IndexError, TypeError): |
|
197 |
self.stdout.write('Invalid line format: %s:\n' % t) |
|
198 |
continue |
|
199 |
else: |
|
200 |
try: |
|
201 |
user = self.get_user(user) |
|
202 |
users.add(user.id) |
|
203 |
except CommandError: |
|
204 |
self.stdout.write('Not found user: %s\n' % user) |
|
205 |
continue |
|
206 |
else: |
|
207 |
try: |
|
208 |
add_base_quota(user, resource, capacity) |
|
209 |
except Exception, e: |
|
210 |
self.stdout.write('Failed to add quota: %s\n' % e) |
|
211 |
continue |
Also available in: Unified diff