Revision 5a0f9d6c snf-astakos-app/astakos/im/management/commands/quota-list.py

b/snf-astakos-app/astakos/im/management/commands/quota-list.py
36 36

  
37 37
from astakos.im.models import AstakosUser
38 38
from astakos.im.quotas import list_user_quotas
39
from snf_django.management.commands import SynnefoCommand
39
from snf_django.management.commands import SynnefoCommand, CommandError
40 40
from snf_django.management import utils
41 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
42 44

  
43 45
import logging
44 46
logger = logging.getLogger(__name__)
......
53 55
                    help=("Specify display unit for resource values "
54 56
                          "(one of %s); defaults to mb") %
55 57
                    common.style_options),
56
        make_option('--user',
57
                    metavar='<uuid or email>',
58
                    dest='user',
59
                    help="List quota for a specified user"),
58
        make_option('--overlimit',
59
                    action='store_true',
60
                    help="Show quota that is over limit"),
61
        make_option('--with-custom',
62
                    metavar='True|False',
63
                    help=("Filter quota different from the default or "
64
                          "equal to it")),
65
        make_option('--filter-by',
66
                    help="Filter by field; "
67
                    "e.g. \"user=uuid,usage>=10M,base_quota<inf\""),
68
        make_option('--displayname',
69
                    action='store_true',
70
                    help="Show user display name"),
60 71
    )
61 72

  
73
    QHFLT = {
74
        "total_quota": ("limit", filtering.parse_with_unit),
75
        "usage": ("usage_max", filtering.parse_with_unit),
76
        "user": ("holder", lambda x: x),
77
        "resource": ("resource", lambda x: x),
78
        "source": ("source", lambda x: x),
79
        }
80

  
81
    INITFLT = {
82
        "base_quota": ("capacity", filtering.parse_with_unit),
83
        }
84

  
62 85
    @transaction.commit_on_success
63 86
    def handle(self, *args, **options):
64 87
        output_format = options["output_format"]
65
        user_ident = options['user']
88
        displayname = bool(options["displayname"])
66 89
        unit_style = options["unit_style"]
67 90
        common.check_style(unit_style)
68 91

  
69
        if user_ident is not None:
70
            users = [common.get_accepted_user(user_ident)]
92
        filteropt = options["filter_by"]
93
        if filteropt is not None:
94
            filters = filteropt.split(",")
71 95
        else:
72
            users = AstakosUser.objects.accepted()
96
            filters = []
97

  
98
        QHQ, INITQ = Q(), Q()
99
        for flt in filters:
100
            q = filtering.make_query(flt, self.QHFLT)
101
            if q is not None:
102
                QHQ &= q
103
            q = filtering.make_query(flt, self.INITFLT)
104
            if q is not None:
105
                INITQ &= q
73 106

  
74
        qh_quotas, astakos_i = list_user_quotas(users)
107
        overlimit = bool(options["overlimit"])
108
        if overlimit:
109
            QHQ &= Q(usage_max__gt=F("limit"))
75 110

  
76
        info = {}
77
        for user in users:
78
            info[user.uuid] = user.email
111
        with_custom = options["with_custom"]
112
        if with_custom is not None:
113
            qeq = Q(capacity=F("resource__uplimit"))
114
            try:
115
                INITQ &= ~qeq if utils.parse_bool(with_custom) else qeq
116
            except ValueError as e:
117
                raise CommandError(e)
118

  
119
        users = AstakosUser.objects.accepted()
120
        qh_quotas, astakos_i = list_user_quotas(
121
            users, qhflt=QHQ, initflt=INITQ)
122

  
123
        if displayname:
124
            info = {}
125
            for user in users:
126
                info[user.uuid] = user.email
127
        else:
128
            info = None
79 129

  
80 130
        print_data, labels = common.show_quotas(
81 131
            qh_quotas, astakos_i, info, style=unit_style)

Also available in: Unified diff