Coverage for crateweb/nlp_classification/forms.py: 97%
60 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/crateweb/nlp_classification/forms.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26CRATE NLP classification forms.
28"""
30from typing import Any
32from django.forms import (
33 CharField,
34 ChoiceField,
35 Form,
36 HiddenInput,
37 ModelForm,
38 ModelChoiceField,
39 ModelMultipleChoiceField,
40 RadioSelect,
41)
43from crate_anon.crateweb.core.constants import (
44 NLP_DB_CONNECTION_NAME,
45 RESEARCH_DB_CONNECTION_NAME,
46)
47from crate_anon.crateweb.nlp_classification.models import (
48 Assignment,
49 Option,
50 Question,
51 SampleSpec,
52 TableDefinition,
53 Task,
54 UserAnswer,
55)
58# Standard create/edit forms in alphabetical order
59class AssignmentForm(ModelForm):
60 class Meta:
61 model = Assignment
62 fields = ["task", "sample_spec", "user"]
65class OptionForm(ModelForm):
66 class Meta:
67 model = Option
68 fields = ["description"]
71class QuestionForm(ModelForm):
72 class Meta:
73 model = Question
74 fields = ["title", "task", "options"]
77class SampleSpecForm(ModelForm):
78 class Meta:
79 model = SampleSpec
80 fields = [
81 "source_column",
82 "nlp_table_definition",
83 "search_term",
84 "size",
85 "seed",
86 ]
89class TableDefinitionForm(ModelForm):
90 class Meta:
91 model = TableDefinition
92 fields = ["db_connection_name", "table_name", "pk_column_name"]
94 db_connection_name = ChoiceField(
95 choices=[
96 (NLP_DB_CONNECTION_NAME, "Research database"),
97 (RESEARCH_DB_CONNECTION_NAME, "NLP database"),
98 ]
99 )
102class TaskForm(ModelForm):
103 class Meta:
104 model = Task
105 fields = ["name"]
108class UserAnswerForm(ModelForm):
109 class Meta:
110 model = UserAnswer
111 fields = ["decision"]
113 def __init__(self, *args: Any, **kwargs: Any) -> None:
114 super().__init__(*args, **kwargs)
116 self.fields["decision"] = ModelChoiceField(
117 queryset=Option.objects.filter(question=self.instance.question),
118 widget=RadioSelect,
119 )
122# TaskAndQuestionWizardView forms in order of step
123class WizardSelectTaskForm(Form):
124 task = ModelChoiceField(
125 queryset=Task.objects.all(),
126 required=False,
127 empty_label="-- Create new task --",
128 )
131class WizardCreateTaskForm(TaskForm):
132 pass
135class WizardSelectQuestionForm(Form):
136 question = ModelChoiceField(
137 queryset=Question.objects.all(),
138 required=False,
139 empty_label="-- Create new question --",
140 )
142 def __init__(self, *args: Any, task=None, **kwargs: Any) -> None:
143 super().__init__(*args, **kwargs)
145 if task is not None:
146 self.fields["question"].queryset = Question.objects.filter(
147 task=task
148 )
151class WizardCreateQuestionForm(ModelForm):
152 class Meta:
153 model = Question
154 fields = ["title", "task"]
156 task = ModelChoiceField(
157 queryset=Task.objects.all(), widget=HiddenInput, required=False
158 )
161class WizardSelectOptionsForm(Form):
162 options = ModelMultipleChoiceField(
163 queryset=Option.objects.all(),
164 required=False,
165 )
168class WizardCreateOptionsForm(Form):
169 description_1 = CharField(required=False)
170 description_2 = CharField(required=False)
173# SampleDataWizardView forms in order of step
174class WizardSelectSourceTableDefinitionForm(Form):
175 table_definition = ModelChoiceField(
176 queryset=TableDefinition.objects.filter(
177 db_connection_name=RESEARCH_DB_CONNECTION_NAME
178 ),
179 required=False,
180 empty_label="-- Create new source table definition --",
181 )
184class WizardSelectSourceTableForm(Form):
185 table_name = ChoiceField()