Revision 398a9604 snf-astakos-app/astakos/im/management/commands/user-show.py

b/snf-astakos-app/astakos/im/management/commands/user-show.py
34 34
from django.core.management.base import CommandError
35 35
from optparse import make_option
36 36

  
37
from astakos.im.models import AstakosUser, get_latest_terms
37
from astakos.im.models import AstakosUser, get_latest_terms, Chain
38 38
from astakos.im.quotas import list_user_quotas
39 39

  
40 40
from synnefo.lib.ordereddict import OrderedDict
......
56 56
                    dest='list_quotas',
57 57
                    default=False,
58 58
                    help="Also list user quota"),
59
        make_option('--projects',
60
                    action='store_true',
61
                    dest='list_projects',
62
                    default=False,
63
                    help="Also list project memberships"),
59 64
    )
60 65

  
61 66
    def handle(self, *args, **options):
......
122 127
                print_data, labels = show_quotas(quotas, initial)
123 128
                utils.pprint_table(self.stdout, print_data, labels,
124 129
                                   options["output_format"])
130

  
131
            if options["list_projects"]:
132
                print_data, labels = ownerships(user)
133
                if print_data:
134
                    self.stdout.write("\n")
135
                    utils.pprint_table(self.stdout, print_data, labels,
136
                                       options["output_format"],
137
                                       title="Owned Projects")
138

  
139
                print_data, labels = memberships(user)
140
                if print_data:
141
                    self.stdout.write("\n")
142
                    utils.pprint_table(self.stdout, print_data, labels,
143
                                       options["output_format"],
144
                                       title="Project Memberships")
145

  
146

  
147
def memberships(user):
148
    ms = user.projectmembership_set.all()
149
    print_data = []
150
    labels = ('project id', 'project name', 'status')
151

  
152
    for m in ms:
153
        project = m.project
154
        print_data.append((project.id,
155
                           project.application.name,
156
                           m.state_display(),
157
                           ))
158
    return print_data, labels
159

  
160

  
161
def ownerships(user):
162
    chain_dict = Chain.objects.all_full_state()
163
    chain_dict = filter_by(is_owner(user), chain_dict)
164
    return chain_info(chain_dict)
165

  
166

  
167
def is_owner(user):
168
    def f(state, project, app):
169
        return user == app.owner
170
    return f
171

  
172

  
173
def filter_by(f, chain_dict):
174
    d = {}
175
    for chain, tpl in chain_dict.iteritems():
176
        if f(*tpl):
177
            d[chain] = tpl
178
    return d
179

  
180

  
181
def chain_info(chain_dict):
182
    labels = ('project id', 'project name', 'status', 'pending app id')
183
    l = []
184
    for chain, (state, project, app) in chain_dict.iteritems():
185
        status = Chain.state_display(state)
186
        if state in Chain.PENDING_STATES:
187
            appid = str(app.id)
188
        else:
189
            appid = ""
190

  
191
        t = (chain,
192
             project.application.name if project else app.name,
193
             status,
194
             appid,
195
             )
196
        l.append(t)
197
    return l, labels

Also available in: Unified diff