Skip to content

Commit

Permalink
Merge pull request #713 from eresearchqut/unallocated_working_group
Browse files Browse the repository at this point in the history
[ERP-2589] [ERP-2651] Working Group Types and the Unallocated Working Group
  • Loading branch information
ppettitau authored Oct 25, 2024
2 parents e4464d1 + 4e40e50 commit 4f854ff
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions rdrf/registry/patients/admin_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ def clinician_display_str(obj):
wgs = ", ".join([wg.name for wg in obj.working_groups.all()])
return f"{title} {full_name} ({wgs})"

def has_displayable_working_groups(choices):
if not choices:
return False

if len(choices) > 1:
return True

working_group_id, *_ = choices[0]
return (
working_group_id
!= WorkingGroup.objects.get_unallocated(
registry=self.registry_model
).id
)

instance = None

if "registry_model" in kwargs:
Expand Down Expand Up @@ -517,7 +532,7 @@ def clinician_display_str(obj):
)
self.fields.update(additional_working_group_fields)
self.fields["working_groups"].choices = working_groups_choices
if not working_groups_choices:
if not has_displayable_working_groups(working_groups_choices):
self.fields["working_groups"].disabled = True
self.fields["working_groups"].required = False
self.fields[
Expand Down Expand Up @@ -551,6 +566,7 @@ def apply_field_config(target_field, target_field_config):
target_field_config.status
== DemographicFields.HIDDEN
):
self.fields[target_field].required = False
self.fields[
target_field
].widget = forms.MultipleHiddenInput()
Expand Down Expand Up @@ -735,31 +751,56 @@ def clean_rdrf_registry(self):
return registries

def clean_working_groups(self):
is_disabled = (
is_base_working_groups_disabled = (
"disabled" in self.fields["working_groups"].widget.attrs
or self.fields["working_groups"].disabled
)
empty_choices = not self.fields["working_groups"].choices

if is_disabled:
if empty_choices:
working_groups = WorkingGroup.objects.none()
else:
working_groups = self.instance.working_groups.all()
else:
working_groups = self.cleaned_data["working_groups"]
base_working_group_choices = self.fields["working_groups"].choices
selected_working_group_ids = self.data.getlist("working_groups")

field_names = [
key for key in self.data.keys() if key.startswith("working_groups_")
additional_working_group_fields = [
field_name
for field_name in self.data.keys()
if field_name.startswith("working_groups_")
]
field_values = [
selected_additional_working_group_ids = [
value
for field_name in field_names
for field_name in additional_working_group_fields
for value in self.data.getlist(field_name)
]

# Determine the base working groups the patient should have
if is_base_working_groups_disabled:
if not base_working_group_choices:
working_groups = WorkingGroup.objects.none()
else:
unallocated_working_group = (
WorkingGroup.objects.get_unallocated(self.registry_model)
)
if selected_additional_working_group_ids:
# The patient has been assigned to additional working groups,
# and they can't otherwise select from the base working groups, so remove them from "Unallocated"
working_groups = WorkingGroup.objects.filter(
id__in=selected_working_group_ids
).exclude(id=unallocated_working_group.id)
elif self.instance.working_groups.exists():
# No working groups have been selected, (assume all working groups controls are disabled)
# so keep existing working groups
working_groups = self.instance.working_groups.all()
else:
# The patient has no allocated working groups, set to "Unallocated"
working_groups = WorkingGroup.objects.filter(
id=unallocated_working_group.id
)
else:
working_groups = WorkingGroup.objects.filter(
id__in=selected_working_group_ids
)

all_selected_working_groups = working_groups.union(
WorkingGroup.objects.filter(id__in=field_values)
WorkingGroup.objects.filter(
id__in=selected_additional_working_group_ids
)
)
if not all_selected_working_groups:
raise forms.ValidationError(
Expand Down

0 comments on commit 4f854ff

Please sign in to comment.