1818from ...misc import get_internal_api_header
1919
2020from ...auth .forms import UserAccountRegistrationForm , UserAccountEditForm
21- from ..forms import AdminUserAccountEditForm
21+ from ..forms import AdminUserAccountEditForm , ForgetPasswordForm
2222from ..forms .auth import AccountLockPasswordForm
2323from ...sample .enums import (
2424 SampleBaseType ,
2929 FluidContainer ,
3030 CellContainer ,
3131)
32- from ...database import TemporaryStore
32+ from ...protocol .enums import ProtocolType
33+ from ...database import TemporaryStore , db , UserAccount
3334
3435from flask import render_template , url_for , redirect , abort , flash , current_app , request
3536from flask_login import current_user , login_required
@@ -249,14 +250,17 @@ def populate_settings(
249250 def flatten_settings (name , settings_val , choices = [], setting = {}):
250251 name_choices = name + "_choices"
251252 name_default = name + "_default"
252- name_selected = name + "_selected"
253+ name_selected = name + "_selected" # display text for selected choices
254+
253255 try :
254256 setting [name_choices ] = settings_val ["choices" ]
255257
256258 if setting [name_choices ] is None or len (setting [name_choices ]) == 0 :
257- setting [name_choices ] = [s [0 ] for s in choices ]
259+ # setting[name_choices] = [s[0] for s in choices]
260+ setting [name_choices ] = []
258261 except :
259- setting [name_choices ] = [s [0 ] for s in choices ]
262+ # setting[name_choices] = [s[0] for s in choices]
263+ setting [name_choices ] = []
260264
261265 try :
262266 setting [name_default ] = settings_val ["default" ]
@@ -266,9 +270,12 @@ def flatten_settings(name, settings_val, choices=[], setting={}):
266270 except :
267271 setting [name_default ] = None
268272
269- setting [name_selected ] = "\n " .join (
270- [s [1 ] for s in choices if s [0 ] in setting [name_choices ]]
271- )
273+ if len (setting [name_choices ]) > 0 :
274+ setting [name_selected ] = "\n " .join (
275+ [s [1 ] for s in choices if s [0 ] in setting [name_choices ]]
276+ )
277+ else :
278+ setting [name_selected ] = ""
272279
273280 return setting
274281
@@ -289,11 +296,12 @@ def flatten_settings(name, settings_val, choices=[], setting={}):
289296 ]
290297
291298 settings = []
299+
292300 for access_type in settings_data :
293301 setting = {}
294302 for k in item_list :
295303 setting .update ({k + "_choices" : [], k + "_default" : None })
296- setting .update ({k + "_selected" : [] })
304+ setting .update ({k + "_selected" : "None" })
297305
298306 if access_type == "data_entry" :
299307 setting ["access_level" ] = 1
@@ -312,12 +320,12 @@ def flatten_settings(name, settings_val, choices=[], setting={}):
312320 setting ["site_selected" ] = "\n " .join (
313321 [s [1 ] for s in sites if s [0 ] in setting ["site_choices" ]]
314322 )
315-
323+ # print("setting: ", setting)
316324 # -- Consent templates
317325 if "consent_template" in item_list :
318326 try :
319- # settings_val = account_data["settings"][access_type]["consent_template"]
320327 settings_val = settings_data [access_type ]["consent_template" ]
328+ settings_val ["choices" ] = None # disable setting choices
321329 setting = flatten_settings (
322330 name = "consent_template" ,
323331 settings_val = settings_val ,
@@ -344,6 +352,7 @@ def flatten_settings(name, settings_val, choices=[], setting={}):
344352 if "collection_protocol" in item_list :
345353 try :
346354 settings_val = settings_data [access_type ]["protocol" ]["ACQ" ]
355+ settings_val ["choices" ] = None # disable setting choices
347356 setting = flatten_settings (
348357 name = "collection_protocol" ,
349358 settings_val = settings_val ,
@@ -357,6 +366,7 @@ def flatten_settings(name, settings_val, choices=[], setting={}):
357366 if "processing_protocol" in item_list :
358367 try :
359368 settings_val = settings_data [access_type ]["protocol" ]["SAP" ]
369+ settings_val ["choices" ] = None # disable setting choices
360370 setting = flatten_settings (
361371 name = "processing_protocol" ,
362372 settings_val = settings_val ,
@@ -488,7 +498,7 @@ def jsonise_settings(form, account_data):
488498 settings ["data_entry" ].update (
489499 {
490500 "consent_template" : {
491- "choices" : setting .consent_template_choices .data ,
501+ # "choices": setting.consent_template_choices.data,
492502 "default" : setting .consent_template_default .data ,
493503 },
494504 "protocol" : {
@@ -497,11 +507,11 @@ def jsonise_settings(form, account_data):
497507 "default" : setting .study_protocol_default .data ,
498508 },
499509 "ACQ" : {
500- "choices" : setting .collection_protocol_choices .data ,
510+ # "choices": setting.collection_protocol_choices.data,
501511 "default" : setting .collection_protocol_default .data ,
502512 },
503513 "SAP" : {
504- "choices" : setting .processing_protocol_choices .data ,
514+ # "choices": setting.processing_protocol_choices.data,
505515 "default" : setting .processing_protocol_default .data ,
506516 },
507517 },
@@ -599,6 +609,7 @@ def admin_edit_settings(id, use_template=None):
599609 study_protocols = []
600610 if protocols_response .status_code == 200 :
601611 study_protocols = protocols_response .json ()["content" ]["choices" ]
612+ # print("stu choices: ", study_protocols)
602613
603614 protocols_response = requests .get (
604615 url_for ("api.protocol_query_tokenuser" , default_type = "ACQ" , _external = True ),
@@ -644,6 +655,7 @@ def admin_edit_settings(id, use_template=None):
644655 None ,
645656 sites ,
646657 consent_templates ,
658+ study_protocols ,
647659 collection_protocols ,
648660 processing_protocols ,
649661 )
@@ -659,6 +671,7 @@ def admin_edit_settings(id, use_template=None):
659671 None ,
660672 sites ,
661673 consent_templates ,
674+ study_protocols ,
662675 collection_protocols ,
663676 processing_protocols ,
664677 )
@@ -680,16 +693,17 @@ def admin_edit_settings(id, use_template=None):
680693 for setting in form .settings .entries :
681694 setting .site_choices .choices = sites
682695 # setting.site_default.choices = sites
683- setting .consent_template_choices .choices = consent_templates
696+
697+ # setting.consent_template_choices.choices = consent_templates
684698 setting .consent_template_default .choices = consent_templates
685699
686700 setting .study_protocol_choices .choices = study_protocols
687701 setting .study_protocol_default .choices = study_protocols
688702
689- setting .collection_protocol_choices .choices = collection_protocols
703+ # setting.collection_protocol_choices.choices = collection_protocols
690704 setting .collection_protocol_default .choices = collection_protocols
691705
692- setting .processing_protocol_choices .choices = processing_protocols
706+ # setting.processing_protocol_choices.choices = processing_protocols
693707 setting .processing_protocol_default .choices = processing_protocols
694708
695709 if (
@@ -741,3 +755,44 @@ def admin_edit_settings(id, use_template=None):
741755 )
742756 else :
743757 return abort (response .status_code )
758+
759+
760+ @admin .route ("auth/forget_password" , methods = ["GET" , "POST" ])
761+ def auth_forget_password ():
762+ form = ForgetPasswordForm ()
763+ if form .validate_on_submit ():
764+ # get password reset token
765+ token_email = requests .post (
766+ url_for ("api.auth_forget_password" , _external = True ),
767+ headers = {
768+ "FlaskApp" : current_app .config .get ("SECRET_KEY" ),
769+ "Email" : form .email .data ,
770+ },
771+ json = {"email" : form .email .data },
772+ )
773+
774+ if token_email .status_code == 200 :
775+ token = token_email .json ()["content" ]["token" ]
776+ confirm_url = url_for (
777+ "auth.change_password_external" , token = token , _external = True
778+ )
779+ template = render_template (
780+ "admin/auth/email/password_reset.html" , reset_url = confirm_url
781+ )
782+ subject = "LIMBUS: Password Reset Email"
783+ msg = Message (
784+ subject ,
785+ recipients = [form .email .data ],
786+ html = template ,
787+ sender = current_app .config ["MAIL_USERNAME" ],
788+ )
789+
790+ # Send password reset email
791+ mail .send (msg )
792+ flash ("Password reset email has been sent!" )
793+ return redirect (url_for ("auth.login" ))
794+
795+ else :
796+ flash (token_email .json ()["message" ])
797+
798+ return render_template ("admin/auth/forget_password.html" , form = form )
0 commit comments