Fix: unknown field choice
This commit is contained in:
parent
27d24a9b28
commit
73cfe06c58
@ -1,13 +1,14 @@
|
||||
from django.apps import AppConfig
|
||||
from django.db.models.signals import post_save
|
||||
from .models import User
|
||||
from .signals import post_save_account_receiver
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
name = "accounts"
|
||||
|
||||
def ready(self) -> None:
|
||||
from django.db.models.signals import post_save
|
||||
from .models import User
|
||||
from .signals import post_save_account_receiver
|
||||
|
||||
post_save.connect(post_save_account_receiver, sender=User)
|
||||
|
||||
return super().ready()
|
||||
|
||||
@ -5,7 +5,7 @@ from .utils import (
|
||||
)
|
||||
|
||||
|
||||
def post_save_account_receiver(instance=None, created=False):
|
||||
def post_save_account_receiver(instance=None, created=False, *args, **kwargs):
|
||||
"""
|
||||
Send email notification
|
||||
"""
|
||||
|
||||
@ -42,9 +42,9 @@ class QuizAddForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(QuizAddForm, self).__init__(*args, **kwargs)
|
||||
if self.instance.pk:
|
||||
self.fields[
|
||||
"questions"
|
||||
].initial = self.instance.question_set.all().select_subclasses()
|
||||
self.fields["questions"].initial = (
|
||||
self.instance.question_set.all().select_subclasses()
|
||||
)
|
||||
|
||||
def save(self, commit=True):
|
||||
quiz = super(QuizAddForm, self).save(commit=False)
|
||||
@ -59,6 +59,7 @@ class MCQuestionForm(forms.ModelForm):
|
||||
model = MCQuestion
|
||||
exclude = ()
|
||||
|
||||
|
||||
class MCQuestionFormSet(forms.BaseInlineFormSet):
|
||||
def clean(self):
|
||||
"""
|
||||
@ -69,10 +70,14 @@ class MCQuestionFormSet(forms.BaseInlineFormSet):
|
||||
super().clean()
|
||||
|
||||
# Collect non-deleted forms
|
||||
valid_forms = [form for form in self.forms if not form.cleaned_data.get('DELETE', True)]
|
||||
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)):
|
||||
valid_choices = [
|
||||
"choice_text" 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
|
||||
@ -80,7 +85,9 @@ class MCQuestionFormSet(forms.BaseInlineFormSet):
|
||||
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]
|
||||
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.")
|
||||
@ -94,7 +101,7 @@ MCQuestionFormSet = inlineformset_factory(
|
||||
Choice,
|
||||
form=MCQuestionForm,
|
||||
formset=MCQuestionFormSet,
|
||||
fields=["choice", "correct"],
|
||||
fields=["choice_text", "correct"],
|
||||
can_delete=True,
|
||||
extra=5,
|
||||
)
|
||||
|
||||
@ -13,6 +13,7 @@ from django.db.models.signals import pre_save
|
||||
from django.urls import reverse
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.dispatch import receiver
|
||||
from model_utils.managers import InheritanceManager
|
||||
|
||||
from course.models import Course
|
||||
|
||||
@ -1,21 +1,31 @@
|
||||
from modeltranslation.translator import register, TranslationOptions
|
||||
from .models import Quiz, Question, Choice, MCQuestion
|
||||
|
||||
|
||||
@register(Quiz)
|
||||
class QuizTranslationOptions(TranslationOptions):
|
||||
fields = ('title', 'description',)
|
||||
fields = (
|
||||
"title",
|
||||
"description",
|
||||
)
|
||||
empty_values = None
|
||||
|
||||
|
||||
@register(Question)
|
||||
class QuestionTranslationOptions(TranslationOptions):
|
||||
fields = ('content', 'explanation',)
|
||||
fields = (
|
||||
"content",
|
||||
"explanation",
|
||||
)
|
||||
empty_values = None
|
||||
|
||||
|
||||
@register(Choice)
|
||||
class ChoiceTranslationOptions(TranslationOptions):
|
||||
fields = ('choice',)
|
||||
fields = ("choice_text",)
|
||||
empty_values = None
|
||||
|
||||
|
||||
@register(MCQuestion)
|
||||
class MCQuestionTranslationOptions(TranslationOptions):
|
||||
pass
|
||||
Loading…
x
Reference in New Issue
Block a user