Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.2 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 django.core.management.base import BaseCommand, CommandError
35 df48dd1b Giorgos Verigakis
36 3362d142 Sofia Papagiannaki
from astakos.im.models import AstakosUser, get_latest_terms
37 df48dd1b Giorgos Verigakis
38 397d5cbe Giorgos Verigakis
from ._common import format_bool, format_date
39 df48dd1b Giorgos Verigakis
40 df48dd1b Giorgos Verigakis
41 df48dd1b Giorgos Verigakis
class Command(BaseCommand):
42 ebd369d0 Sofia Papagiannaki
    args = "<user ID or email>"
43 df48dd1b Giorgos Verigakis
    help = "Show user info"
44 df48dd1b Giorgos Verigakis
    
45 df48dd1b Giorgos Verigakis
    def handle(self, *args, **options):
46 df48dd1b Giorgos Verigakis
        if len(args) != 1:
47 ebd369d0 Sofia Papagiannaki
            raise CommandError("Please provide a user ID or email")
48 df48dd1b Giorgos Verigakis
        
49 df48dd1b Giorgos Verigakis
        email_or_id = args[0]
50 0a569195 Sofia Papagiannaki
        if email_or_id.isdigit():
51 0a569195 Sofia Papagiannaki
            users = AstakosUser.objects.filter(id=int(email_or_id))
52 0a569195 Sofia Papagiannaki
        else:
53 0a569195 Sofia Papagiannaki
            users = AstakosUser.objects.filter(email=email_or_id)
54 0a569195 Sofia Papagiannaki
        if users.count() == 0:
55 df48dd1b Giorgos Verigakis
            field = 'id' if email_or_id.isdigit() else 'email'
56 df48dd1b Giorgos Verigakis
            msg = "Unknown user with %s '%s'" % (field, email_or_id)
57 df48dd1b Giorgos Verigakis
            raise CommandError(msg)
58 df48dd1b Giorgos Verigakis
        
59 0a569195 Sofia Papagiannaki
        for user in users:
60 0a569195 Sofia Papagiannaki
            kv = {
61 0a569195 Sofia Papagiannaki
                'id': user.id,
62 0a569195 Sofia Papagiannaki
                'email': user.email,
63 0a569195 Sofia Papagiannaki
                'first name': user.first_name,
64 0a569195 Sofia Papagiannaki
                'last name': user.last_name,
65 0a569195 Sofia Papagiannaki
                'active': format_bool(user.is_active),
66 0a569195 Sofia Papagiannaki
                'admin': format_bool(user.is_superuser),
67 0a569195 Sofia Papagiannaki
                'last login': format_date(user.last_login),
68 0a569195 Sofia Papagiannaki
                'date joined': format_date(user.date_joined),
69 0a569195 Sofia Papagiannaki
                'last update': format_date(user.updated),
70 30dc8c1a Sofia Papagiannaki
                #'token': user.auth_token,
71 0a569195 Sofia Papagiannaki
                'token expiration': format_date(user.auth_token_expires),
72 0a569195 Sofia Papagiannaki
                'invitations': user.invitations,
73 0a569195 Sofia Papagiannaki
                'invitation level': user.level,
74 0a569195 Sofia Papagiannaki
                'provider': user.provider,
75 0a569195 Sofia Papagiannaki
                'verified': format_bool(user.is_verified),
76 0a569195 Sofia Papagiannaki
                'has_credits': format_bool(user.has_credits),
77 304acb60 Olga Brani
                'groups': [elem.name for elem in user.astakos_groups.all()],
78 30dc8c1a Sofia Papagiannaki
                'permissions': [elem.codename for elem in user.user_permissions.all()],
79 30dc8c1a Sofia Papagiannaki
                'group_permissions': user.get_group_permissions(),
80 0a569195 Sofia Papagiannaki
                'third_party_identifier': user.third_party_identifier,
81 30dc8c1a Sofia Papagiannaki
                'email_verified': format_bool(user.email_verified),
82 814113c4 Sofia Papagiannaki
                'username': user.username,
83 304acb60 Olga Brani
                'activation_sent_date': format_date(user.activation_sent),
84 304acb60 Olga Brani
                'resources' : user.quota
85 0a569195 Sofia Papagiannaki
            }
86 3362d142 Sofia Papagiannaki
            if get_latest_terms():
87 3362d142 Sofia Papagiannaki
                has_signed_terms = user.signed_terms()
88 3362d142 Sofia Papagiannaki
                kv['has_signed_terms'] = format_bool(has_signed_terms)
89 3362d142 Sofia Papagiannaki
                if has_signed_terms:
90 3362d142 Sofia Papagiannaki
                    kv['date_signed_terms'] = format_date(user.date_signed_terms)
91 0a569195 Sofia Papagiannaki
            
92 0a569195 Sofia Papagiannaki
            for key, val in sorted(kv.items()):
93 0a569195 Sofia Papagiannaki
                line = '%s: %s\n' % (key.rjust(22), val)
94 0a569195 Sofia Papagiannaki
                self.stdout.write(line.encode('utf8'))
95 0a569195 Sofia Papagiannaki
            self.stdout.write('\n')