<s> from __future__ import unicode_literals , absolute_import , print_function <EOL> from optparse import make_option <EOL> from django . contrib . auth . models import User <EOL> from django . core . management import BaseCommand <EOL> from corehq import privileges <EOL> from django_prbac . models import UserRole , Role , Grant <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , * args , ** options ) : <EOL> ops_role = Role . objects . get_or_create ( <EOL> name = "<STR_LIT>" , <EOL> slug = privileges . OPERATIONS_TEAM , <EOL> ) [ <NUM_LIT:0> ] <EOL> accounting_admin = Role . objects . get_or_create ( <EOL> name = "<STR_LIT>" , <EOL> slug = privileges . ACCOUNTING_ADMIN , <EOL> ) [ <NUM_LIT:0> ] <EOL> if not ops_role . has_privilege ( accounting_admin ) : <EOL> Grant . objects . create ( <EOL> from_role = ops_role , <EOL> to_role = accounting_admin , <EOL> ) <EOL> remove_user = options . get ( '<STR_LIT>' , False ) <EOL> for arg in args : <EOL> try : <EOL> user = User . objects . get ( username = arg ) <EOL> try : <EOL> user_role = UserRole . objects . get ( user = user ) <EOL> except UserRole . DoesNotExist : <EOL> user_privs = Role . objects . get_or_create ( <EOL> name = "<STR_LIT>" % user . username , <EOL> slug = "<STR_LIT>" % user . username , <EOL> ) [ <NUM_LIT:0> ] <EOL> user_role = UserRole . objects . create ( <EOL> user = user , <EOL> role = user_privs , <EOL> ) <EOL> if remove_user : <EOL> try : <EOL> grant = Grant . objects . get ( <EOL> from_role = user_role . role , <EOL> to_role = ops_role <EOL> ) <EOL> grant . delete ( ) <EOL> print ( "<STR_LIT>" <EOL> % user . username ) <EOL> except Grant . DoesNotExist : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" % user . username ) <EOL> elif not user_role . has_privilege ( ops_role ) : <EOL> Grant . objects . create ( <EOL> from_role = user_role . role , <EOL> to_role = ops_role , <EOL> ) <EOL> print ( "<STR_LIT>" % user . username ) <EOL> else : <EOL> print ( "<STR_LIT>" <EOL> % user . username ) <EOL> except User . DoesNotExist : <EOL> print ( "<STR_LIT>" % arg ) </s>
