maintain question order when editing
This commit is contained in:
+16
-1
@@ -719,7 +719,7 @@ def exam_scores_cid(request, pk):
|
||||
fig = px.histogram(
|
||||
df,
|
||||
x=0,
|
||||
title="Distribution of scores",
|
||||
title="{}: distribution of scores".format(exam),
|
||||
labels={"0": "Score"},
|
||||
height=400,
|
||||
width=600,
|
||||
@@ -1021,9 +1021,16 @@ class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
# save exam orders (there must be a better way to do this)
|
||||
exam_orders = {}
|
||||
for exam in self.object.exams.all():
|
||||
exam_orders[exam] = list(exam.exam_questions.all())
|
||||
print(exam_orders[exam])
|
||||
|
||||
self.object = form.save(commit=False)
|
||||
self.object.save()
|
||||
|
||||
|
||||
form.instance.author.add(self.request.user.id)
|
||||
|
||||
context = self.get_context_data(form=form)
|
||||
@@ -1033,6 +1040,14 @@ class AnatomyQuestionUpdate(LoginRequiredMixin, UpdateView):
|
||||
response = super().form_valid(form)
|
||||
formset.instance = self.object
|
||||
formset.save()
|
||||
|
||||
# restore exam orders
|
||||
for exam in self.object.exams.all():
|
||||
if exam in exam_orders and self.object in exam_orders[exam]:
|
||||
print(exam_orders[exam])
|
||||
exam.exam_questions.set(exam_orders[exam])
|
||||
exam.save()
|
||||
|
||||
return response
|
||||
else:
|
||||
return super().form_invalid(form)
|
||||
|
||||
+1
-1
@@ -186,7 +186,7 @@ def exam_scores_cid(request, pk):
|
||||
fig = px.histogram(
|
||||
df,
|
||||
x=0,
|
||||
title="Distribution of scores",
|
||||
title="{}: distribution of scores".format(exam),
|
||||
labels={"0": "Score"},
|
||||
height=400,
|
||||
width=600,
|
||||
|
||||
Reference in New Issue
Block a user