Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.9 kB)

1 9d20fe23 Kostas Papadimitriou
# Copyright 2012 GRNET S.A. All rights reserved.
2 9d20fe23 Kostas Papadimitriou
#
3 9d20fe23 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 9d20fe23 Kostas Papadimitriou
# without modification, are permitted provided that the following
5 9d20fe23 Kostas Papadimitriou
# conditions are met:
6 9d20fe23 Kostas Papadimitriou
#
7 9d20fe23 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 9d20fe23 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 9d20fe23 Kostas Papadimitriou
#      disclaimer.
10 9d20fe23 Kostas Papadimitriou
#
11 9d20fe23 Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 9d20fe23 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 9d20fe23 Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 9d20fe23 Kostas Papadimitriou
#      provided with the distribution.
15 9d20fe23 Kostas Papadimitriou
#
16 9d20fe23 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 9d20fe23 Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 9d20fe23 Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 9d20fe23 Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 9d20fe23 Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 9d20fe23 Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 9d20fe23 Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 9d20fe23 Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 9d20fe23 Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 9d20fe23 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 9d20fe23 Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 9d20fe23 Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 9d20fe23 Kostas Papadimitriou
#
29 9d20fe23 Kostas Papadimitriou
# The views and conclusions contained in the software and
30 9d20fe23 Kostas Papadimitriou
# documentation are those of the authors and should not be
31 9d20fe23 Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 9d20fe23 Kostas Papadimitriou
# or implied, of GRNET S.A.
33 9d20fe23 Kostas Papadimitriou
34 9d20fe23 Kostas Papadimitriou
from optparse import make_option
35 9d20fe23 Kostas Papadimitriou
36 9d20fe23 Kostas Papadimitriou
from django.core.management.base import NoArgsCommand
37 9d20fe23 Kostas Papadimitriou
38 9d20fe23 Kostas Papadimitriou
from django.db.models import Count
39 9d20fe23 Kostas Papadimitriou
from astakos.im.models import Group
40 9d20fe23 Kostas Papadimitriou
41 9d20fe23 Kostas Papadimitriou
from ._common import format
42 9d20fe23 Kostas Papadimitriou
43 9d20fe23 Kostas Papadimitriou
44 9d20fe23 Kostas Papadimitriou
class Command(NoArgsCommand):
45 9d20fe23 Kostas Papadimitriou
    help = "List available groups"
46 9d20fe23 Kostas Papadimitriou
47 9d20fe23 Kostas Papadimitriou
    option_list = NoArgsCommand.option_list + (
48 9d20fe23 Kostas Papadimitriou
        make_option('-c',
49 9d20fe23 Kostas Papadimitriou
                    action='store_true',
50 9d20fe23 Kostas Papadimitriou
                    dest='csv',
51 9d20fe23 Kostas Papadimitriou
                    default=False,
52 9d20fe23 Kostas Papadimitriou
                    help="Use pipes to separate values"),
53 9d20fe23 Kostas Papadimitriou
    )
54 9d20fe23 Kostas Papadimitriou
55 9d20fe23 Kostas Papadimitriou
    def handle_noargs(self, **options):
56 9d20fe23 Kostas Papadimitriou
        objects = Group.objects.annotate(
57 9d20fe23 Kostas Papadimitriou
            users_count=Count('user')).order_by('id')
58 9d20fe23 Kostas Papadimitriou
59 9d20fe23 Kostas Papadimitriou
        labels = ['id', 'name', 'users count']
60 9d20fe23 Kostas Papadimitriou
        columns = [3, 40, 20]
61 9d20fe23 Kostas Papadimitriou
62 9d20fe23 Kostas Papadimitriou
        if not options['csv']:
63 9d20fe23 Kostas Papadimitriou
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
64 9d20fe23 Kostas Papadimitriou
            self.stdout.write(line + '\n')
65 9d20fe23 Kostas Papadimitriou
            sep = '-' * len(line)
66 9d20fe23 Kostas Papadimitriou
            self.stdout.write(sep + '\n')
67 9d20fe23 Kostas Papadimitriou
68 9d20fe23 Kostas Papadimitriou
        for group in objects:
69 9d20fe23 Kostas Papadimitriou
            id = str(group.pk)
70 9d20fe23 Kostas Papadimitriou
            name = str(group.name)
71 9d20fe23 Kostas Papadimitriou
            user_count = str(group.users_count)
72 9d20fe23 Kostas Papadimitriou
            field_values = [id, name, user_count]
73 9d20fe23 Kostas Papadimitriou
            fields = (format(elem) for elem in field_values)
74 9d20fe23 Kostas Papadimitriou
75 9d20fe23 Kostas Papadimitriou
            if options['csv']:
76 9d20fe23 Kostas Papadimitriou
                line = '|'.join(fields)
77 9d20fe23 Kostas Papadimitriou
            else:
78 9d20fe23 Kostas Papadimitriou
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
79 9d20fe23 Kostas Papadimitriou
80 9d20fe23 Kostas Papadimitriou
            self.stdout.write(line + '\n')