11"""A command to set the API key for a user using when using TokenAuthentication."""
22
3- from optparse import make_option
4-
53from django .contrib .auth import get_user_model
64from django .core .management .base import BaseCommand , CommandError
75
@@ -15,29 +13,35 @@ class Command(BaseCommand):
1513 """A command to set the API key for a user using when using TokenAuthentication."""
1614
1715 help = 'Set the API key for the specified user.'
18- args = '<username> <api_key>'
19- option_list = BaseCommand .option_list + (
20- make_option ('--delete-key' , action = 'store_true' , default = False , help = "Delete API key for user" ),
21- )
16+
17+ def add_arguments (self , parser ):
18+ parser .add_argument ('username' , nargs = '?' )
19+ parser .add_argument ('api_key' , nargs = '?' )
20+ parser .add_argument (
21+ '--delete-key' ,
22+ action = 'store_true' ,
23+ default = False ,
24+ help = "Delete API key for user" ,
25+ )
2226
2327 def handle (self , * args , ** options ):
24- if len ( args ) < 1 :
28+ if options [ 'username' ] is None :
2529 raise CommandError ("You must supply a username." )
2630
27- username = args [ 0 ]
31+ username = options [ 'username' ]
2832
2933 if options ['delete_key' ]:
3034 delete_user_auth_token (username )
3135 print 'Removed API key for user: <{0}>' .format (username )
3236 else :
33- if len ( args ) < 2 :
37+ if options [ 'api_key' ] is None :
3438 raise CommandError ("You must supply both a username and key." )
3539
3640 # pylint: disable=no-member
3741 user , _ = User .objects .get_or_create (username = username )
3842
3943 try :
40- key = args [ 1 ]
44+ key = options [ 'api_key' ]
4145 set_user_auth_token (user , key )
4246 except AttributeError :
4347 print "The key %s is in use by another user. Please select another key." % key
0 commit comments