Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user_list.py @ 304acb60

History | View | Annotate | Download (3.5 kB)

1 ca96a346 Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 df48dd1b Giorgos Verigakis
#
3 df48dd1b Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 df48dd1b Giorgos Verigakis
# without modification, are permitted provided that the following
5 df48dd1b Giorgos Verigakis
# conditions are met:
6 df48dd1b Giorgos Verigakis
#
7 df48dd1b Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 df48dd1b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 df48dd1b Giorgos Verigakis
#      disclaimer.
10 df48dd1b Giorgos Verigakis
#
11 df48dd1b Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 df48dd1b Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 df48dd1b Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 df48dd1b Giorgos Verigakis
#      provided with the distribution.
15 df48dd1b Giorgos Verigakis
#
16 df48dd1b Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 df48dd1b Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 df48dd1b Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 df48dd1b Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 df48dd1b Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 df48dd1b Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 df48dd1b Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 df48dd1b Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 df48dd1b Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 df48dd1b Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 df48dd1b Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 df48dd1b Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 df48dd1b Giorgos Verigakis
#
29 df48dd1b Giorgos Verigakis
# The views and conclusions contained in the software and
30 df48dd1b Giorgos Verigakis
# documentation are those of the authors and should not be
31 df48dd1b Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 df48dd1b Giorgos Verigakis
# or implied, of GRNET S.A.
33 df48dd1b Giorgos Verigakis
34 df48dd1b Giorgos Verigakis
from optparse import make_option
35 df48dd1b Giorgos Verigakis
36 df48dd1b Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 df48dd1b Giorgos Verigakis
38 df48dd1b Giorgos Verigakis
from astakos.im.models import AstakosUser
39 df48dd1b Giorgos Verigakis
40 397d5cbe Giorgos Verigakis
from ._common import format_bool
41 397d5cbe Giorgos Verigakis
42 df48dd1b Giorgos Verigakis
43 df48dd1b Giorgos Verigakis
class Command(BaseCommand):
44 df48dd1b Giorgos Verigakis
    help = "List users"
45 df48dd1b Giorgos Verigakis
    
46 df48dd1b Giorgos Verigakis
    option_list = BaseCommand.option_list + (
47 df48dd1b Giorgos Verigakis
        make_option('-c',
48 df48dd1b Giorgos Verigakis
            action='store_true',
49 df48dd1b Giorgos Verigakis
            dest='csv',
50 df48dd1b Giorgos Verigakis
            default=False,
51 df48dd1b Giorgos Verigakis
            help="Use pipes to separate values"),
52 df48dd1b Giorgos Verigakis
        make_option('-p',
53 df48dd1b Giorgos Verigakis
            action='store_true',
54 df48dd1b Giorgos Verigakis
            dest='pending',
55 df48dd1b Giorgos Verigakis
            default=False,
56 df48dd1b Giorgos Verigakis
            help="List only users pending activation"),
57 751d24cf Sofia Papagiannaki
        make_option('-n',
58 751d24cf Sofia Papagiannaki
            action='store_true',
59 751d24cf Sofia Papagiannaki
            dest='pending_send_mail',
60 751d24cf Sofia Papagiannaki
            default=False,
61 751d24cf Sofia Papagiannaki
            help="List only users who have not received activation"),
62 df48dd1b Giorgos Verigakis
        )
63 df48dd1b Giorgos Verigakis
    
64 df48dd1b Giorgos Verigakis
    def handle(self, *args, **options):
65 df48dd1b Giorgos Verigakis
        if args:
66 df48dd1b Giorgos Verigakis
            raise CommandError("Command doesn't accept any arguments")
67 df48dd1b Giorgos Verigakis
        
68 df48dd1b Giorgos Verigakis
        users = AstakosUser.objects.all()
69 df48dd1b Giorgos Verigakis
        if options['pending']:
70 df48dd1b Giorgos Verigakis
            users = users.filter(is_active=False)
71 751d24cf Sofia Papagiannaki
        elif options['pending_send_mail']:
72 751d24cf Sofia Papagiannaki
            users = users.filter(is_active=False, activation_sent=None)
73 df48dd1b Giorgos Verigakis
        
74 304acb60 Olga Brani
        labels = ('id', 'email', 'real name', 'active', 'admin', 'provider')
75 751d24cf Sofia Papagiannaki
        columns = (3, 24, 24, 6, 5, 12, 24)
76 df48dd1b Giorgos Verigakis
        
77 df48dd1b Giorgos Verigakis
        if not options['csv']:
78 df48dd1b Giorgos Verigakis
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
79 df48dd1b Giorgos Verigakis
            self.stdout.write(line + '\n')
80 df48dd1b Giorgos Verigakis
            sep = '-' * len(line)
81 df48dd1b Giorgos Verigakis
            self.stdout.write(sep + '\n')
82 df48dd1b Giorgos Verigakis
        
83 df48dd1b Giorgos Verigakis
        for user in users:
84 df48dd1b Giorgos Verigakis
            id = str(user.id)
85 397d5cbe Giorgos Verigakis
            active = format_bool(user.is_active)
86 397d5cbe Giorgos Verigakis
            admin = format_bool(user.is_superuser)
87 304acb60 Olga Brani
            fields = (id, user.email, user.realname, active, admin, user.provider)
88 df48dd1b Giorgos Verigakis
            
89 df48dd1b Giorgos Verigakis
            if options['csv']:
90 df48dd1b Giorgos Verigakis
                line = '|'.join(fields)
91 df48dd1b Giorgos Verigakis
            else:
92 df48dd1b Giorgos Verigakis
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
93 df48dd1b Giorgos Verigakis
            
94 df48dd1b Giorgos Verigakis
            self.stdout.write(line.encode('utf8') + '\n')