Merge branch 'sagundevkota/issue#68' of github.com:SagunDevkota/django-lms into SagunDevkota-sagundevkota/issue#68

This commit is contained in:
Adil Mohak 2024-09-29 09:21:47 +03:00
commit 633e1387ea
3 changed files with 37 additions and 5 deletions

View File

@ -59,11 +59,41 @@ class MCQuestionForm(forms.ModelForm):
model = MCQuestion
exclude = ()
class MCQuestionFormSet(forms.BaseInlineFormSet):
def clean(self):
"""
Custom validation for the formset to ensure:
1. At least two choices are provided and not marked for deletion.
2. At least one of the choices is marked as correct.
"""
super().clean()
# Collect non-deleted forms
valid_forms = [form for form in self.forms if not form.cleaned_data.get('DELETE', True)]
valid_choices = ['choice' in form.cleaned_data.keys() for form in valid_forms]
if(not all(valid_choices)):
raise forms.ValidationError("You must add a valid choice name.")
# If all forms are deleted, raise a validation error
if len(valid_forms) < 2:
raise forms.ValidationError("You must provide at least two choices.")
# Check if at least one of the valid forms is marked as correct
correct_choices = [form.cleaned_data.get('correct', False) for form in valid_forms]
if not any(correct_choices):
raise forms.ValidationError("One choice must be marked as correct.")
if correct_choices.count(True)>1:
raise forms.ValidationError("Only one choice must be marked as correct.")
MCQuestionFormSet = inlineformset_factory(
MCQuestion,
Choice,
form=MCQuestionForm,
formset=MCQuestionFormSet,
fields=["choice", "correct"],
can_delete=True,
extra=5,

View File

@ -118,10 +118,10 @@ class MCQuestionCreate(CreateView):
context = self.get_context_data()
formset = context["formset"]
course = context["course"]
with transaction.atomic():
form.instance.question = self.request.POST.get("content")
self.object = form.save()
if formset.is_valid():
if formset.is_valid():
with transaction.atomic():
form.instance.question = self.request.POST.get("content")
self.object = form.save()
formset.instance = self.object
formset.save()
if "another" in self.request.POST:
@ -131,6 +131,8 @@ class MCQuestionCreate(CreateView):
quiz_id=self.kwargs["quiz_id"],
)
return redirect("quiz_index", course.slug)
else:
return self.form_invalid(form)
return super(MCQuestionCreate, self).form_invalid(form)

View File

@ -16,7 +16,7 @@
<div class="title-1">Add questions [{{ quiz_obj|truncatechars:15 }}]</div>
<br><br>
{{ formset.non_form_errors }}
<div class="container">
<div class="info-text bg-orange mb-3">{{ quizQuestions }} question added</div>