Revision ffb5cca1

b/snf-astakos-app/astakos/im/management/commands/project-list.py
33 33

  
34 34
from optparse import make_option
35 35

  
36
from django.core.management.base import NoArgsCommand
36
from astakos.im.models import Chain, Project
37
from synnefo.webproject.management.commands import ListCommand
37 38

  
38
from astakos.im.models import Chain
39
from ._common import format, shortened
40 39

  
40
def get_name(chain):
41
    try:
42
        p = Project.objects.get(pk=chain.pk)
43
    except Project.DoesNotExist:
44
        app = chain.last_application()
45
        return app.name
46
    else:
47
        return p.name
41 48

  
42
class Command(NoArgsCommand):
49

  
50
def get_owner_name(chain):
51
    return chain.last_application().owner.realname
52

  
53

  
54
def get_owner_email(chain):
55
    return chain.last_application().owner.email
56

  
57

  
58
def get_state(chain):
59
    try:
60
        p = Project.objects.get(pk=chain.pk)
61
    except Project.DoesNotExist:
62
        p = None
63
    app = chain.last_application()
64
    return chain.get_state(p, app)[0]
65

  
66

  
67
def get_state_display(chain):
68
    return Chain.state_display(get_state(chain))
69

  
70

  
71
def get_appid(chain):
72
    try:
73
        p = Project.objects.get(pk=chain.pk)
74
    except Project.DoesNotExist:
75
        p = None
76
    app = chain.last_application()
77
    state = chain.get_state(p, app)[0]
78
    if state in Chain.PENDING_STATES:
79
        return str(app.id)
80
    else:
81
        return ""
82

  
83

  
84
class Command(ListCommand):
43 85
    help = """
44 86
    List projects and project status.
45 87

  
......
69 111
                           by a new project
70 112
"""
71 113

  
72
    option_list = NoArgsCommand.option_list + (
114
    object_class = Chain
115

  
116
    FIELDS = {
117
        'ProjID': ('pk', 'The id of the project'),
118
        'Name': (get_name, 'The name of the project'),
119
        'Owner': (get_owner_name, 'The name of the project owner'),
120
        'Email': (get_owner_email, 'The email of the project owner'),
121
        'Status': (get_state_display, 'The status of the project'),
122
        'AppID': (get_appid, 'The project application identification'),
123
    }
124

  
125
    fields = ['ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID']
126

  
127
    option_list = ListCommand.option_list + (
73 128
        make_option('--all',
74 129
                    action='store_true',
75 130
                    dest='all',
......
101 156
                    dest='full',
102 157
                    default=False,
103 158
                    help="Do not shorten long names"),
104
        make_option('-c',
105
                    action='store_true',
106
                    dest='csv',
107
                    default=False,
108
                    help="Use pipes to separate values"),
109 159
    )
110 160

  
111
    def handle_noargs(self, **options):
112
        allow_shorten = not options['full']
113
        csv = options['csv']
114

  
115
        chain_dict = Chain.objects.all_full_state()
116

  
117
        if not options['all']:
118
            f_states = []
119
            if options['new']:
120
                f_states.append(Chain.PENDING)
121
            if options['modified']:
122
                f_states += Chain.MODIFICATION_STATES
123
            if options['pending']:
124
                f_states.append(Chain.PENDING)
125
                f_states += Chain.MODIFICATION_STATES
126
            if options['skip']:
127
                if not f_states:
161
    def handle_db_objects(self, objects, **options):
162
        if options['all']:
163
            return
164

  
165
        f_states = []
166
        if options['new']:
167
            f_states.append(Chain.PENDING)
168
        if options['modified']:
169
            f_states += Chain.MODIFICATION_STATES
170
        if options['pending']:
171
            f_states.append(Chain.PENDING)
172
            f_states += Chain.MODIFICATION_STATES
173
        if options['skip']:
174
            if not f_states:
128 175
                    f_states = Chain.RELEVANT_STATES
129 176

  
130
            if f_states:
131
                chain_dict = filter_by_state(chain_dict, f_states)
132

  
133
        self.show(csv, allow_shorten, chain_dict)
134

  
135
    def show(self, csv, allow_shorten, chain_dict):
136
        labels = ('ProjID', 'Name', 'Owner', 'Email', 'Status', 'AppID')
137
        columns = (7, 23, 20, 20, 17, 7)
138

  
139
        if not csv:
140
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
141
            self.stdout.write(line + '\n')
142
            sep = '-' * len(line)
143
            self.stdout.write(sep + '\n')
144

  
145
        for info in chain_info(chain_dict):
146

  
147
            fields = [
148
                (info['projectid'], False),
149
                (info['name'], True),
150
                (info['owner'], True),
151
                (info['email'], True),
152
                (info['status'], False),
153
                (info['appid'], False),
154
            ]
155

  
156
            fields = [(format(elem), flag) for (elem, flag) in fields]
157

  
158
            if csv:
159
                line = '|'.join(fields)
160
            else:
161
                output = []
162
                for (field, shorten), width in zip(fields, columns):
163
                    s = (shortened(field, width) if shorten and allow_shorten
164
                         else field)
165
                    s = s.rjust(width)
166
                    output.append(s)
167

  
168
                line = ' '.join(output)
169

  
170
            self.stdout.write(line + '\n')
171

  
172

  
173
def filter_by_state(chain_dict, states):
174
    d = {}
175
    for chain, (state, project, app) in chain_dict.iteritems():
176
        if state in states:
177
            d[chain] = (state, project, app)
178
    return d
179

  
180

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

  
190
        d = {
191
            'projectid': str(chain),
192
            'name': project.application.name if project else app.name,
193
            'owner': app.owner.realname,
194
            'email': app.owner.email,
195
            'status': status,
196
            'appid': appid,
197
        }
198
        l.append(d)
199
    return l
177
        if f_states:
178
            map(objects.remove,
179
                filter(lambda o: get_state(o) not in f_states, objects))
b/snf-astakos-app/astakos/im/management/commands/user-auth-policy-list.py
35 35
from synnefo.webproject.management.commands import ListCommand
36 36

  
37 37

  
38
def get_groups(profile):
39
    return ','.join(profile.groups.values_list('name', flat=True))
40

  
41

  
42
def get_users(profile):
43
    return ','.join(profile.users.values_list('email', flat=True))
44

  
45

  
38 46
class Command(ListCommand):
39 47
    help = "List existing authentication provider policy profiles"
40 48

  
41 49
    object_class = AuthProviderPolicyProfile
42 50

  
43
    def get_groups(profile):
44
        return ','.join(profile.groups.values_list('name', flat=True))
45

  
46
    def get_users(profile):
47
        return ','.join(profile.users.values_list('email', flat=True))
48

  
49 51
    FIELDS = {
50 52
        'id': ('pk', 'The id of the profile'),
51 53
        'name': ('name', 'The name of the profile'),
52 54
        'provider': ('provider', 'The provider of the profile'),
53
        'exclusive': ('is_exclusive', 'Whether the profile is exclusive or not'),
55
        'exclusive': ('is_exclusive', 'Whether the profile is exclusive'),
54 56
        'groups': (get_groups, 'The groups of the profile'),
55 57
        'users': (get_users, 'The users of the profile'),
56 58
    }
b/snf-astakos-app/astakos/im/management/commands/user-group-list.py
35 35
from synnefo.webproject.management.commands import ListCommand
36 36

  
37 37

  
38
def users_count(group):
39
    return group.user_set.count()
40

  
41

  
38 42
class Command(ListCommand):
39 43
    help = "List available groups"
40 44

  
41 45
    object_class = Group
42 46

  
43
    def users_count(group):
44
        return group.user_set.count()
45

  
46 47
    FIELDS = {
47 48
        'id': ('id', 'The id of the group'),
48 49
        'name': ('name', 'The name of the group'),
b/snf-astakos-app/astakos/im/management/commands/user-list.py
37 37
from synnefo.webproject.management.commands import ListCommand
38 38

  
39 39

  
40
def get_providers(user):
41
    return ','.join(
42
        [unicode(auth) for auth in user.auth_providers.filter(active=True)]
43
    )
44

  
45

  
46
def get_groups(user):
47
    return ','.join(user.groups.all().values_list('name', flat=True))
48

  
49

  
40 50
class Command(ListCommand):
41 51
    help = "List users"
42 52

  
43 53
    object_class = AstakosUser
44 54

  
45
    def get_providers(user):
46
        return ','.join(
47
            [unicode(auth) for auth in user.auth_providers.filter(active=True)]
48
        )
49

  
50
    def get_groups(user):
51
        return ','.join(user.groups.all().values_list('name', flat=True))
52

  
53 55
    FIELDS = {
54 56
        'id': ('id', ('The id of the user')),
55 57
        'real name': ('realname', 'The name of the user'),
b/snf-webproject/synnefo/webproject/management/commands/__init__.py
257 257
                obj.user_email = ucache.get_name(uuid)
258 258

  
259 259
        # Special handling of DB results
260
        self.handle_db_objects(objects)
260
        objects = list(objects)
261
        self.handle_db_objects(objects, **options)
261 262

  
262 263
        headers = self.fields
263 264
        columns = [self.FIELDS[key][0] for key in headers]
......
288 289
    def handle_args(self, *args, **kwargs):
289 290
        pass
290 291

  
291
    def handle_db_objects(self, objects):
292
    def handle_db_objects(self, objects, **options):
292 293
        pass
293 294

  
294 295
    def handle_output(self, table, headers):

Also available in: Unified diff