From 8c8f305f33cfd42c2fda46eaf4ea82382cb1ceba Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 9 Jan 2023 13:03:02 +0000 Subject: [PATCH] Allow CID users to request details by email --- generic/models.py | 16 +++++++++++++++- generic/views.py | 1 + rad/urls.py | 1 + rad/views.py | 20 ++++++++++++++++++++ templates/base.html | 2 +- templates/cid_selector.html | 10 +++++++++- 6 files changed, 47 insertions(+), 3 deletions(-) diff --git a/generic/models.py b/generic/models.py index 31ed447e..bcef0275 100644 --- a/generic/models.py +++ b/generic/models.py @@ -1,4 +1,5 @@ from collections import defaultdict +from typing import Tuple from django.contrib.auth.mixins import LoginRequiredMixin from django.db import models from django.http import HttpRequest @@ -544,6 +545,10 @@ class CidUser(models.Model): html_msg = f"""""" def email_results(self, resend=False, additional_emails=[]): + """Emails a CID candidate results (internal candidates) + + ***DEPRECATED*** + """ if not self.internal_candidate: return False, "Not internal candidate." @@ -574,12 +579,21 @@ class CidUser(models.Model): self.save() return True, "" - def email_details(self, resend=False): + def email_details(self, resend: bool=False) -> Tuple[bool, str]: + """Email candidate details to the CID user + + Args: + resend (bool, optional): Force sending details (even if sent previously). Defaults to False. + + Returns: + Tuple[bool, str]: Tuple containing the email status and any error message + """ if self.login_email_sent and not resend: return False, "Already sent." login_url = reverse("cid_selector") + msg = f""" See below for your details for: {self.group} diff --git a/generic/views.py b/generic/views.py index 2590c9f8..bde45181 100644 --- a/generic/views.py +++ b/generic/views.py @@ -2227,6 +2227,7 @@ class CidUserView(CidManagerRequiredMixin, SingleTableMixin, FilterView): return context + @user_is_cid_user_manager def cid_group_view(request): groups = CidUserGroup.objects.filter(archive=False) diff --git a/rad/urls.py b/rad/urls.py index 3699d0b2..ea17e5d6 100644 --- a/rad/urls.py +++ b/rad/urls.py @@ -90,6 +90,7 @@ urlpatterns = [ path("cid//", views.cid_scores_admin, name="cid_scores_admin"), path("user/scores", views.user_scores, name="user_scores"), path("cid/", views.cid_selector, name="cid_selector"), + path("cid/request_cid_details/", views.request_cid_details, name="request_cid_details"), # Global url that registers RTS compatible exams path("exam/json/", views.active_exams, name="active_exams"), path( diff --git a/rad/views.py b/rad/views.py index 1ace4bd1..d87ac5f7 100644 --- a/rad/views.py +++ b/rad/views.py @@ -11,6 +11,9 @@ from django.shortcuts import render, get_object_or_404, redirect from django.views.decorators.csrf import csrf_exempt from django import forms +from django.core.exceptions import ValidationError +from django.core.validators import validate_email + from django.utils.html import format_html # from django.contrib.auth.models import User @@ -724,3 +727,20 @@ def accounts_bulk_create(request): return render(request, "accounts_bulk_create.html", context) else: return render(request, "accounts_bulk_create.html", {}) + + +def request_cid_details(request): + email = request.POST.get("email") + try: + validate_email(email) + except ValidationError as e: + return HttpResponse("Invalid email.") + + cid_users = CidUser.objects.filter(email=email) + + for cid_user in cid_users: + cid_user.email_details(resend=True) + cid_user.email_details() + + + return HttpResponse("Candidate details will be sent to the requested email (if it is found in the system).") diff --git a/templates/base.html b/templates/base.html index 4714eee0..4ed5476e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -66,7 +66,7 @@ - +