From 832f589c80580aabb00ebd18f4bdd6cc97e20a4b Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 21 Apr 2022 16:55:31 +0100 Subject: [PATCH] . --- generic/templates/generic/exam_cids_edit.html | 37 +++++++++++++++++-- generic/views.py | 19 ++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/generic/templates/generic/exam_cids_edit.html b/generic/templates/generic/exam_cids_edit.html index d139ab26..8cddf9b5 100644 --- a/generic/templates/generic/exam_cids_edit.html +++ b/generic/templates/generic/exam_cids_edit.html @@ -4,15 +4,15 @@
    {% for cid in current_cid_users %} -
  1. {{cid.cid}} [{{cid.passcode}}] {{cid.name}} / - Email: {{cid.email}}
  2. {% endfor %} {% for cid in available_cid_users %}
  3. {{cid.cid}} [{{cid.passcode}}] {{cid.name}} / - Email: {{cid.email}}
  4. {% endfor %} @@ -24,7 +24,36 @@ $(document).ready(() => { $(".toggle-btn").click((el) => { console.log(el); - console.log(el.dataset.pk); + console.log(el.target.dataset.pk); + console.log(!el.target.parent.hasClass("current")); + $.ajax({ + url: "{% url 'generic:generic_exam_json_edit' %}"", + data: { + csrfmiddlewaretoken: "{{csrf_token}}", + edit_cid_user: JSON.stringify(ids), + add: !el.target.parent.hasClass("current"), + exam_id: $(evt.target).data("exam-id"), + }, + type: "POST", + dataType: "json", + }) + // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly + .done(function (data) { + console.log(data); + + if (data.status == "success") { + if (data.added) { + el.target.parent.addClass("current"); + toastr.info(`User ${el.target.dataset.cid} added to exam`) + } else { + el.target.parent.removedClass("current"); + toastr.info(`User ${el.target.dataset.cid} removed from exam`) + + } + } + }) + .always(function () { + }) }); diff --git a/generic/views.py b/generic/views.py index d8cedf50..9f3f6702 100644 --- a/generic/views.py +++ b/generic/views.py @@ -673,10 +673,23 @@ class ExamViews(View, LoginRequiredMixin): exam = get_object_or_404(self.Exam, pk=pk) - if "edit_cid_users" in request.POST: - user_ids = json.loads(request.POST.get("cid_user_ids")) + if "toggle_cid_user" in request.POST: + user_id = json.loads(request.POST.get("cid_user_id")) + + add = request.POST.get("add") - data = {"status": "success"} + cid_user = CidUser.objects.filter(pk=user_id) + + app_exam_map = {} + app_exam_map["rapids"] = cid_user.rapid_exams + + if add: + app_exam_map[self.app_name].add(pk) + else: + app_exam_map[self.app_name].remove(pk) + + + data = {"status": "success", "added": add} return JsonResponse(data, status=200) if "add_exam_questions" in request.POST: