1212from datetime import timedelta , datetime
1313from django .conf import settings
1414from apps .authorization .models import update_grants
15+ from apps .authorization .models import ArchivedDataAccessGrant , DataAccessGrant
16+
17+ # Imports for quieting things during startup.
18+ from waffle .models import Switch
1519
1620
1721def create_group (name = "BlueButton" ):
@@ -24,57 +28,95 @@ def create_group(name="BlueButton"):
2428 return g
2529
2630
31+ def get_switch (name ):
32+ try :
33+ sw = Switch .objects .get (name = name )
34+ return sw .active
35+ except Exception as e :
36+ print (f"Could not get switch { name } : { e } " )
37+
38+
39+ def set_switch (name , b ):
40+ # DISABLE SOME WAFFLE SWITCHES
41+ # We don't want email, etc.
42+ sw , _ = Switch .objects .get_or_create (name = name )
43+ sw .active = b
44+ sw .save ()
45+
46+ # usr would be a string if it is anything
47+
48+
2749def create_user (group , usr ):
28- u_name = "fred "
29- first_name = "Fred"
30- last_name = "Flinstone "
31- email = "fred@example.com "
32- password = "foobarfoobarfoobar "
50+ u_name = "rogersf "
51+ first_name = "Fred"
52+ last_name = "Rogers "
53+ email = "fred@landofmakebelieve.gov "
54+ password = "danielthetiger "
3355 user_type = "BEN"
34-
56+
3557 if usr is not None :
3658 u_name = usr
37- first_name = "{}{}" .format (usr , "First" )
59+ first_name = "{}{}" .format (usr , "First" )
3860 last_name = "{}{}" .format (usr , "Last" )
39- email = "{}.{}@example.com " .format (first_name , last_name )
61+ email = "{}.{}@{} " .format (first_name , last_name , email )
4062 user_type = "DEV"
4163
64+ # This violates constraints on other tables.
65+ usr_q = User .objects .filter (username = u_name )
66+ if usr_q .exists ():
67+ # Delete any ADAGs for this user, or we will run into a
68+ # constraint issue at startup.
69+ count = ArchivedDataAccessGrant .objects .filter (beneficiary = usr_q .first ()).delete ()
70+ print (f"Deleted { count } ADAGs for { u_name } " )
71+ count = DataAccessGrant .objects .filter (beneficiary = usr_q .first ()).delete ()
72+ print (f"Deleted { count } ADAGs for { u_name } " )
4273
43- if User .objects .filter (username = u_name ).exists ():
4474 User .objects .filter (username = u_name ).delete ()
4575
4676 u = None
4777
4878 if usr is not None :
49- u = User .objects .create_user (username = u_name ,
50- first_name = first_name ,
51- last_name = last_name ,
52- email = email )
53- u .set_unusable_password ()
79+ try :
80+ u , _ = User .objects .get_or_create (username = u_name ,
81+ first_name = first_name ,
82+ last_name = last_name ,
83+ email = email ,
84+ signals_to_disable = ["post_save" ])
85+ u .set_unusable_password ()
86+ except Exception as e :
87+ print (f"Did not create user: { e } " )
5488 else :
5589 # create a sample user 'fred' for dev local that has a usable password
56- u = User .objects .create_user (username = u_name ,
57- first_name = first_name ,
58- last_name = last_name ,
59- email = email ,
60- password = password ,)
61-
62- UserProfile .objects .create (user = u ,
63- user_type = user_type ,
64- create_applications = True ,
65- password_reset_question_1 = '1' ,
66- password_reset_answer_1 = 'blue' ,
67- password_reset_question_2 = '2' ,
68- password_reset_answer_2 = 'Frank' ,
69- password_reset_question_3 = '3' ,
70- password_reset_answer_3 = 'Bentley' )
71-
72- u .groups .add (group )
73-
74- if usr is None :
75- c , g_o_c = Crosswalk .objects .get_or_create (user = u ,
76- fhir_id_v2 = settings .DEFAULT_SAMPLE_FHIR_ID_V2 ,
77- _user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536" )
90+ try :
91+ # get_or_create returns a tuple (v, bool)
92+ u , _ = User .objects .get_or_create (username = u_name ,
93+ first_name = first_name ,
94+ last_name = last_name ,
95+ email = email ,
96+ password = password ,)
97+
98+ UserProfile .objects .create (user = u ,
99+ user_type = user_type ,
100+ create_applications = True ,
101+ password_reset_question_1 = '1' ,
102+ password_reset_answer_1 = 'blue' ,
103+ password_reset_question_2 = '2' ,
104+ password_reset_answer_2 = 'Frank' ,
105+ password_reset_question_3 = '3' ,
106+ password_reset_answer_3 = 'Bentley' )
107+ except Exception as e :
108+ print (f"Did not create user and profile: { e } " )
109+
110+ if u is None :
111+ print (f"Error creating user; exiting." )
112+ else :
113+ u .groups .add (group )
114+
115+ user_id_hash = "ee78989d1d9ba0b98f3cfbd52479f10c7631679c17563186f70fbef038cc9536"
116+ Crosswalk .objects .filter (_user_id_hash = user_id_hash ).delete ()
117+ c , _ = Crosswalk .objects .get_or_create (user = u ,
118+ fhir_id_v2 = settings .DEFAULT_SAMPLE_FHIR_ID_V2 ,
119+ _user_id_hash = user_id_hash )
78120 return u
79121
80122
@@ -86,26 +128,29 @@ def create_application(user, group, app, redirect):
86128 if redirect :
87129 redirect_uri = redirect
88130
89- if not (redirect_uri .startswith ("http://" ) or redirect_uri .startswith ("https://" )):
131+ if not (redirect_uri .startswith ("http://" ) or redirect_uri .startswith ("https://" )):
90132 redirect_uri = "https://" + redirect_uri
91133
92- a = Application .objects .create (name = app_name ,
93- redirect_uris = redirect_uri ,
94- user = user ,
95- data_access_type = "THIRTEEN_MONTH" ,
96- client_type = "confidential" ,
97- authorization_grant_type = "authorization-code" )
134+ try :
135+ a = Application .objects .create (name = app_name ,
136+ redirect_uris = redirect_uri ,
137+ user = user ,
138+ data_access_type = "THIRTEEN_MONTH" ,
139+ client_type = "confidential" ,
140+ authorization_grant_type = "authorization-code" ,)
98141
99- titles = ["My Medicare and supplemental coverage information." ,
100- "My Medicare claim information." ,
101- "My general patient and demographic information." ,
102- "Profile information including name and email."
103- ]
142+ titles = ["My Medicare and supplemental coverage information." ,
143+ "My Medicare claim information." ,
144+ "My general patient and demographic information." ,
145+ "Profile information including name and email."
146+ ]
104147
105- for t in titles :
106- c = ProtectedCapability .objects .get (title = t )
107- a .scope .add (c )
108- return a
148+ for t in titles :
149+ c = ProtectedCapability .objects .get (title = t )
150+ a .scope .add (c )
151+ return a
152+ except Exception as e :
153+ print (f"Skipped creation of { app_name } : { e } " )
109154
110155
111156def create_test_token (user , application ):
@@ -121,7 +166,8 @@ def create_test_token(user, application):
121166 t = AccessToken .objects .create (user = user , application = application ,
122167 token = "sample-token-string" ,
123168 expires = expires ,
124- scope = ' ' .join (scope ))
169+ scope = ' ' .join (scope ),)
170+
125171 return t
126172
127173
@@ -134,12 +180,15 @@ def add_arguments(self, parser):
134180 parser .add_argument ("-r" , "--redirect" , help = "Redirect url of the application." )
135181
136182 def handle (self , * args , ** options ):
137- usr = options [ "user" ]
138- app = options [ "app" ]
183+ usr = options . get ( "user" , None )
184+ app = options . get ( "app" , None )
139185 redirect = options ["redirect" ]
140186
187+ set_switch ('outreach_email' , False )
188+
141189 g = create_group ()
142190 u = create_user (g , usr )
191+ print (f"Created user { u } " )
143192 a = create_application (u , g , app , redirect )
144193 t = None
145194 if usr is None and app is None :
@@ -150,3 +199,6 @@ def handle(self, *args, **options):
150199 print ("client_secret:" , a .client_secret )
151200 print ("access_token:" , t .token if t else "None" )
152201 print ("redirect_uri:" , a .redirect_uris )
202+
203+ # Restore switch to whatever it was.
204+ set_switch ('outreach_email' , True )
0 commit comments