Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.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 astakos.im.models import AstakosUser
37
from synnefo.webproject.management.commands import ListCommand
38

    
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

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

    
53
    object_class = AstakosUser
54

    
55
    FIELDS = {
56
        'id': ('id', ('The id of the user')),
57
        'real name': ('realname', 'The name of the user'),
58
        'active': ('is_active', 'Whether the user is active or not'),
59
        'verified':
60
        ('email_verified', 'Whether the user has a verified email address'),
61
        'moderated':
62
        ('moderated', 'Account moderated'),
63
        'admin': ('is_superuser', 'Whether the user is admin or not'),
64
        'uuid': ('uuid', 'The uuid of the user'),
65
        'providers': (get_providers,
66
                      'The authentication providers of the user'),
67
        'activation_sent': ('activation_sent',
68
                            'The date activation sent to the user'),
69
        'displayname': ('username', 'The display name of the user'),
70
        'groups': (get_groups, 'The groups of the user')
71
    }
72

    
73
    fields = ['id', 'real name', 'active', 'verified', 'moderated', 'admin',
74
              'uuid']
75

    
76
    option_list = ListCommand.option_list + (
77
        make_option('-p',
78
                    action='store_true',
79
                    dest='pending',
80
                    default=False,
81
                    help="List only users pending activation"),
82
        make_option('--auth-providers',
83
                    action='store_true',
84
                    dest='auth_providers',
85
                    default=False,
86
                    help="Display user authentication providers"),
87
        make_option('--group',
88
                    action='append',
89
                    dest='groups',
90
                    default=None,
91
                    help="Only show users that belong to the specified goups"),
92
        make_option('-n',
93
                    action='store_true',
94
                    dest='pending_send_mail',
95
                    default=False,
96
                    help="List only users who have not received activation"),
97
        make_option('--active',
98
                    action='store_true',
99
                    dest='active',
100
                    default=False,
101
                    help="Display only active users"),
102
        make_option('--pending-moderation',
103
                    action='store_true',
104
                    dest='pending_moderation',
105
                    default=False,
106
                    help="Display unmoderated users"),
107
        make_option('--pending-verification',
108
                    action='store_true',
109
                    dest='pending_verification',
110
                    default=False,
111
                    help="Display unverified users"),
112
        make_option("--displayname",
113
                    dest="displayname",
114
                    action="store_true",
115
                    default=False,
116
                    help="Display user displayname")
117
    )
118

    
119
    def handle_args(self, *args, **options):
120
        if options['pending']:
121
            self.filters['is_active'] = False
122

    
123
        if options['pending_send_mail']:
124
            self.filters['is_active'] = False
125
            self.filters['activation_sent'] = None
126

    
127
        if options['active']:
128
            self.filters['is_active'] = True
129

    
130
        if options['pending_moderation']:
131
            self.filters['email_verified'] = True
132
            self.filters['moderated'] = False
133

    
134
        if options['pending_verification']:
135
            self.filters['email_verified'] = False
136

    
137
        if options['auth_providers']:
138
            self.fields.extend(['providers'])
139

    
140
        if options['displayname']:
141
            self.fields.extend(['displayname'])