196 lines
4.9 KiB
Python
196 lines
4.9 KiB
Python
from django.forms import (
|
|
Form,
|
|
ModelForm,
|
|
ModelMultipleChoiceField,
|
|
ModelChoiceField,
|
|
ChoiceField,
|
|
CharField,
|
|
)
|
|
from django.forms import inlineformset_factory
|
|
|
|
from .models import (
|
|
Answer,
|
|
CidUserAnswer,
|
|
AnatomyQuestion,
|
|
Examination,
|
|
BodyPart,
|
|
Structure,
|
|
Region,
|
|
Modality,
|
|
QuestionType,
|
|
Exam,
|
|
)
|
|
|
|
from django.contrib.admin.widgets import FilteredSelectMultiple
|
|
from django.forms.widgets import RadioSelect, TextInput, Textarea
|
|
|
|
|
|
class AnatomyAnswerForm(ModelForm):
|
|
class Meta:
|
|
model = CidUserAnswer
|
|
fields = ("answer",)
|
|
|
|
|
|
class MarkAnatomyQuestionForm(Form):
|
|
# correct = forms.CharField(required=False)
|
|
# half_correct = forms.CharField(required=False)
|
|
# incorrect = forms.CharField(required=False)
|
|
marked_answers = CharField(required=False)
|
|
|
|
|
|
class AnatomyQuestionForm(ModelForm):
|
|
|
|
exams = ModelMultipleChoiceField(queryset=Exam.objects.all())
|
|
|
|
|
|
class Media:
|
|
# Django also includes a few javascript files necessary
|
|
# for the operation of this form element. You need to
|
|
# include <script src="/admin/jsi18n"></script>
|
|
# in the template.
|
|
css = {
|
|
"all": ["css/widgets.css"],
|
|
}
|
|
# Adding this javascript is crucial
|
|
js = ["jsi18n.js", "tesseract.min.js"]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(AnatomyQuestionForm, self).__init__(*args, **kwargs)
|
|
# self.fields['question'].widget.attrs = {'class': 'question-form', 'rows': 10, 'cols': 100}
|
|
# self.fields['feedback'].widget.attrs = {'class': 'feedback-form', 'rows': 10, 'cols': 100}
|
|
self.fields["question_type"] = ModelChoiceField(
|
|
required=True,
|
|
queryset=QuestionType.objects.all(),
|
|
initial=1,
|
|
)
|
|
|
|
self.fields["region"] = ModelChoiceField(
|
|
required=False,
|
|
queryset=Region.objects.all(),
|
|
)
|
|
|
|
self.fields["modality"] = ModelChoiceField(
|
|
required=True,
|
|
queryset=Modality.objects.all(),
|
|
)
|
|
|
|
self.fields["structure"] = ModelChoiceField(
|
|
required=False,
|
|
queryset=Structure.objects.all(),
|
|
)
|
|
|
|
self.fields["examination"] = ModelChoiceField(
|
|
required=False,
|
|
queryset=Examination.objects.all(),
|
|
)
|
|
|
|
self.fields["body_part"] = ModelChoiceField(
|
|
required=False,
|
|
queryset=BodyPart.objects.all(),
|
|
)
|
|
|
|
#self.fields["exams"] = ModelChoiceField(
|
|
# required=False,
|
|
# queryset=Exam.objects.all(),
|
|
#)
|
|
|
|
|
|
if kwargs.get('instance'):
|
|
# We get the 'initial' keyword argument or initialize it
|
|
# as a dict if it didn't exist.
|
|
initial = kwargs.setdefault('initial', {})
|
|
# The widget for a ModelMultipleChoiceField expects
|
|
# a list of primary key for the selected data.
|
|
initial['exams'] = [t.pk for t in
|
|
kwargs['instance'].exams.all()]
|
|
|
|
ModelForm.__init__(self, *args, **kwargs)
|
|
|
|
def save(self, commit=True):
|
|
# Get the unsaved Pizza instance
|
|
instance = ModelForm.save(self, False)
|
|
|
|
# Prepare a 'save_m2m' method for the form,
|
|
old_save_m2m = self.save_m2m
|
|
|
|
def save_m2m():
|
|
old_save_m2m()
|
|
# This is where we actually link the pizza with toppings
|
|
instance.exams.clear()
|
|
for exam in self.cleaned_data['exams']:
|
|
instance.exams.add(exam)
|
|
|
|
self.save_m2m = save_m2m
|
|
|
|
# Do we need to save all changes now?
|
|
#if commit:
|
|
instance.save()
|
|
self.save_m2m()
|
|
|
|
return instance
|
|
|
|
class Meta:
|
|
model = AnatomyQuestion
|
|
|
|
fields = [
|
|
"question_type",
|
|
"image",
|
|
"region",
|
|
"modality",
|
|
"structure",
|
|
"examination",
|
|
"body_part",
|
|
"description",
|
|
]
|
|
|
|
widgets = {}
|
|
|
|
|
|
AnswerFormSet = inlineformset_factory(
|
|
AnatomyQuestion,
|
|
Answer,
|
|
fields=["answer", "status"],
|
|
widgets={
|
|
"answer": Textarea(
|
|
attrs={"required": "true", "minlength": 2, "maxlength": 500, 'rows': 3}
|
|
)
|
|
},
|
|
exclude=[],
|
|
can_delete=True,
|
|
extra=1,
|
|
max_num=10,
|
|
field_classes="testing",
|
|
)
|
|
|
|
AnswerUpdateFormSet = inlineformset_factory(
|
|
AnatomyQuestion,
|
|
Answer,
|
|
fields=["answer", "status"],
|
|
widgets={
|
|
"answer": Textarea(
|
|
attrs={"required": "true", "minlength": 2, "maxlength": 500, 'rows': 3}
|
|
)
|
|
},
|
|
exclude=[],
|
|
can_delete=True,
|
|
extra=0,
|
|
max_num=10,
|
|
field_classes="testing",
|
|
)
|
|
|
|
class ExaminationForm(ModelForm):
|
|
class Meta:
|
|
model = Examination
|
|
fields = ['examination']
|
|
|
|
|
|
class StructureForm(ModelForm):
|
|
class Meta:
|
|
model = Structure
|
|
fields = ['structure']
|
|
|
|
|
|
class BodyPartForm(ModelForm):
|
|
class Meta:
|
|
model = BodyPart
|
|
fields = ['bodypart'] |