Revision 07a1f977 snf-astakos-app/astakos/im/management/commands/user-add.py

b/snf-astakos-app/astakos/im/management/commands/user-add.py
49 49

  
50 50

  
51 51
class Command(BaseCommand):
52
    args = "<email>"
52
    args = "<email> <first name> <last name>"
53 53
    help = "Create a user"
54 54

  
55 55
    option_list = BaseCommand.option_list + (
56
        make_option('--first-name',
57
                    dest='first_name',
58
                    metavar='NAME',
59
                    help="Set user's first name"),
60
        make_option('--last-name',
61
                    dest='last_name',
62
                    metavar='NAME',
63
                    help="Set user's last name"),
64 56
        make_option('--affiliation',
65 57
                    dest='affiliation',
66 58
                    metavar='AFFILIATION',
......
90 82
    )
91 83

  
92 84
    def handle(self, *args, **options):
93
        if len(args) != 1:
85
        if len(args) != 3:
94 86
            raise CommandError("Invalid number of arguments")
95 87

  
96
        email = args[0].decode('utf8')
97

  
88
        email, first_name, last_name = (args[i].decode('utf8') for i in range(3))
89
        
98 90
        try:
99 91
            validate_email(email)
100 92
        except ValidationError:
101 93
            raise CommandError("Invalid email")
102 94

  
103
        u = {'email': email}
95
        u = {'email': email,
96
             'first_name':first_name,
97
             'last_name':last_name
98
        }
104 99
        u.update(filter_custom_options(options))
105 100
        if not u.get('password'):
106 101
            u['password'] = AstakosUser.objects.make_random_password()
107 102

  
108 103
        try:
109 104
            c = AstakosCallpoint()
110
            r = c.create_users((u,))
105
            r = c.create_users((u,)).next()
111 106
        except socket.error, e:
112 107
            raise CommandError(e)
113 108
        except ValidationError, e:
114 109
            raise CommandError(e)
115 110
        else:
116
            failed = (res for res in r if not res.is_success)
117
            for r in failed:
118
                if not r.is_success:
119
                    raise CommandError(r.reason)
120
            if not failed:
121
                self.stdout.write('User created successfully')
122
                if not u.get('password'):
123
                    self.stdout.write('with password: %s' % u['password'])
111
            if not r.is_success:
112
                raise CommandError(r.reason)
113
            else:
114
                self.stdout.write('User created successfully ')
115
                if not options.get('password'):
116
                    self.stdout.write('with password: %s\n' % u['password'])
117
                else:
118
                    self.stdout.write('\n')

Also available in: Unified diff