From 74c5c2ee082bb04d6ca21c4a0bb7afa2b6a201d6 Mon Sep 17 00:00:00 2001
From: Catherine Pitt <cen1001@cam.ac.uk>
Date: Thu, 13 Oct 2022 10:30:26 +0100
Subject: [PATCH] Reload supervisors list when safety form loaded

See RT 221400. The list of people who are allowed to sign off safety
forms is generated from the database. Because of the way WTForms works,
the list is set when the code to define the safety form class is first
run, and each safety form instance that is created gets the same list
from the class. This is annoying when new people have been added to the
list, as the applicaton doesn't see them until it restarts and hence
re-runs the code to define the class.

This change reloads the choices on the safety induction signer field on
instances of the safety form immediately after they are created in the
safety checklist view, thus immediately picking up new signers.
---
 chemistry_starters/views/safety_checklist.py | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/chemistry_starters/views/safety_checklist.py b/chemistry_starters/views/safety_checklist.py
index 0941f5e..0e7ed6c 100644
--- a/chemistry_starters/views/safety_checklist.py
+++ b/chemistry_starters/views/safety_checklist.py
@@ -53,6 +53,11 @@ def safety_checklist_form():
         safety_form = forms.SafetyChecklist(data=form_data)
     else:
         safety_form = forms.SafetyChecklistWithTraining(data=form_data)
+    # reload the supervisors choices from the database here, otherwise they may be stale
+    # as the initial list is created when the app first imports the form
+    safety_form.safety_induction_person_signing_off_id.choices = database.get_hids(
+        "supervisor", null_option=""
+    )
     if request.method == "GET":
         return render_template(
             "safety/safety_form.html",
-- 
GitLab