Revision df48dd1b

b/astakos/im/management/commands/activateuser.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37

  
38
from astakos.im.models import AstakosUser
39
    
40

  
41
class Command(BaseCommand):
42
    args = "<user_id or email> [user_id or email] ..."
43
    help = "Activates one or more users"
44
    
45
    def handle(self, *args, **options):
46
        if not args:
47
            raise CommandError("No user was given")
48
        
49
        for email_or_id in args:
50
            try:
51
                if email_or_id.isdigit():
52
                    user = AstakosUser.objects.get(id=int(email_or_id))
53
                else:
54
                    user = AstakosUser.objects.get(email=email_or_id)
55
            except AstakosUser.DoesNotExist:
56
                field = 'id' if email_or_id.isdigit() else 'email'
57
                msg = "Unknown user with %s '%s'" % (field, email_or_id)
58
                self.stderr.write(msg + '\n')
59
                continue
60
            
61
            if user.is_active:
62
                msg = "User '%s' already active\n" % (user.email,)
63
                self.stderr.write(msg)
64
                continue
65
            
66
            user.is_active = True
67
            user.save()
68
            
69
            self.stdout.write("Activated '%s'\n" % (user.email,))
b/astakos/im/management/commands/createuser.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35
from random import choice
36
from string import digits, lowercase, uppercase
37
from uuid import uuid4
38

  
39
from django.core.management.base import BaseCommand, CommandError
40

  
41
from astakos.im.models import AstakosUser
42

  
43

  
44
def generate_password():
45
    pool = lowercase + uppercase + digits
46
    return ''.join(choice(pool) for i in range(10))
47

  
48

  
49
class Command(BaseCommand):
50
    args = "<email> <first name> <last name> <affiliation>"
51
    help = "Modify a user's attributes"
52
    
53
    option_list = BaseCommand.option_list + (
54
        make_option('--active',
55
            action='store_true',
56
            dest='active',
57
            default=False,
58
            help="Activate user"),
59
        make_option('--admin',
60
            action='store_true',
61
            dest='admin',
62
            default=False,
63
            help="Give user admin rights"),
64
        make_option('--password',
65
            dest='password',
66
            metavar='PASSWORD',
67
            help="Set user's password")
68
        )
69
    
70
    def handle(self, *args, **options):
71
        if len(args) != 4:
72
            raise CommandError("Invalid number of arguments")
73
        
74
        args = [a.decode('utf8') for a in args]
75
        email, first, last, affiliation = args
76
        
77
        username =  uuid4().hex[:30]
78
        password = options.get('password')
79
        if password is None:
80
            password = generate_password()
81
        
82
        user = AstakosUser(username=username, first_name=first, last_name=last,
83
                           email=email, affiliation=affiliation,
84
                           provider='local')
85
        user.set_password(password)
86
        user.renew_token()
87
        
88
        if options['active']:
89
            user.is_active = True
90
        if options['admin']:
91
            user.is_admin = True
92
        
93
        user.save()
94
        
95
        msg = "Created user id %d" % (user.id,)
96
        if options['password'] is None:
97
            msg += " with password '%s'" % (password,)
98
        self.stdout.write(msg + '\n')
b/astakos/im/management/commands/listinvitations.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37

  
38
from astakos.im.models import Invitation
39

  
40

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

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37

  
38
from astakos.im.models import AstakosUser
39

  
40

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

  
34
from optparse import make_option
35

  
36
from django.core.management.base import BaseCommand, CommandError
37

  
38
from astakos.im.models import AstakosUser
39

  
40

  
41
class Command(BaseCommand):
42
    args = "<user_id or email>"
43
    help = "Modify a user's attributes"
44
    
45
    option_list = BaseCommand.option_list + (
46
        make_option('--invitations',
47
            dest='invitations',
48
            metavar='NUM',
49
            help="Update user's invitations"),
50
        make_option('--password',
51
            dest='password',
52
            metavar='PASSWORD',
53
            help="Set user's password"),
54
        make_option('--renew-token',
55
            action='store_true',
56
            dest='renew_token',
57
            default=False,
58
            help="Renew the user's token"),
59
        make_option('--set-admin',
60
            action='store_true',
61
            dest='admin',
62
            default=False,
63
            help="Give user admin rights"),
64
        make_option('--set-noadmin',
65
            action='store_true',
66
            dest='noadmin',
67
            default=False,
68
            help="Revoke user's admin rights"),
69
        )
70
    
71
    def handle(self, *args, **options):
72
        if len(args) != 1:
73
            raise CommandError("Please provide a user_id or email")
74
        
75
        email_or_id = args[0]
76
        try:
77
            if email_or_id.isdigit():
78
                user = AstakosUser.objects.get(id=int(email_or_id))
79
            else:
80
                user = AstakosUser.objects.get(email=email_or_id)
81
        except AstakosUser.DoesNotExist:
82
            field = 'id' if email_or_id.isdigit() else 'email'
83
            msg = "Unknown user with %s '%s'" % (field, email_or_id)
84
            raise CommandError(msg)
85
        
86
        if options.get('admin'):
87
            user.is_superuser = True
88
        elif options.get('noadmin'):
89
            user.is_superuser = False
90
        
91
        invitations = options.get('invitations')
92
        if invitations is not None:
93
            user.invitations = int(invitations)
94
        
95
        password = options.get('password')
96
        if password is not None:
97
            user.set_password(password)
98
        
99
        if options['renew_token']:
100
            user.renew_token()
101
        
102
        user.save()
b/astakos/im/management/commands/showinvitation.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from datetime import datetime
35
from optparse import make_option
36

  
37
from django.core.management.base import BaseCommand, CommandError
38
from django.utils.timesince import timesince, timeuntil
39

  
40
from astakos.im.models import Invitation
41

  
42

  
43
def format_bool(b):
44
    return 'YES' if b else 'NO'
45

  
46
def format_date(d):
47
    if not d:
48
        return ''
49
    
50
    if d < datetime.now():
51
        return timesince(d) + ' ago'
52
    else:
53
        return 'in ' + timeuntil(d)
54

  
55

  
56
class Command(BaseCommand):
57
    help = "Show user info"
58
    
59
    def handle(self, *args, **options):
60
        if len(args) != 1:
61
            raise CommandError("Please provide an invitation id")
62
        
63
        try:
64
            invitation = Invitation.objects.get(id=int(args[0]))
65
        except Invitation.DoesNotExist:
66
            raise CommandError("Unknown invitation id '%s'" % (args[0],))
67
        
68
        kv = {
69
            'id': invitation.id,
70
            'real name': invitation.realname,
71
            'email': invitation.username,
72
            'code': invitation.code,
73
            'accepted': format_bool(invitation.is_accepted),
74
            'consumed': format_bool(invitation.is_consumed),
75
            'date created': format_date(invitation.created),
76
            'date accepted': format_date(invitation.accepted),
77
            'date consumed': format_date(invitation.consumed),
78
            'inviter real name': invitation.inviter.realname,
79
            'invitater email': invitation.inviter.email,
80
        }
81
        
82
        for key, val in sorted(kv.items()):
83
            line = '%s: %s\n' % (key.rjust(18), val)
84
            self.stdout.write(line.encode('utf8'))
b/astakos/im/management/commands/showuser.py
1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

  
34
from datetime import datetime
35
from optparse import make_option
36

  
37
from django.core.management.base import BaseCommand, CommandError
38
from django.utils.timesince import timesince, timeuntil
39

  
40
from astakos.im.models import AstakosUser
41

  
42

  
43
def format_bool(b):
44
    return 'YES' if b else 'NO'
45

  
46
def format_date(d):
47
    if d < datetime.now():
48
        return timesince(d) + ' ago'
49
    else:
50
        return 'in ' + timeuntil(d)
51

  
52

  
53
class Command(BaseCommand):
54
    help = "Show user info"
55
    
56
    def handle(self, *args, **options):
57
        if len(args) != 1:
58
            raise CommandError("Please provide a user_id or email")
59
        
60
        email_or_id = args[0]
61
        try:
62
            if email_or_id.isdigit():
63
                user = AstakosUser.objects.get(id=int(email_or_id))
64
            else:
65
                user = AstakosUser.objects.get(email=email_or_id)
66
        except AstakosUser.DoesNotExist:
67
            field = 'id' if email_or_id.isdigit() else 'email'
68
            msg = "Unknown user with %s '%s'" % (field, email_or_id)
69
            raise CommandError(msg)
70
        
71
        kv = {
72
            'id': user.id,
73
            'email': user.email,
74
            'first name': user.first_name,
75
            'last name': user.last_name,
76
            'active': format_bool(user.is_active),
77
            'admin': format_bool(user.is_superuser),
78
            'last login': format_date(user.last_login),
79
            'date joined': format_date(user.date_joined),
80
            'last update': format_date(user.updated),
81
            'token': user.auth_token,
82
            'token expiration': format_date(user.auth_token_expires),
83
            'invitations': user.invitations,
84
            'invitation level': user.level,
85
            'provider': user.provider,
86
            'verified': format_bool(user.is_verified)
87
        }
88
        
89
        for key, val in sorted(kv.items()):
90
            line = '%s: %s\n' % (key.rjust(16), val)
91
            self.stdout.write(line.encode('utf8'))

Also available in: Unified diff