This commit is contained in:
Ross
2021-05-14 20:15:45 +01:00
parent de0792cb54
commit 77ddc7b4b1
2 changed files with 13 additions and 12 deletions
+6 -8
View File
@@ -197,10 +197,9 @@ class AddQuestionNote(CreateView):
@csrf_exempt @csrf_exempt
def answer_suggestion_submit(request): def answer_suggestion_submit(request):
if request.is_ajax and request.method == "POST": if request.is_ajax and request.method == "POST":
j = json.loads(request.body.decode()) question_type, qid = request.POST.get("qid").split("/")
question_type, qid = j["qid"].split("/") answer_string = request.POST.get("answer")
answer_string = j["answer"] status = request.POST.get("status")
status = j["status"]
if str(status) not in " 012": if str(status) not in " 012":
return JsonResponse({"success": False, "error": "Invalid status"}) return JsonResponse({"success": False, "error": "Invalid status"})
@@ -229,9 +228,8 @@ def answer_suggestion_submit(request):
@login_required @login_required
def answer_suggestion_confirm(request): def answer_suggestion_confirm(request):
if request.is_ajax and request.method == "POST": if request.is_ajax and request.method == "POST":
j = json.loads(request.body.decode()) question_type = request.POST.get("question_type")
question_type = j["question_type"] aid = request.POST.get("aid")
aid = j["aid"]
if question_type == "anatomy": if question_type == "anatomy":
AnswerModel = AnatomyAnswer AnswerModel = AnatomyAnswer
@@ -242,7 +240,7 @@ def answer_suggestion_confirm(request):
else: else:
return JsonResponse({"success": False, "error": "Invalid question type"}) return JsonResponse({"success": False, "error": "Invalid question type"})
answer = get_object_or_404(AnswerModel, pk=qid) answer = get_object_or_404(AnswerModel, pk=aid)
answer.proposed = False answer.proposed = False
answer.save() answer.save()
return JsonResponse({"success": True, "error": "Answer changed"}) return JsonResponse({"success": True, "error": "Answer changed"})
@@ -62,14 +62,15 @@
// send request to change the is_private state on customSwitches toggle // send request to change the is_private state on customSwitches toggle
$(".proposed-answer").each((n, el) => { $(".proposed-answer").each((n, el) => {
$(el).append("<span>SAVE</span>").on("click", function () { $(el).append("<span class='confirm'>[Confirm]</span>").on("click", function () {
$.ajax({ $.ajax({
url: "{% url 'answer_suggestion_confirm' %}", url: "{% url 'answer_suggestion_confirm' %}",
data: { data: {
csrfmiddlewaretoken: "{{ csrf_token }}", csrfmiddlewaretoken: "{{ csrf_token }}",
//active: this.checked // true if checked else false //active: this.checked // true if checked else false
question_type: "rapid",
aid: el.dataset.aid
}, },
body: JSON.stringify({ question_type: "rapid", aid: el.dataset.aid}),
type: "POST", type: "POST",
dataType: "json", dataType: "json",
}) })
@@ -77,8 +78,10 @@
.done(function (data) { .done(function (data) {
console.log(data); console.log(data);
if (data.status == "success") { if (data.success) {
toastr.info('Exam state changed.') toastr.info('Answer saved')
$(el).find(".confirm").remove()
$(el).removeClass("proposed-answer")
} }
// show some message according to the response. // show some message according to the response.
// For eg. A message box showing that the status has been changed // For eg. A message box showing that the status has been changed