Add QuestionReview model and implement review functionality with HTMX integration

This commit is contained in:
Ross
2025-10-22 21:00:01 +01:00
parent 6d589ef630
commit b417266e56
6 changed files with 125 additions and 0 deletions
+52
View File
@@ -1988,6 +1988,7 @@ class ExamViews(View, LoginRequiredMixin):
"view_feedback": view_feedback,
"can_edit": can_edit,
"remote_url": settings.REMOTE_URL,
"app_name": self.app_name,
},
)
@@ -2947,6 +2948,56 @@ class GenericViewBase:
question_json = question.get_question_json(based=False, feedback=True)
return JsonResponse(question_json)
def question_set_review(self, request, pk):
"""HTMX endpoint to get/set the latest QuestionReview for a question.
GET: renders the current review block or the form (if ?edit=1)
POST: creates a new QuestionReview and returns the rendered block
"""
from django.contrib.contenttypes.models import ContentType
from generic.models import QuestionReview
logger.debug("question_set_review called")
question = get_object_or_404(self.question_object, pk=pk)
ct = ContentType.objects.get_for_model(question)
latest = (
QuestionReview.objects.filter(content_type=ct, object_id=question.pk)
.order_by("-created_on").first()
)
# If GET and edit requested, return the form. If `new` is set, render with no latest so the
# form is blank for creating a fresh review.
if request.method == "GET" and request.GET.get("edit"):
if request.GET.get("new"):
return render(request, "generic/partials/question_review_form.html", {"question": question, "latest": None, "app_name": self.app_name})
return render(request, "generic/partials/question_review_form.html", {"question": question, "latest": latest, "app_name": self.app_name})
if request.method == "POST":
# Require login to post a review
if not request.user.is_authenticated:
return HttpResponse(status=403)
status = request.POST.get("status")
comment = request.POST.get("comment", "")
review = QuestionReview.objects.create(
content_type=ct,
object_id=question.pk,
author=request.user,
status=status,
comment=comment,
)
return render(request, "generic/partials/question_review_block.html", {"review": review, "question": question, "app_name": self.app_name})
# Default: render the block (latest if exists, otherwise form)
if latest:
return render(request, "generic/partials/question_review_block.html", {"review": latest, "question": question, "app_name": self.app_name})
else:
return render(request, "generic/partials/question_review_form.html", {"question": question, "latest": latest, "app_name": self.app_name})
@method_decorator(user_passes_test(lambda u: u.is_superuser))
def user_answer_delete_multiple(self, request):
print(request.POST["answer_ids"])
@@ -2999,6 +3050,7 @@ class GenericViewBase:
"view_feedback": view_feedback,
"remote_url": settings.REMOTE_URL,
"can_edit": question.can_edit(request.user),
"app_name": self.app_name,
},
)