Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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
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('-n',
60
                    action='store_true',
61
                    dest='pending_send_mail',
62
                    default=False,
63
                    help="List only users who have not received activation"),
64
        make_option('--uuid',
65
                    action='store_true',
66
                    dest='only_uuid',
67
                    default=False,
68
                    help="Only display user uuid (default)"),
69
        make_option('--displayname',
70
                    action='store_true',
71
                    dest='displayname',
72
                    default=False,
73
                    help="Display both uuid and display name"),
74
        make_option('--active',
75
                    action='store_true',
76
                    dest='active',
77
                    default=False,
78
                    help="Display only active users"),
79
        make_option('--filter-by',
80
                    dest='filter_by',
81
                    help="Filter results. Comma seperated list of key `cond`"
82
                    " val pairs that displayed entries must satisfy. e.g."
83
                    " --filter-by \"is_active=True,email_verified=True\"."
84
                    " Available keys are: %s" % ", ".join(FIELDS)),
85

    
86
    )
87

    
88
    def handle_noargs(self, **options):
89
        users = AstakosUser.objects.all().order_by('id')
90
        if options['pending']:
91
            users = users.filter(is_active=False)
92
        elif options['pending_send_mail']:
93
            users = users.filter(is_active=False, activation_sent=None)
94

    
95
        active_only = options['active']
96
        if active_only:
97
            users = filter_results(users, "is_active=True")
98

    
99
        filter_by = options['filter_by']
100
        if filter_by:
101
            users = filter_results(users, filter_by)
102

    
103
        displayname = options['displayname']
104

    
105
        ids = [user.id for user in users]
106
        auths = AstakosUserAuthProvider.objects.filter(
107
            user__in=ids, active=True)
108

    
109
        all_auth = partition_by(lambda a: a.user_id, auths)
110

    
111
        labels = filter(lambda x: x is not Omit,
112
                        [('id', 3),
113
                         ('display name', 24) if displayname else Omit,
114
                         ('real name', 24),
115
                         ('active', 6),
116
                         ('admin', 5),
117
                         ('uuid', 36),
118
                         ('providers', 24),
119
                         ])
120

    
121
        columns = [c for (l, c) in labels]
122

    
123
        if not options['csv']:
124
            line = ' '.join(l.rjust(w) for l, w in labels)
125
            self.stdout.write(line + '\n')
126
            sep = '-' * len(line)
127
            self.stdout.write(sep + '\n')
128

    
129
        for user in users:
130
            id = str(user.id)
131
            active = user.is_active
132
            admin = user.is_superuser
133
            uuid = user.uuid or ''
134
            auths = all_auth[user.id]
135
            auth_display = ",".join(unicode(auth) for auth in auths)
136

    
137
            elems = filter(lambda x: x is not Omit,
138
                           [id,
139
                            user.username if displayname else Omit,
140
                            user.realname,
141
                            active, admin, uuid,
142
                            auth_display,
143
                            ])
144
            fields = (format(elem) for elem in elems)
145

    
146
            if options['csv']:
147
                line = '|'.join(fields)
148
            else:
149
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
150

    
151
            self.stdout.write(line + '\n')
152

    
153

    
154
class Omit(object):
155
    pass
156

    
157

    
158
def partition_by(f, l):
159
    d = {}
160
    for x in l:
161
        group = f(x)
162
        group_l = d.get(group, [])
163
        group_l.append(x)
164
        d[group] = group_l
165
    return d