Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / modifyuser.py @ 3597f9c9

History | View | Annotate | Download (4 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 optparse import make_option
35 df48dd1b Giorgos Verigakis
36 df48dd1b Giorgos Verigakis
from django.core.management.base import BaseCommand, CommandError
37 df48dd1b Giorgos Verigakis
38 397d5cbe Giorgos Verigakis
from ._common import get_user
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 = "Modify a user's attributes"
44 df48dd1b Giorgos Verigakis
    
45 df48dd1b Giorgos Verigakis
    option_list = BaseCommand.option_list + (
46 df48dd1b Giorgos Verigakis
        make_option('--invitations',
47 df48dd1b Giorgos Verigakis
            dest='invitations',
48 df48dd1b Giorgos Verigakis
            metavar='NUM',
49 df48dd1b Giorgos Verigakis
            help="Update user's invitations"),
50 ebd369d0 Sofia Papagiannaki
        make_option('--level',
51 ebd369d0 Sofia Papagiannaki
            dest='level',
52 ebd369d0 Sofia Papagiannaki
            metavar='NUM',
53 ebd369d0 Sofia Papagiannaki
            help="Update user's level"),
54 df48dd1b Giorgos Verigakis
        make_option('--password',
55 df48dd1b Giorgos Verigakis
            dest='password',
56 df48dd1b Giorgos Verigakis
            metavar='PASSWORD',
57 df48dd1b Giorgos Verigakis
            help="Set user's password"),
58 df48dd1b Giorgos Verigakis
        make_option('--renew-token',
59 df48dd1b Giorgos Verigakis
            action='store_true',
60 df48dd1b Giorgos Verigakis
            dest='renew_token',
61 df48dd1b Giorgos Verigakis
            default=False,
62 df48dd1b Giorgos Verigakis
            help="Renew the user's token"),
63 df48dd1b Giorgos Verigakis
        make_option('--set-admin',
64 df48dd1b Giorgos Verigakis
            action='store_true',
65 df48dd1b Giorgos Verigakis
            dest='admin',
66 df48dd1b Giorgos Verigakis
            default=False,
67 df48dd1b Giorgos Verigakis
            help="Give user admin rights"),
68 df48dd1b Giorgos Verigakis
        make_option('--set-noadmin',
69 df48dd1b Giorgos Verigakis
            action='store_true',
70 df48dd1b Giorgos Verigakis
            dest='noadmin',
71 df48dd1b Giorgos Verigakis
            default=False,
72 df48dd1b Giorgos Verigakis
            help="Revoke user's admin rights"),
73 ebd369d0 Sofia Papagiannaki
        make_option('--set-active',
74 ebd369d0 Sofia Papagiannaki
            action='store_true',
75 ebd369d0 Sofia Papagiannaki
            dest='active',
76 ebd369d0 Sofia Papagiannaki
            default=False,
77 ebd369d0 Sofia Papagiannaki
            help="Change user's state to inactive"),
78 ebd369d0 Sofia Papagiannaki
        make_option('--set-inactive',
79 fc28d013 Sofia Papagiannaki
            action='store_true',
80 fc28d013 Sofia Papagiannaki
            dest='inactive',
81 fc28d013 Sofia Papagiannaki
            default=False,
82 fc28d013 Sofia Papagiannaki
            help="Change user's state to inactive"),
83 df48dd1b Giorgos Verigakis
        )
84 df48dd1b Giorgos Verigakis
    
85 df48dd1b Giorgos Verigakis
    def handle(self, *args, **options):
86 df48dd1b Giorgos Verigakis
        if len(args) != 1:
87 ebd369d0 Sofia Papagiannaki
            raise CommandError("Please provide a user ID or email")
88 df48dd1b Giorgos Verigakis
        
89 397d5cbe Giorgos Verigakis
        user = get_user(args[0])
90 397d5cbe Giorgos Verigakis
        if not user:
91 397d5cbe Giorgos Verigakis
            raise CommandError("Unknown user")
92 df48dd1b Giorgos Verigakis
        
93 df48dd1b Giorgos Verigakis
        if options.get('admin'):
94 df48dd1b Giorgos Verigakis
            user.is_superuser = True
95 df48dd1b Giorgos Verigakis
        elif options.get('noadmin'):
96 df48dd1b Giorgos Verigakis
            user.is_superuser = False
97 df48dd1b Giorgos Verigakis
        
98 ebd369d0 Sofia Papagiannaki
        if options.get('active'):
99 ebd369d0 Sofia Papagiannaki
            user.is_active = True
100 ebd369d0 Sofia Papagiannaki
        elif options.get('inactive'):
101 ebd369d0 Sofia Papagiannaki
            user.is_active = False
102 ebd369d0 Sofia Papagiannaki
        
103 df48dd1b Giorgos Verigakis
        invitations = options.get('invitations')
104 df48dd1b Giorgos Verigakis
        if invitations is not None:
105 df48dd1b Giorgos Verigakis
            user.invitations = int(invitations)
106 df48dd1b Giorgos Verigakis
        
107 ebd369d0 Sofia Papagiannaki
        level = options.get('level')
108 ebd369d0 Sofia Papagiannaki
        if level is not None:
109 ebd369d0 Sofia Papagiannaki
            user.level = int(level)
110 ebd369d0 Sofia Papagiannaki
        
111 df48dd1b Giorgos Verigakis
        password = options.get('password')
112 df48dd1b Giorgos Verigakis
        if password is not None:
113 df48dd1b Giorgos Verigakis
            user.set_password(password)
114 df48dd1b Giorgos Verigakis
        
115 df48dd1b Giorgos Verigakis
        if options['renew_token']:
116 df48dd1b Giorgos Verigakis
            user.renew_token()
117 df48dd1b Giorgos Verigakis
        
118 ebd369d0 Sofia Papagiannaki
        user.save()