147 lines
4.8 KiB
Python
Executable File
147 lines
4.8 KiB
Python
Executable File
from django.forms import (
|
|
Form,
|
|
ModelForm,
|
|
ModelMultipleChoiceField,
|
|
ModelChoiceField,
|
|
ChoiceField,
|
|
CharField,
|
|
HiddenInput,
|
|
)
|
|
from django.forms import inlineformset_factory
|
|
|
|
from generic.models import CidUser, Examination, QuestionNote
|
|
|
|
from django.contrib.admin.widgets import FilteredSelectMultiple
|
|
from django.forms.widgets import RadioSelect, TextInput, Textarea
|
|
|
|
from rapids.models import Rapid
|
|
from anatomy.models import AnatomyQuestion
|
|
from longs.models import Long
|
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
|
|
from physics.models import CidUserAnswer as PhysicsCidUserAnswer, Question
|
|
from physics.models import Exam as PhysicsExam
|
|
from physics.models import Question as SbasQuestion
|
|
|
|
from anatomy.models import CidUserAnswer as AnatomyCidUserAnswer
|
|
from anatomy.models import Exam as AnatomyExam
|
|
from anatomy.models import AnatomyQuestion
|
|
from anatomy.models import Answer as AnatomyAnswer
|
|
|
|
from rapids.models import CidUserAnswer as RapidsCidUserAnswer
|
|
from rapids.models import Exam as RapidsExam
|
|
from rapids.models import Rapid
|
|
from rapids.models import Answer as RapidAnswer
|
|
|
|
from longs.models import CidUserAnswer as LongsCidUserAnswer
|
|
from longs.models import Exam as LongsExam
|
|
from longs.models import Long
|
|
|
|
from sbas.models import CidUserAnswer as SbasCidUserAnswer
|
|
from sbas.models import Exam as SbasExam
|
|
from sbas.models import Question as SbasQuestion
|
|
|
|
class ExaminationForm(ModelForm):
|
|
class Meta:
|
|
model = Examination
|
|
fields = ["examination"]
|
|
|
|
|
|
class QuestionNoteForm(ModelForm):
|
|
class Meta:
|
|
model = QuestionNote
|
|
fields = ["content_type", "object_id", "author", "note_type", "note"]
|
|
widgets = {"content_type": HiddenInput(), "object_id": HiddenInput()}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
question_type = kwargs.pop("question_type")
|
|
pk = kwargs.pop("pk")
|
|
user = kwargs.pop("user")
|
|
|
|
# if question_type == "rapid":
|
|
# question = Rapid
|
|
# content_type = ContentType.objects.get(model="rapid")
|
|
# elif question_type == "anatomy":
|
|
# question = AnatomyQuestion
|
|
# content_type = ContentType.objects.get(model="anatomyquestion")
|
|
# elif question_type == "long":
|
|
# question = Long
|
|
# content_type = ContentType.objects.get(model="long")
|
|
# else:
|
|
# raise PermissionError()
|
|
|
|
# get_object_or_404(question, pk=pk)
|
|
# We get the 'initial' keyword argument or initialize it
|
|
# as a dict if it didn't exist.
|
|
# if kwargs.get("instance"):
|
|
ModelForm.__init__(self, *args, **kwargs)
|
|
super(QuestionNoteForm, self).__init__(*args, **kwargs)
|
|
|
|
# initial = kwargs.setdefault("initial", {})
|
|
## The widget for a ModelMultipleChoiceField expects
|
|
## a list of primary key for the selected data.
|
|
##initial["content_type"] = question
|
|
##initial["content_id"] = pk
|
|
# self.fields["content_type"].initial = content_type
|
|
# self.fields["object_id"].initial = pk
|
|
|
|
|
|
class CidUserForm(ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
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["physics_exams"] = [t.pk for t in kwargs["instance"].physics_exams.all()]
|
|
|
|
ModelForm.__init__(self, *args, **kwargs)
|
|
|
|
super(CidUserForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields["physics_exams"] = ModelMultipleChoiceField(
|
|
required=False,
|
|
queryset=PhysicsExam.objects.filter(archive=False),
|
|
widget=FilteredSelectMultiple(verbose_name="Physics Exams", is_stacked=False),
|
|
)
|
|
|
|
def save(self, commit=True):
|
|
# Get the unsaved Long 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()
|
|
instance.physics_exam.clear()
|
|
for physics_exam in self.cleaned_data["physics_exam"]:
|
|
# We need the id here
|
|
instance.physics_exam.add(physics_exam.pk)
|
|
|
|
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 = CidUser
|
|
fields = [
|
|
"passcode",
|
|
"active",
|
|
"internal_candidate",
|
|
"name",
|
|
"email",
|
|
"supervisor_email",
|
|
]
|