Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.7 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 snf_django.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('--auth-providers',
78
                    action='store_true',
79
                    dest='auth_providers',
80
                    default=False,
81
                    help="Display user authentication providers"),
82
        make_option('--group',
83
                    action='append',
84
                    dest='groups',
85
                    default=None,
86
                    help="Only show users that belong to the specified goups"),
87
        make_option('--active',
88
                    action='store_true',
89
                    dest='active',
90
                    default=False,
91
                    help="Display only active users"),
92
        make_option('--pending-moderation',
93
                    action='store_true',
94
                    dest='pending_moderation',
95
                    default=False,
96
                    help="Display unmoderated users"),
97
        make_option('--pending-verification',
98
                    action='store_true',
99
                    dest='pending_verification',
100
                    default=False,
101
                    help="Display unverified users"),
102
        make_option("--displayname",
103
                    dest="displayname",
104
                    action="store_true",
105
                    default=False,
106
                    help="Display user displayname")
107
    )
108

    
109
    def handle_args(self, *args, **options):
110
        if options['active']:
111
            self.filters['is_active'] = True
112

    
113
        if options['pending_moderation']:
114
            self.filters['email_verified'] = True
115
            self.filters['moderated'] = False
116

    
117
        if options['pending_verification']:
118
            self.filters['email_verified'] = False
119

    
120
        if options['auth_providers']:
121
            self.fields.extend(['providers'])
122

    
123
        if options['displayname']:
124
            self.fields.extend(['displayname'])