class documentation

Form for querying the database.

It is supposed to be called with the GET or POST data to be cleaned as the first argument. The user argument is necessary to determine which datasets the user may or may not have access to. If the user isn't logged in, the form will use the update_dataset_options method to only show public datasets.

Via add_subsite_choice_fields and add_lnl_toggle_buttons, the form is dynamically extended with the buttons and selection for subsites and LNLs. Beyond that, all fields mirror the fields in the Statistics class in the query module pretty closely.

The form class also has the classmethod from_initial, which creates a bound form from the initial field data. This is useful as the form isn't used like a typical Django form that is rendered empty and unbound, waiting for user input. Instead, the form is always bound (either with the initial data or with the user's input), validated, and only then rendered.

Class Method from_initial Return a bound form filled with the default values for each field.
Static Method generate_icd_codes Generate all subsite ICD codes from the cleaned_data.
Method __init__ Extend default initialization.
Method add_lnl_toggle_buttons Add all LNL toggle buttons to the form.
Method add_subsite_choice_fields Add all Subsites choice fields to the form.
Method check_lnl_conflicts Ensure that LNLs I, II, and V are not in conflict with their sublevels.
Method clean Clean the form data.
Method get_subsite_fields Return the names of the subsite checkboxes.
Method update_dataset_options Update the DatasetModel choices based on the user's permissions.
Class Variable central Filter by whether the tumor is central or not.
Class Variable datasets Patients from which datasets to include in the query.
Class Variable hpv Select patients that are HPV positive, negative, or unknown.
Class Variable is_n_plus Select patients with N+ or N0 status.
Class Variable midext Filter by whether the tumor crosses the mid-sagittal plane.
Class Variable modalities Which modalities to combine into a consensus diagnosis.
Class Variable modality_combine Method to use to combine the modalities' LNL involvement diagnoses.
Class Variable show_percent Show the statistics after querying as percentages or absolute numbers.
Class Variable smoke Select patients that are smokers, non-smokers, or unknown.
Class Variable surgery Did the patient undergo neck dissection?
Class Variable t_stage Only include patients with the selected T-stages.
@classmethod
def from_initial(cls: type[T], user) -> T:

Return a bound form filled with the default values for each field.

This uses the form_from_initial helper function to create the form with the initial data. This helper function is also used at other places in the code to create forms filled with initial data.

@staticmethod
def generate_icd_codes(cleaned_data: dict[str, Any]) -> Generator[str, None, None]:

Generate all subsite ICD codes from the cleaned_data.

In the cleaned_data up to this point, the ICD codes are still grouped by Subsites. E.g., there would be a key subsite_larynx in the cleaned_data with a list of selected ICD codes as value. This method extracts and concatenates all these lists of selected ICD codes into a single generator of selected ICD codes.

def __init__(self, *args, user, **kwargs):

Extend default initialization.

After calling the parent constructor, the selectable datasets are populated based on the user's permissions. I.e., a logged-in user can see private datasets, while an anonymous user can only see public datasets. This is handled by the update_dataset_options method.

Then, the defined Subsites as separate EasySubsiteChoiceField fields are added to the form. We do this dynamically via the add_subsite_choice_fields method, because we want to render the subsites separately in the frontend.

Also, the LNL toggle buttons are added to the form dynamically, because it would be cumbersome to add them manually for each LNL and side.

Note that the form contains the user input or initial values already upon initialization. But it is OK to add fields even after that, because the form only goes through its fields and tries to validate its data for each of then when form.is_valid() is called.

def add_lnl_toggle_buttons(self):

Add all LNL toggle buttons to the form.

def add_subsite_choice_fields(self):

Add all Subsites choice fields to the form.

def check_lnl_conflicts(self, cleaned_data: dict[str, Any]) -> dict[str, Any]:

Ensure that LNLs I, II, and V are not in conflict with their sublevels.

This is already handled in the frontend using JavaScript, but it is technically possible to e.g. provide a GET request with the URL parameters that would still violate the sub- and superlevel constraints.

One example would be that the superlevel II is selected to be healthy, but sublevel IIa is selected to be involved. That's a violation, because by definition, superlevel II is involved as soon as one of the sublevels harbors disease.

def clean(self) -> dict[str, Any]:

Clean the form data.

The default cleaning provided by Django is extended to deal with some special cases that arise from our data. On the one hand, this involved casting the t_stage list of values to integers, but more importantly, it also ensures that the sub- and superlevel involvement of the LNLs I, II, and V are not in conflict.

Further, it extracts a list of ICD codes from the subsite checkboxes via the generate_icd_codes method.

def get_subsite_fields(self) -> list[str]:

Return the names of the subsite checkboxes.

def update_dataset_options(self, user):

Update the DatasetModel choices based on the user's permissions.

central =

Filter by whether the tumor is central or not.

datasets =

Patients from which datasets to include in the query.

hpv =

Select patients that are HPV positive, negative, or unknown.

is_n_plus =

Select patients with N+ or N0 status.

midext =

Filter by whether the tumor crosses the mid-sagittal plane.

modalities =

Which modalities to combine into a consensus diagnosis.

modality_combine =

Method to use to combine the modalities' LNL involvement diagnoses.

show_percent =

Show the statistics after querying as percentages or absolute numbers.

smoke =

Select patients that are smokers, non-smokers, or unknown.

surgery =

Did the patient undergo neck dissection?

t_stage =

Only include patients with the selected T-stages.