Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ERP-2589] [ERP-2651] Working Group Types and the Unallocated Working Group #713

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading