.
This commit is contained in:
@@ -18,6 +18,7 @@ from django.urls import reverse_lazy, reverse
|
||||
|
||||
from django.http import Http404, JsonResponse
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from .forms import (
|
||||
ExamAuthorForm,
|
||||
@@ -988,3 +989,81 @@ class AnswerAutocomplete(autocomplete.Select2QuerySetView):
|
||||
qs = qs.filter(answer__istartswith=self.q)
|
||||
|
||||
return qs
|
||||
|
||||
|
||||
def confirm_answer(request, answer_id: int):
|
||||
if request.htmx:
|
||||
answer = get_object_or_404(Answer, pk=answer_id)
|
||||
|
||||
# Check if the user has permission to confirm the answer
|
||||
if not answer.can_edit(request.user):
|
||||
return HttpResponse("Invalid permissions", status=403)
|
||||
|
||||
answer.clean()
|
||||
answer.proposed = False
|
||||
answer.save()
|
||||
return HttpResponse("Answer accepted")
|
||||
|
||||
raise PermissionDenied()
|
||||
|
||||
def delete_answer(request, answer_id: int):
|
||||
if request.htmx:
|
||||
answer = get_object_or_404(Answer, pk=answer_id)
|
||||
|
||||
# Check if the user has permission to confirm the answer
|
||||
if not answer.can_edit(request.user):
|
||||
return HttpResponse("Invalid permissions", status=403)
|
||||
|
||||
answer.delete()
|
||||
return HttpResponse("Answer deleted")
|
||||
raise PermissionDenied()
|
||||
|
||||
def question_add_exam(request, question_id: int):
|
||||
if request.htmx:
|
||||
question = get_object_or_404(Rapid, pk=question_id)
|
||||
|
||||
if not question.can_edit(request.user):
|
||||
return HttpResponse("Invalid permissions", status=403)
|
||||
|
||||
|
||||
if request.POST:
|
||||
exam_id = request.POST.get("exam_id", None)
|
||||
|
||||
if exam_id is None:
|
||||
return HttpResponse("No exam id provided", status=400)
|
||||
|
||||
exam = get_object_or_404(Exam, pk=exam_id)
|
||||
|
||||
if request.POST.get("remove", False) == "true":
|
||||
question.exams.remove(exam)
|
||||
return HttpResponse("Question removed from exam.")
|
||||
else:
|
||||
question.exams.add(exam)
|
||||
|
||||
return HttpResponse("Question added to exam.")
|
||||
else:
|
||||
# Return a list of exams that we can add to the question
|
||||
|
||||
exams = Exam.objects.filter(author=request.user) | Exam.objects.filter(open_access=True)
|
||||
exams = exams.difference(question.exams.all())
|
||||
|
||||
if not exams:
|
||||
return HttpResponse("No exams available to add")
|
||||
|
||||
html = "Exams to add to question: <br>"
|
||||
for exam in exams:
|
||||
html = html + (f"<span id='htmx-exam-list'><form><input name='exam_id' value='{exam.id}' type='hidden'><button hx-post=\"{request.path}\""
|
||||
f">{exam.name}: {exam.id}</button></form>"
|
||||
)
|
||||
html = html + "<br>Exams to remove from question: <br>"
|
||||
for exam in question.exams.all():
|
||||
html = html + (f"<span id='htmx-exam-list'><form><input name='exam_id' value='{exam.id}' type='hidden'><input name='remove' value='true' type='hidden'><button hx-post=\"{request.path}\""
|
||||
f"hx-confirm='Are you sure you want to remove this exam from the question?'"
|
||||
f">{exam.name}: {exam.id}</button></form>"
|
||||
)
|
||||
html = html + "<button _='on click remove #htmx-exam-list'>Cancel</button>"
|
||||
|
||||
return HttpResponse(mark_safe(html))
|
||||
|
||||
|
||||
raise PermissionDenied()
|
||||
Reference in New Issue
Block a user