Revision 2a6fc999 snf-astakos-app/astakos/im/management/commands/user-list.py

b/snf-astakos-app/astakos/im/management/commands/user-list.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
35 35

  
36 36
from django.core.management.base import NoArgsCommand
37 37

  
38
from astakos.im.models import AstakosUser
38
from astakos.im.models import AstakosUser, AstakosUserAuthProvider
39 39

  
40 40
from ._common import format
41 41

  
......
68 68
        elif options['pending_send_mail']:
69 69
            users = users.filter(is_active=False, activation_sent=None)
70 70

  
71
        ids = [user.id for user in users]
72
        auths = AstakosUserAuthProvider.objects.filter(
73
            user__in=ids, active=True)
74

  
75
        all_auth = partition_by(lambda a: a.user_id, auths)
76

  
71 77
        labels = ('id', 'email', 'real name', 'active', 'admin', 'uuid', 'providers')
72 78
        columns = (3, 24, 24, 6, 5, 12, 36, 24)
73 79

  
......
82 88
            active = user.is_active
83 89
            admin = user.is_superuser
84 90
            uuid = user.uuid or ''
91
            auths = all_auth[user.id]
92
            auth_display = ",".join(unicode(auth) for auth in auths)
85 93
            fields = (format(elem) for elem in (
86 94
                            id,
87 95
                            user.email,
88 96
                            user.realname,
89 97
                            active, admin, uuid,
90
                            user.auth_providers_display
98
                            auth_display
91 99
            ))
92 100

  
93 101
            if options['csv']:
......
96 104
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
97 105

  
98 106
            self.stdout.write(line + '\n')
107

  
108

  
109
def partition_by(f, l):
110
    d = {}
111
    for x in l:
112
        group = f(x)
113
        group_l = d.get(group, [])
114
        group_l.append(x)
115
        d[group] = group_l
116
    return d

Also available in: Unified diff