Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / user-show.py @ 075b91de

History | View | Annotate | Download (5.3 kB)

1 a3c6203c Giorgos Korfiatis
# Copyright 2012, 2013 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 a0699ee4 Sofia Papagiannaki
from django.core.management.base import CommandError
35 91eca048 Giorgos Korfiatis
from optparse import make_option
36 df48dd1b Giorgos Verigakis
37 3362d142 Sofia Papagiannaki
from astakos.im.models import AstakosUser, get_latest_terms
38 91eca048 Giorgos Korfiatis
from astakos.im.quotas import list_user_quotas
39 21fabaad Sofia Papagiannaki
40 a3c6203c Giorgos Korfiatis
from synnefo.lib.ordereddict import OrderedDict
41 a0699ee4 Sofia Papagiannaki
from synnefo.webproject.management.commands import SynnefoCommand
42 a0699ee4 Sofia Papagiannaki
from synnefo.webproject.management import utils
43 df48dd1b Giorgos Verigakis
44 91eca048 Giorgos Korfiatis
from ._common import format, show_quotas
45 df48dd1b Giorgos Verigakis
46 094c0768 Sofia Papagiannaki
import uuid
47 df48dd1b Giorgos Verigakis
48 865849d7 Giorgos Korfiatis
49 a0699ee4 Sofia Papagiannaki
class Command(SynnefoCommand):
50 a0699ee4 Sofia Papagiannaki
    args = "<user ID or email or uuid>"
51 df48dd1b Giorgos Verigakis
    help = "Show user info"
52 5ce3ce4f Sofia Papagiannaki
53 91eca048 Giorgos Korfiatis
    option_list = SynnefoCommand.option_list + (
54 075b91de Giorgos Korfiatis
        make_option('--quota',
55 91eca048 Giorgos Korfiatis
                    action='store_true',
56 91eca048 Giorgos Korfiatis
                    dest='list_quotas',
57 91eca048 Giorgos Korfiatis
                    default=False,
58 075b91de Giorgos Korfiatis
                    help="Also list user quota"),
59 91eca048 Giorgos Korfiatis
    )
60 91eca048 Giorgos Korfiatis
61 df48dd1b Giorgos Verigakis
    def handle(self, *args, **options):
62 df48dd1b Giorgos Verigakis
        if len(args) != 1:
63 ebd369d0 Sofia Papagiannaki
            raise CommandError("Please provide a user ID or email")
64 5ce3ce4f Sofia Papagiannaki
65 094c0768 Sofia Papagiannaki
        identifier = args[0]
66 094c0768 Sofia Papagiannaki
        if identifier.isdigit():
67 094c0768 Sofia Papagiannaki
            users = AstakosUser.objects.filter(id=int(identifier))
68 0a569195 Sofia Papagiannaki
        else:
69 094c0768 Sofia Papagiannaki
            try:
70 094c0768 Sofia Papagiannaki
                uuid.UUID(identifier)
71 094c0768 Sofia Papagiannaki
            except:
72 094c0768 Sofia Papagiannaki
                users = AstakosUser.objects.filter(email__iexact=identifier)
73 094c0768 Sofia Papagiannaki
            else:
74 094c0768 Sofia Papagiannaki
                users = AstakosUser.objects.filter(uuid=identifier)
75 0a569195 Sofia Papagiannaki
        if users.count() == 0:
76 094c0768 Sofia Papagiannaki
            field = 'id' if identifier.isdigit() else 'email'
77 094c0768 Sofia Papagiannaki
            msg = "Unknown user with %s '%s'" % (field, identifier)
78 df48dd1b Giorgos Verigakis
            raise CommandError(msg)
79 5ce3ce4f Sofia Papagiannaki
80 0a569195 Sofia Papagiannaki
        for user in users:
81 c7c0ec58 Giorgos Korfiatis
            settings_dict = {}
82 c7c0ec58 Giorgos Korfiatis
            settings = user.settings()
83 c7c0ec58 Giorgos Korfiatis
            for setting in settings:
84 c7c0ec58 Giorgos Korfiatis
                settings_dict[setting.setting] = setting.value
85 c7c0ec58 Giorgos Korfiatis
86 a3c6203c Giorgos Korfiatis
            kv = OrderedDict(
87 a3c6203c Giorgos Korfiatis
                [
88 a3c6203c Giorgos Korfiatis
                    ('id', user.id),
89 a3c6203c Giorgos Korfiatis
                    ('uuid', user.uuid),
90 a3c6203c Giorgos Korfiatis
                    ('email', user.email),
91 a3c6203c Giorgos Korfiatis
                    ('first name', user.first_name),
92 a3c6203c Giorgos Korfiatis
                    ('last name', user.last_name),
93 a3c6203c Giorgos Korfiatis
                    ('active', user.is_active),
94 a3c6203c Giorgos Korfiatis
                    ('admin', user.is_superuser),
95 a3c6203c Giorgos Korfiatis
                    ('last login', user.last_login),
96 a3c6203c Giorgos Korfiatis
                    ('date joined', user.date_joined),
97 a3c6203c Giorgos Korfiatis
                    ('last update', user.updated),
98 a3c6203c Giorgos Korfiatis
                    #('token', user.auth_token),
99 a3c6203c Giorgos Korfiatis
                    ('token expiration', user.auth_token_expires),
100 a3c6203c Giorgos Korfiatis
                    ('invitations', user.invitations),
101 a3c6203c Giorgos Korfiatis
                    ('invitation level', user.level),
102 a3c6203c Giorgos Korfiatis
                    ('providers', user.auth_providers_display),
103 a3c6203c Giorgos Korfiatis
                    ('verified', user.is_verified),
104 21fabaad Sofia Papagiannaki
                    ('has credits', format(user.has_credits)),
105 a3c6203c Giorgos Korfiatis
                    ('groups', [elem.name for elem in user.groups.all()]),
106 a3c6203c Giorgos Korfiatis
                    ('permissions', [elem.codename
107 a3c6203c Giorgos Korfiatis
                                     for elem in user.user_permissions.all()]),
108 21fabaad Sofia Papagiannaki
                    ('group permissions', user.get_group_permissions()),
109 21fabaad Sofia Papagiannaki
                    ('email verified', user.email_verified),
110 a3c6203c Giorgos Korfiatis
                    ('username', user.username),
111 a3c6203c Giorgos Korfiatis
                    ('activation_sent_date', user.activation_sent),
112 a3c6203c Giorgos Korfiatis
                ])
113 a3c6203c Giorgos Korfiatis
114 c7c0ec58 Giorgos Korfiatis
            if settings_dict:
115 c7c0ec58 Giorgos Korfiatis
                kv['settings'] = settings_dict
116 c7c0ec58 Giorgos Korfiatis
117 3362d142 Sofia Papagiannaki
            if get_latest_terms():
118 fcf90160 Sofia Papagiannaki
                has_signed_terms = user.signed_terms
119 437c0052 Sofia Papagiannaki
                kv['has_signed_terms'] = has_signed_terms
120 3362d142 Sofia Papagiannaki
                if has_signed_terms:
121 437c0052 Sofia Papagiannaki
                    kv['date_signed_terms'] = user.date_signed_terms
122 5ce3ce4f Sofia Papagiannaki
123 a0699ee4 Sofia Papagiannaki
            utils.pprint_table(self.stdout, [kv.values()], kv.keys(),
124 a0699ee4 Sofia Papagiannaki
                               options["output_format"], vertical=True)
125 91eca048 Giorgos Korfiatis
126 91eca048 Giorgos Korfiatis
            if options["list_quotas"]:
127 91eca048 Giorgos Korfiatis
                self.stdout.write("\n")
128 91eca048 Giorgos Korfiatis
                _, quotas, initial, _ = list_user_quotas([user])
129 91eca048 Giorgos Korfiatis
                print_data, labels = show_quotas(quotas, initial)
130 91eca048 Giorgos Korfiatis
                utils.pprint_table(self.stdout, print_data, labels,
131 91eca048 Giorgos Korfiatis
                                   options["output_format"])