Commit ff846349 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

List quotas for a specified user

Refs #3389
parent 5631d997
......@@ -32,7 +32,9 @@
# or implied, of GRNET S.A.
from datetime import datetime
import uuid
from django.core.validators import validate_email
from django.utils.timesince import timesince, timeuntil
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
......@@ -210,3 +212,25 @@ def filter_results(objects, filter_by):
raise CommandError(e)
except Exception as e:
raise CommandError("Can not filter results: %s" % e)
def is_uuid(s):
if s is None:
return False
try:
uuid.UUID(s)
except ValueError:
return False
else:
return True
def is_email(s):
if s is None:
return False
try:
validate_email(s)
except:
return False
else:
return True
......@@ -35,8 +35,9 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from astakos.im.models import sync_all_users, sync_projects
from astakos.im.models import sync_all_users, sync_users, AstakosUser
from astakos.im.functions import get_user_by_uuid
from astakos.im.management.commands._common import is_uuid, is_email
import logging
logger = logging.getLogger(__name__)
......@@ -61,13 +62,27 @@ class Command(BaseCommand):
dest='sync',
default=False,
help="Sync quotaholder"),
make_option('--user',
metavar='<uuid or email>',
dest='user',
help="List quotas for a specified user"),
)
def handle(self, *args, **options):
sync = options['sync']
verify = options['verify'] or sync
user_ident = options['user']
ex, nonex, qh_l, qh_c, astakos_i, astakos_q, info = self.run(sync)
if user_ident is not None:
if verify:
raise CommandError("Cannot combine `--user' option "
"with `--verify' or `--sync'.")
log = self.run_sync_user(user_ident)
else:
log = self.run(sync)
ex, nonex, qh_l, qh_c, astakos_i, astakos_q, info = log
if verify:
self.print_verify(nonex, qh_l, astakos_q)
......@@ -75,6 +90,29 @@ class Command(BaseCommand):
else:
self.list_quotas(qh_l, qh_c, astakos_i, info)
@transaction.commit_on_success
def run_sync_user(self, user_ident):
if is_uuid(user_ident):
try:
user = AstakosUser.objects.get(uuid=user_ident)
except AstakosUser.DoesNotExist:
raise CommandError('Not found user having uuid: %s' %
user_ident)
elif is_email(user_ident):
try:
user = AstakosUser.objects.get(username=user_ident)
except AstakosUser.DoesNotExist:
raise CommandError('Not found user having email: %s' %
user_ident)
else:
raise CommandError('Please specify user by uuid or email')
try:
return sync_users([user], sync=False)
except BaseException, e:
logger.exception(e)
raise CommandError("Failed to compute quotas.")
@transaction.commit_on_success
def run(self, sync):
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment