From a7826c003b6f231be4cf76e09758c3b7f87d8069 Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 4 May 2021 23:27:35 +0100 Subject: [PATCH] . --- generic/forms.py | 30 +++++++++++++++++++++++++++++- rad/urls.py | 4 ++-- rad/views.py | 8 +++++++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/generic/forms.py b/generic/forms.py index 987544dc..8e0eb68c 100755 --- a/generic/forms.py +++ b/generic/forms.py @@ -15,6 +15,11 @@ from generic.models import ( 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 class ExaminationForm(ModelForm): class Meta: @@ -24,4 +29,27 @@ class ExaminationForm(ModelForm): class QuestionNoteForm(ModelForm): class Meta: model = QuestionNote - fields = ["content_type", "object_id", "author", "note_type", "note"] \ No newline at end of file + fields = ["content_type", "object_id", "author", "note_type", "note"] + + def __init__(self, *args, **kwargs): + question_type = kwargs.pop("question_type") + + if question_type == "rapid": + question = Rapid + elif question_type == "anatomy": + question = AnatomyQuestion + elif question_type == "long": + question = AnatomyQuestion + else: + raise PermissionError() + + get_object_or_404(question, pk=kwargs.pop("pk")) + # 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["content_type"] = [t.pk for t in kwargs["instance"].exams.all()] + + ModelForm.__init__(self, *args, **kwargs) + #super(QuestionNoteForm(), self).__init__(*args, **kwargs) \ No newline at end of file diff --git a/rad/urls.py b/rad/urls.py index f7240a06..2ffa4ac4 100644 --- a/rad/urls.py +++ b/rad/urls.py @@ -55,8 +55,8 @@ urlpatterns = [ path("exam/json/unbased", views.active_exams_unbased, name="active_exams_unbased"), path("exam/submit", views.exam_submit, name="global_exam_answers_submit"), #path('', include('generic.urls')), - path("feedback//", views.feedback_create, name="feedback_create"), - path("feedback/", views.AddQuestionNote.as_view(), name="feedback"), + path("feedback//", views.AddQuestionNote.as_view(), name="feedback_create"), + #path("feedback/", views.AddQuestionNote.as_view(), name="feedback"), path('api/', include(router.urls)), diff --git a/rad/views.py b/rad/views.py index 0f67d82b..14080faa 100644 --- a/rad/views.py +++ b/rad/views.py @@ -160,4 +160,10 @@ def feedback_create(request, question_type, pk): class AddQuestionNote(LoginRequiredMixin, CreateView): model = QuestionNote - form_class = QuestionNoteForm \ No newline at end of file + form_class = QuestionNoteForm + + def get_form_kwargs(self): + kwargs = super(AddQuestionNote, self).get_form_kwargs() + # update the kwargs for the form init method with yours + kwargs.update(self.kwargs) # self.kwargs contains all url conf params + return kwargs \ No newline at end of file