Add FRCR toggle functionality to question detail view
This commit is contained in:
+29
-1
@@ -24,7 +24,7 @@ from django.core.cache import cache
|
||||
|
||||
from django.urls import reverse_lazy, reverse
|
||||
|
||||
from django.http import Http404, HttpResponseBadRequest, JsonResponse
|
||||
from django.http import Http404, HttpResponseBadRequest, JsonResponse, HttpResponseForbidden
|
||||
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
|
||||
@@ -656,6 +656,34 @@ def question_overview(request):
|
||||
return render(request, "sbas/question_overview.html", context)
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(["POST", "GET"])
|
||||
def toggle_frcr_appropriate(request, pk):
|
||||
"""Toggle or set the Question.frcr_appropriate boolean via HTMX.
|
||||
|
||||
Accepts POST requests. If POST contains 'frcr' parameter its truthiness
|
||||
will be applied; otherwise the value is toggled.
|
||||
|
||||
Returns the rendered partial `_frcr_toggle.html` so HTMX can swap it in.
|
||||
"""
|
||||
question = get_object_or_404(Question, pk=pk)
|
||||
|
||||
# Simple permission: allow staff or users with change permission
|
||||
if not (request.user.is_staff or request.user.has_perm('sbas.change_question')):
|
||||
return HttpResponseForbidden("Forbidden")
|
||||
|
||||
# Toggle or set
|
||||
if request.method == "POST":
|
||||
val = request.POST.get("frcr")
|
||||
if val is None:
|
||||
question.frcr_appropriate = not question.frcr_appropriate
|
||||
else:
|
||||
question.frcr_appropriate = str(val).lower() in ("1", "true", "yes", "on")
|
||||
question.save()
|
||||
|
||||
return render(request, "sbas/_frcr_toggle.html", {"question": question})
|
||||
|
||||
|
||||
class UserAnswerTableView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
||||
model = UserAnswer
|
||||
table_class = UserAnswerTable
|
||||
|
||||
Reference in New Issue
Block a user