Revision db8cfc97 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 BaseCommand, CommandError
36 36
from django.db import transaction
37 37

  
38
from astakos.im.models import sync_all_users, sync_projects
38
from astakos.im.models import sync_all_users, sync_users, AstakosUser
39 39
from astakos.im.functions import get_user_by_uuid
40
from astakos.im.management.commands._common import is_uuid, is_email
40 41

  
41 42
import logging
42 43
logger = logging.getLogger(__name__)
......
61 62
                    dest='sync',
62 63
                    default=False,
63 64
                    help="Sync quotaholder"),
65
        make_option('--user',
66
                    metavar='<uuid or email>',
67
                    dest='user',
68
                    help="List quotas for a specified user"),
64 69
    )
65 70

  
66 71
    def handle(self, *args, **options):
67 72
        sync = options['sync']
68 73
        verify = options['verify'] or sync
74
        user_ident = options['user']
69 75

  
70
        ex, nonex, qh_l, qh_c, astakos_i, astakos_q, info = self.run(sync)
76
        if user_ident is not None:
77
            if verify:
78
                raise CommandError("Cannot combine `--user' option "
79
                                   "with `--verify' or `--sync'.")
80

  
81
            log = self.run_sync_user(user_ident)
82
        else:
83
            log = self.run(sync)
84

  
85
        ex, nonex, qh_l, qh_c, astakos_i, astakos_q, info = log
71 86

  
72 87
        if verify:
73 88
            self.print_verify(nonex, qh_l, astakos_q)
......
76 91
            self.list_quotas(qh_l, qh_c, astakos_i, info)
77 92

  
78 93
    @transaction.commit_on_success
94
    def run_sync_user(self, user_ident):
95
        if is_uuid(user_ident):
96
            try:
97
                user = AstakosUser.objects.get(uuid=user_ident)
98
            except AstakosUser.DoesNotExist:
99
                raise CommandError('Not found user having uuid: %s' %
100
                                   user_ident)
101
        elif is_email(user_ident):
102
            try:
103
                user = AstakosUser.objects.get(username=user_ident)
104
            except AstakosUser.DoesNotExist:
105
                raise CommandError('Not found user having email: %s' %
106
                                   user_ident)
107
        else:
108
            raise CommandError('Please specify user by uuid or email')
109

  
110
        try:
111
            return sync_users([user], sync=False)
112
        except BaseException, e:
113
            logger.exception(e)
114
            raise CommandError("Failed to compute quotas.")
115

  
116
    @transaction.commit_on_success
79 117
    def run(self, sync):
80 118
        try:
81 119
            return sync_all_users(sync=sync)

Also available in: Unified diff