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