Refactor exam_user_bulk_edit method to support both generic and app-specific exam IDs for improved flexibility
This commit is contained in:
+8
-5
@@ -2805,20 +2805,23 @@ class ExamViews(View, LoginRequiredMixin):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
@method_decorator(login_required)
|
@method_decorator(login_required)
|
||||||
def exam_user_bulk_edit(self, request, pk):
|
def exam_user_bulk_edit(self, request, pk=None, exam_id=None):
|
||||||
"""Bulk add/remove User objects to/from an exam. Expects POST with 'bulk_pks' (JSON list)
|
"""Bulk add/remove User objects to/from an exam. Expects POST with 'bulk_pks' (JSON list)
|
||||||
and 'add' ('true'/'false'). Returns a rendered user list partial for HTMX swaps.
|
and 'add' ('true'/'false'). Returns a rendered user list partial for HTMX swaps.
|
||||||
"""
|
"""
|
||||||
|
# Support both `pk` (generic urls) and `exam_id` (app-specific urls like atlas)
|
||||||
|
exam_pk = pk if pk is not None else exam_id
|
||||||
|
|
||||||
if request.method != 'POST':
|
if request.method != 'POST':
|
||||||
return JsonResponse({'status': 'error, invalid method'}, status=400)
|
return JsonResponse({'status': 'error, invalid method'}, status=400)
|
||||||
|
|
||||||
if request.user.groups.filter(name="cid_user_manager").exists():
|
if request.user.groups.filter(name="cid_user_manager").exists():
|
||||||
pass
|
pass
|
||||||
elif not self.check_user_edit_access(request.user, exam_id=pk):
|
elif not self.check_user_edit_access(request.user, exam_id=exam_pk):
|
||||||
data = {"status": "invalid permisions"}
|
data = {"status": "invalid permisions"}
|
||||||
return JsonResponse(data, status=403)
|
return JsonResponse(data, status=403)
|
||||||
|
|
||||||
exam = get_object_or_404(self.Exam, pk=pk)
|
exam = get_object_or_404(self.Exam, pk=exam_pk)
|
||||||
|
|
||||||
bulk_pks_raw = request.POST.get('bulk_pks') or request.POST.get('bulk_pks[]')
|
bulk_pks_raw = request.POST.get('bulk_pks') or request.POST.get('bulk_pks[]')
|
||||||
items = []
|
items = []
|
||||||
@@ -2867,9 +2870,9 @@ class ExamViews(View, LoginRequiredMixin):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if this_add:
|
if this_add:
|
||||||
app_exam_map[self.app_name].add(pk)
|
app_exam_map[self.app_name].add(exam_pk)
|
||||||
else:
|
else:
|
||||||
app_exam_map[self.app_name].remove(pk)
|
app_exam_map[self.app_name].remove(exam_pk)
|
||||||
changed.append(user_obj.pk)
|
changed.append(user_obj.pk)
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user