Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user-list.py @ cb14cc6c

History | View | Annotate | Download (6.3 kB)

1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from optparse import make_option
35

    
36
from django.core.management.base import NoArgsCommand
37

    
38
from astakos.im.models import AstakosUser, AstakosUserAuthProvider, Group
39

    
40
from ._common import format, filter_results
41

    
42

    
43
class Command(NoArgsCommand):
44
    help = "List users"
45

    
46
    FIELDS = AstakosUser._meta.get_all_field_names()
47

    
48
    option_list = NoArgsCommand.option_list + (
49
        make_option('-c',
50
                    action='store_true',
51
                    dest='csv',
52
                    default=False,
53
                    help="Use pipes to separate values"),
54
        make_option('-p',
55
                    action='store_true',
56
                    dest='pending',
57
                    default=False,
58
                    help="List only users pending activation"),
59
        make_option('--auth-providers',
60
                    action='store_true',
61
                    dest='auth_providers',
62
                    default=False,
63
                    help="Display user authentication providers"),
64
        make_option('--group',
65
                    action='append',
66
                    dest='groups',
67
                    default=None,
68
                    help="Only show users that belong to the specified goups"),
69
        make_option('-n',
70
                    action='store_true',
71
                    dest='pending_send_mail',
72
                    default=False,
73
                    help="List only users who have not received activation"),
74
        make_option('--uuid',
75
                    action='store_true',
76
                    dest='only_uuid',
77
                    default=False,
78
                    help="Only display user uuid (default)"),
79
        make_option('--displayname',
80
                    action='store_true',
81
                    dest='displayname',
82
                    default=False,
83
                    help="Display both uuid and display name"),
84
        make_option('--active',
85
                    action='store_true',
86
                    dest='active',
87
                    default=False,
88
                    help="Display only active users"),
89
        make_option('--filter-by',
90
                    dest='filter_by',
91
                    help="Filter results. Comma seperated list of key `cond`"
92
                    " val pairs that displayed entries must satisfy. e.g."
93
                    " --filter-by \"is_active=True,email_verified=True\"."
94
                    " Available keys are: %s" % ", ".join(FIELDS)),
95

    
96
    )
97

    
98
    def handle_noargs(self, **options):
99
        users = AstakosUser.objects.all().order_by('id')
100
        if options['pending']:
101
            users = users.filter(is_active=False)
102
        elif options['pending_send_mail']:
103
            users = users.filter(is_active=False, activation_sent=None)
104

    
105
        active_only = options['active']
106
        if active_only:
107
            users = filter_results(users, "is_active=True")
108

    
109
        filter_by = options['filter_by']
110
        if filter_by:
111
            users = filter_results(users, filter_by)
112

    
113
        displayname = options['displayname']
114

    
115
        ids = [user.id for user in users]
116
        auths = AstakosUserAuthProvider.objects.filter(
117
            user__in=ids, active=True)
118

    
119
        all_auth = partition_by(lambda a: a.user_id, auths)
120

    
121
        labels = filter(lambda x: x is not Omit,
122
                        [('id', 3),
123
                         ('display name', 24) if displayname else Omit,
124
                         ('real name', 24),
125
                         ('active', 6),
126
                         ('admin', 5),
127
                         ('uuid', 36),
128
                         ('providers', 24),
129
                         ])
130

    
131
        columns = [c for (l, c) in labels]
132

    
133
        if not options['csv']:
134
            line = ' '.join(l.rjust(w) for l, w in labels)
135
            self.stdout.write(line + '\n')
136
            sep = '-' * len(line)
137
            self.stdout.write(sep + '\n')
138

    
139
        for user in users:
140
            id = str(user.id)
141
            active = user.is_active
142
            admin = user.is_superuser
143
            uuid = user.uuid or ''
144
            auths = all_auth[user.id]
145
            auth_display = ",".join(unicode(auth) for auth in auths)
146

    
147
            elems = filter(lambda x: x is not Omit,
148
                           [id,
149
                            user.username if displayname else Omit,
150
                            user.realname,
151
                            active, admin, uuid,
152
                            auth_display,
153
                            ])
154
            fields = (format(elem) for elem in elems)
155

    
156
            if options['csv']:
157
                line = '|'.join(fields)
158
            else:
159
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
160

    
161
            self.stdout.write(line + '\n')
162

    
163

    
164
class Omit(object):
165
    pass
166

    
167

    
168
def partition_by(f, l):
169
    d = {}
170
    for x in l:
171
        group = f(x)
172
        group_l = d.get(group, [])
173
        group_l.append(x)
174
        d[group] = group_l
175
    return d