3333
3434def POST_api_accounts_new (request ):
3535 # An e-mail is provided, and if the e-mail already exists
36- # as an account, then return 403.
37-
38- bulk_request = request .data
39- # Instantiate any necessary imports.
40- db = DbUtils .DbUtils ()
41-
42- # Does the account associated with this e-mail already
43- # exist in either a temporary or a permanent user profile?
44- if (
45- db .check_user_exists (
46- p_app_label = "api" , p_model_name = "new_users" , p_email = bulk_request ["email" ]
47- )
48- is None
49- ):
50- if User .objects .filter (email = bulk_request ["email" ]).exists ():
51- # Account has already been activated.
52- return Response (
53- status = status .HTTP_409_CONFLICT ,
54- data = {"message" : "Account has already been activated." },
36+ # as an account, then return 409.
37+ try :
38+ # Instantiate any necessary imports.
39+ db = DbUtils .DbUtils ()
40+
41+ # Does the account associated with this e-mail already
42+ # exist in either a temporary or a permanent user profile?
43+ if (
44+ db .check_user_exists (
45+ p_app_label = "api" , p_model_name = "new_users" , p_email = request .data ["email" ]
46+ )
47+ is None
48+ ):
49+ if User .objects .filter (email = request .data ["email" ]).exists ():
50+ # Account has already been activated.
51+ return Response (
52+ status = status .HTTP_409_CONFLICT ,
53+ data = {"message" : "Account has already been activated." },
54+ )
55+
56+ # The email has not already been asked for and
57+ # it has not been activated.
58+
59+ # Generate a temp ID to use so that the account can
60+ # be activated.
61+
62+ # The data is based on whether or not a token was provided.
63+
64+ # Create a temporary identifier.
65+ temp_identifier = uuid .uuid4 ().hex
66+ if "token" in request .data and "hostname" in request .data :
67+ p_data = {
68+ "email" : request .data ["email" ],
69+ "temp_identifier" : temp_identifier ,
70+ "hostname" : request .data ["hostname" ],
71+ "token" : request .data ["token" ],
72+ }
73+
74+ else :
75+ p_data = {
76+ "email" : request .data ["email" ],
77+ "temp_identifier" : temp_identifier ,
78+ }
79+
80+ objects_written = db .write_object (
81+ p_app_label = "api" ,
82+ p_model_name = "new_users" ,
83+ p_fields = ["email" , "temp_identifier" , "hostname" , "token" ],
84+ p_data = p_data ,
5585 )
5686
57- # The email has not already been asked for and
58- # it has not been activated.
59-
60- # Generate a temp ID to use so that the account can
61- # be activated.
87+ if objects_written < 1 :
88+ # There is a problem with the write.
89+ return Response (
90+ status = status .HTTP_500_INTERNAL_SERVER_ERROR ,
91+ data = "Not able to save the new account." ,
92+ )
6293
63- # The data is based on whether or not a token was provided.
94+ # Send an e-mail to let the requestor know that they
95+ # need to follow the activation link within 10 minutes.
6496
65- # Create a temporary identifier.
66- temp_identifier = uuid .uuid4 ().hex
97+ # Source: https://realpython.com/python-send-email/#sending-fancy-emails
6798
68- if "token" in bulk_request and "hostname" in bulk_request :
69- p_data = {
70- "email" : bulk_request ["email" ],
71- "temp_identifier" : temp_identifier ,
72- "hostname" : bulk_request ["hostname" ],
73- "token" : bulk_request ["token" ],
74- }
99+ activation_link = ""
100+ template = ""
75101
76- else :
77- p_data = {
78- "email" : bulk_request ["email" ],
79- "temp_identifier" : temp_identifier ,
80- }
81-
82- objects_written = db .write_object (
83- p_app_label = "api" ,
84- p_model_name = "new_users" ,
85- p_fields = ["email" , "temp_identifier" , "hostname" , "token" ],
86- p_data = p_data ,
87- )
88-
89- if objects_written < 1 :
90- # There is a problem with the write.
91- return Response (
92- status = status .HTTP_500_INTERNAL_SERVER_ERROR ,
93- data = "Not able to save the new account." ,
102+ activation_link = (
103+ settings .PUBLIC_HOSTNAME
104+ + "/api/accounts/activate/"
105+ + urllib .parse .quote (request .data ["email" ])
106+ + "/"
107+ + temp_identifier
94108 )
95109
96- # Send an e-mail to let the requestor know that they
97- # need to follow the activation link within 10 minutes.
98-
99- # Source: https://realpython.com/python-send-email/#sending-fancy-emails
100-
101- activation_link = ""
102- template = ""
103-
104- activation_link = (
105- settings .PUBLIC_HOSTNAME
106- + "/api/accounts/activate/"
107- + urllib .parse .quote (bulk_request ["email" ])
108- + "/"
109- + temp_identifier
110- )
111-
112- template = '<html><body><p>Please click this link within the next 10 minutes to activate your BioCompute Portal account: <a href="{}" target="_blank">{}</a>.</p></body></html>' .format (
113- activation_link , activation_link
114- )
115-
116- try :
117- send_mail (
118- subject = "Registration for BioCompute Portal" ,
119- message = "Testing." ,
120- html_message = template ,
121- 122- recipient_list = [bulk_request ["email" ]],
123- fail_silently = False ,
110+ template = '<html><body><p>Please click this link within the next 10 minutes to activate your BioCompute Portal account: <a href="{}" target="_blank">{}</a>.</p></body></html>' .format (
111+ activation_link , activation_link
124112 )
125113
126- except Exception as e :
127- print ("activation_link" , activation_link )
128- # print('ERROR: ', e)
129- # TODO: Should handle when the send_mail function fails?
130- # return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={"message": "Not able to send authentication email: {}".format(e)})
131- return Response (status = status .HTTP_201_CREATED )
114+ try :
115+ send_mail (
116+ subject = "Registration for BioCompute Portal" ,
117+ message = "Testing." ,
118+ html_message = template ,
119+ 120+ recipient_list = [request .data ["email" ]],
121+ fail_silently = False ,
122+ )
123+ print ("Email signal sent" )
124+
125+ except Exception as error :
126+ print ("activation_link" , activation_link )
127+ print ('ERROR: ' , error )
128+ return Response (
129+ status = status .HTTP_201_CREATED , data = {
130+ "message" : f"Not able to send authentication email: { error } " ,
131+ "activation_link" : f"{ activation_link } "
132+ }
133+ )
134+
135+ if request .data ["token" ] == "SampleToken" :
136+ print ("testing with SampleToken" )
137+ return Response (
138+ status = status .HTTP_201_CREATED , data = {
139+ "message" : "Testing token received" ,
140+ "activation_link" : f"{ activation_link } "
141+ }
142+ )
143+
144+ return Response (status = status .HTTP_201_CREATED )
132145
133- else :
134-
135- # Account has already been asked for.
146+ else :
147+ return Response (
148+ status = status .HTTP_409_CONFLICT ,
149+ data = {"message" : "Account has already been requested." },
150+ )
151+ except :
136152 return Response (
137- status = status .HTTP_409_CONFLICT ,
138- data = {"message" : "Account has already been requested ." },
139- )
153+ status = status .HTTP_400_BAD_REQUEST ,
154+ data = {"message" : "Bad request format ." },
155+ )
0 commit comments