Add email preview functionality for candidate details: allow users to preview email content before sending

This commit is contained in:
Ross
2025-12-22 10:26:22 +00:00
parent 54e1cf4ce0
commit 7b47a75657
3 changed files with 51 additions and 22 deletions
+33 -21
View File
@@ -1552,27 +1552,9 @@ class CidUser(models.Model):
if self.login_email_sent and not resend: if self.login_email_sent and not resend:
return False, "Already sent." return False, "Already sent."
login_url = reverse("cid_selector") # Build the message payload. Exposed as a separate method so callers
# can preview the email without actually sending it.
msg = f""" msg = self.generate_email_details_msg()
See below for your details for: {self.group}
---------
CID: {self.cid}
Passcode: {self.passcode}
---------
You will need these to access to the test system.
Direct link to login:
{settings.REMOTE_URL}{login_url}?cid={self.cid}&passcode={self.passcode}
The above details are unique to you. Please do not share as otherwise others will be able to access and submit your results.
Please note this inbox is not monitored. Direct all enquiries to plh-tr.radiology-academy@nhs.net
"""
try: try:
# send_mail( # send_mail(
@@ -1597,6 +1579,36 @@ class CidUser(models.Model):
self.save() self.save()
return True, "" return True, ""
def generate_email_details_msg(self) -> str:
"""Generate the candidate details email body without sending it.
Returns:
str: The plaintext email body that would be sent.
"""
login_url = reverse("cid_selector")
msg = f"""
See below for your details for: {self.group}
---------
CID: {self.cid}
Passcode: {self.passcode}
---------
You will need these to access to the test system.
Direct link to login:
{settings.REMOTE_URL}{login_url}?cid={self.cid}&passcode={self.passcode}
The above details are unique to you. Please do not share as otherwise others will be able to access and submit your results.
Please note this inbox is not monitored. Direct all enquiries to plh-tr.radiology-academy@nhs.net
"""
return msg
def get_email_details_url(self): def get_email_details_url(self):
return reverse("generic:candidate_email_details", kwargs={"cid": self.cid}) return reverse("generic:candidate_email_details", kwargs={"cid": self.cid})
@@ -22,6 +22,7 @@
{% endif %} {% endif %}
{% if user.email %} {% if user.email %}
<button type="button" class="btn btn-sm btn-outline-primary ms-2 send-login-email-btn" data-cid="{{ user.cid }}" data-url="{% url 'generic:candidate_email_details' user.cid %}">Send</button> <button type="button" class="btn btn-sm btn-outline-primary ms-2 send-login-email-btn" data-cid="{{ user.cid }}" data-url="{% url 'generic:candidate_email_details' user.cid %}">Send</button>
<a class="btn btn-sm btn-outline-secondary ms-2" href="{{ user.get_email_details_url }}?preview=1" target="_blank">View</a>
{% else %} {% else %}
<span class="small text-muted ms-2">No email</span> <span class="small text-muted ms-2">No email</span>
{% endif %} {% endif %}
+16
View File
@@ -5043,6 +5043,22 @@ def candidate_email_details(request, cid, resend=False):
except Exception: except Exception:
resend_flag = False resend_flag = False
# Allow a preview mode which returns the generated email body without
# actually sending the email. Useful for reviewing content in the UI.
preview_flag = False
try:
if request.method == 'POST':
preview_flag = str(request.POST.get('preview', '')).lower() in ('1', 'true', 'yes')
else:
preview_flag = str(request.GET.get('preview', '')).lower() in ('1', 'true', 'yes')
except Exception:
preview_flag = False
if preview_flag:
# Return plaintext body for preview (opens cleanly in a new tab/window)
msg = user.generate_email_details_msg()
return HttpResponse(msg, content_type='text/plain')
email_sent, comment = user.email_details(resend=resend_flag) email_sent, comment = user.email_details(resend=resend_flag)
logger.debug(f"Email details sent to CID {cid}: sent={email_sent}, comment={comment}") logger.debug(f"Email details sent to CID {cid}: sent={email_sent}, comment={comment}")