Skip to content

Commit

Permalink
Merge pull request #517 from rcpch/perms-tests
Browse files Browse the repository at this point in the history
*based on PR #510 'Finished KPI Tests'* Finishes first round of testing
  • Loading branch information
pacharanero authored Jul 7, 2023
2 parents 1d6a782 + 3046ece commit c59f5e3
Show file tree
Hide file tree
Showing 102 changed files with 11,205 additions and 1,693 deletions.
18 changes: 14 additions & 4 deletions epilepsy12/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Epilepsy12UserAdmin(UserAdmin, SimpleHistoryAdmin):
"email",
"surname",
"role",
"organisation_employer",
"is_active",
)
list_display = (
Expand All @@ -28,7 +27,6 @@ class Epilepsy12UserAdmin(UserAdmin, SimpleHistoryAdmin):
"first_name",
"surname",
"is_active",
"twitter_handle",
"role",
"organisation_employer",
"is_superuser",
Expand All @@ -52,20 +50,30 @@ class Epilepsy12UserAdmin(UserAdmin, SimpleHistoryAdmin):
},
),
("Epilepsy12 Centre", {"fields": ("organisation_employer", "role")}),
("Contacts", {"fields": ("email", "twitter_handle")}),
("Contacts", {"fields": ("email",)}),
(
"Permissions",
{
"fields": (
"is_active",
"is_staff",
"is_rcpch_staff",
"is_rcpch_audit_team_member",
"is_superuser",
"email_confirmed",
"view_preference",
)
},
),
(
"Access",
{
"fields": (
"last_login",
"date_joined",
)
},
),
(
"Group Permissions",
{
Expand All @@ -76,7 +84,6 @@ class Epilepsy12UserAdmin(UserAdmin, SimpleHistoryAdmin):
),
},
),
("Personal", {"fields": ("bio",)}),
)
add_fieldsets = (
(
Expand All @@ -89,6 +96,7 @@ class Epilepsy12UserAdmin(UserAdmin, SimpleHistoryAdmin):
"first_name",
"surname",
"is_staff",
"is_rcpch_staff",
"is_active",
"is_rcpch_audit_team_member",
"role",
Expand All @@ -114,6 +122,8 @@ def get_form(self, request, obj=None, **kwargs):
form.base_fields["title"].disabled = True
form.base_fields["email"].disabled = True
form.base_fields["is_staff"].disabled = True
form.base_fields["is_rcpch_staff"].disabled = True
form.base_fields["is_rcpch_audit_team_member"].disabled = True
return form


Expand Down
60 changes: 35 additions & 25 deletions epilepsy12/common_view_functions/aggregate_by.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Literal

# Django imports
from django.apps import apps
from django.contrib.gis.db.models import (
Q,
F,
Expand All @@ -16,7 +17,8 @@

# E12 imports
from epilepsy12.constants import ETHNICITIES, SEX_TYPE
from ..models import Case

# from ..models import Case
from .report_queries import (
get_all_organisations,
get_all_trusts,
Expand All @@ -34,12 +36,12 @@
def cases_aggregated_by_sex(selected_organisation):
# aggregate queries on trust level cases

Case = apps.get_model("epilepsy12", "Case")

sex_long_list = [When(sex=k, then=Value(v)) for k, v in SEX_TYPE]

cases_aggregated_by_sex = (
Case.objects.filter(
organisations__OrganisationName__contains=selected_organisation
)
Case.objects.filter(organisations=selected_organisation)
.values("sex")
.annotate(sex_display=DJANGO_CASE(*sex_long_list, output_field=CharField()))
.values("sex_display")
Expand All @@ -52,34 +54,37 @@ def cases_aggregated_by_sex(selected_organisation):

def cases_aggregated_by_deprivation_score(selected_organisation):
# aggregate queries on trust level cases
Case = apps.get_model("epilepsy12", "Case")

deprivation_quintiles = ((1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (None, 6))

imd_long_list = [
When(index_of_multiple_deprivation_quintile=k, then=Value(v))
for k, v in deprivation_quintiles
]
cases_in_selected_organisation = Case.objects.filter(
organisations__OrganisationName__contains=selected_organisation
)

cases_aggregated_by_deprivation = (
Case.objects.filter(
organisations__OrganisationName__contains=selected_organisation
)
# Filter just Cases in selected org
cases_in_selected_organisation
# Get list of IMD quintiles
.values("index_of_multiple_deprivation_quintile")
# Converting 'None' to 6 in a new index_of_multiple_deprivation_quintile_display "column"
.annotate(
index_of_multiple_deprivation_quintile_display=DJANGO_CASE(
*imd_long_list, output_field=PositiveSmallIntegerField()
When(index_of_multiple_deprivation_quintile=None, then=Value(6)),
default="index_of_multiple_deprivation_quintile",
output_field=PositiveSmallIntegerField(),
)
)
# Keeps only the new column
.values("index_of_multiple_deprivation_quintile_display")
# Value count the new column
.annotate(
cases_aggregated_by_deprivation=Count(
"index_of_multiple_deprivation_quintile"
)
"index_of_multiple_deprivation_quintile_display"
),
)
.order_by("index_of_multiple_deprivation_quintile")
.order_by('index_of_multiple_deprivation_quintile_display')

)

# map quintile num to string repr

deprivation_quintile_str_map = {
1: "1st quintile",
2: "2nd quintile",
Expand All @@ -89,19 +94,23 @@ def cases_aggregated_by_deprivation_score(selected_organisation):
6: "Not known",
}

for index, q in enumerate(cases_aggregated_by_deprivation):
q[
"index_of_multiple_deprivation_quintile_display"
] = deprivation_quintile_str_map.get(
q.get("index_of_multiple_deprivation_quintile_display")
)
for aggregate in cases_aggregated_by_deprivation:
quintile = aggregate["index_of_multiple_deprivation_quintile_display"]

str_map = deprivation_quintile_str_map.get(quintile)

aggregate.update(
{"index_of_multiple_deprivation_quintile_display_str": str_map}
)

return cases_aggregated_by_deprivation


def cases_aggregated_by_ethnicity(selected_organisation):
# aggregate queries on trust level cases

Case = apps.get_model("epilepsy12", "Case")

ethnicity_long_list = [When(ethnicity=k, then=Value(v)) for k, v in ETHNICITIES]

cases_aggregated_by_ethnicity = (
Expand Down Expand Up @@ -219,6 +228,7 @@ def return_all_aggregated_kpis_for_cohort_and_abstraction_level_annotated_by_sub
"""
Returns aggregated KPIS for given cohort annotated by sublevel of abstraction (eg kpis in each NHS England region, labelled by region)
"""
Case = apps.get_model("epilepsy12", "Case")

if abstraction_level == "organisation":
abstraction_sublevels = get_all_organisations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

# django imports
from django.contrib.gis.db.models import Q
from django.apps import apps

# E12 imports
from epilepsy12.constants import KPI_SCORE
from epilepsy12.models import AntiEpilepsyMedicine, Episode


def score_kpi_3(registration_instance, age_at_first_paediatric_assessment) -> int:
"""3. tertiary_input
Expand All @@ -20,6 +21,9 @@ def score_kpi_3(registration_instance, age_at_first_paediatric_assessment) -> in

assessment = registration_instance.assessment

AntiEpilepsyMedicine = apps.get_model("epilepsy12", "AntiEpilepsyMedicine")
Episode = apps.get_model("epilepsy12", "Episode")

# EVALUATE ELIGIBILITY CRITERIA

# first gather relevant data
Expand Down Expand Up @@ -69,6 +73,7 @@ def score_kpi_3(registration_instance, age_at_first_paediatric_assessment) -> in
else:
return KPI_SCORE["FAIL"]


def score_kpi_3b(registration_instance) -> int:
"""3b. epilepsy_surgery_referral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

# django imports
from django.contrib.gis.db.models import Q
from django.apps import apps

# E12 imports
from epilepsy12.models import Syndrome
# from epilepsy12.models import Syndrome
from epilepsy12.constants import KPI_SCORE


Expand All @@ -20,6 +21,8 @@ def score_kpi_5(registration_instance, age_at_first_paediatric_assessment) -> in
multiaxial_diagnosis = registration_instance.multiaxialdiagnosis
investigations = registration_instance.investigations

Syndrome = apps.get_model("epilepsy12", "Syndrome")

# not scored
if (age_at_first_paediatric_assessment >= 2) and (
multiaxial_diagnosis.syndrome_present is None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# python imports

# django imports
from epilepsy12.models import AntiEpilepsyMedicine, MedicineEntity
from django.apps import apps

# E12 imports
from epilepsy12.constants import KPI_SCORE


def score_kpi_8(registration_instance, age_at_first_paediatric_assessment) -> int:
AntiEpilepsyMedicine = apps.get_model("epilepsy12", "AntiEpilepsyMedicine")
MedicineEntity = apps.get_model("epilepsy12", "MedicineEntity")

"""8. Sodium Valproate
Percentage of all females 12 years and above currently on valproate treatment with annual risk acknowledgement form completed
Expand Down
8 changes: 6 additions & 2 deletions epilepsy12/common_view_functions/calculate_kpis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# django imports
from django.contrib.gis.db.models import Sum
from django.apps import apps

# E12 imports
from ..general_functions import has_all_attributes
Expand Down Expand Up @@ -30,7 +31,8 @@
calculate_age_at_first_paediatric_assessment_in_years,
check_is_registered,
)
from epilepsy12.models import KPI

# from epilepsy12.models import KPI
from epilepsy12.constants import KPI_SCORE


Expand All @@ -45,7 +47,9 @@ def calculate_kpis(registration_instance):
2 - measure not applicable (eg ECG in nonconvulsive seizure)
None - measure not scored yet
"""


KPI = apps.get_model("epilepsy12", "KPI")

# first set default value 'NOT_SCORED' to all KPIs
paediatrician_with_expertise_in_epilepsies = KPI_SCORE["NOT_SCORED"]
epilepsy_specialist_nurse = KPI_SCORE["NOT_SCORED"]
Expand Down
34 changes: 13 additions & 21 deletions epilepsy12/common_view_functions/group_for_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@

# rcpch
from epilepsy12.constants.user_types import (
ROLES,
TITLES,
AUDIT_CENTRE_LEAD_CLINICIAN,
TRUST_AUDIT_TEAM_FULL_ACCESS,
AUDIT_CENTRE_CLINICIAN,
TRUST_AUDIT_TEAM_EDIT_ACCESS,
# groups
AUDIT_CENTRE_ADMINISTRATOR,
AUDIT_CENTRE_CLINICIAN,
AUDIT_CENTRE_LEAD_CLINICIAN,
RCPCH_AUDIT_TEAM,
RCPCH_AUDIT_PATIENT_FAMILY,
# permissions
TRUST_AUDIT_TEAM_VIEW_ONLY,
TRUST_AUDIT_TEAM_EDIT_ACCESS,
RCPCH_AUDIT_LEAD,
TRUST_AUDIT_TEAM_FULL_ACCESS,
EPILEPSY12_AUDIT_TEAM_FULL_ACCESS,
RCPCH_AUDIT_ANALYST,
EPILEPSY12_AUDIT_TEAM_EDIT_ACCESS,
RCPCH_AUDIT_ADMINISTRATOR,
EPILEPSY12_AUDIT_TEAM_VIEW_ONLY,
RCPCH_AUDIT_PATIENT_FAMILY,
PATIENT_ACCESS,
TRUST_AUDIT_TEAM_VIEW_ONLY,
AUDIT_CENTRE_MANAGER,
# preferences in the view
VIEW_PREFERENCES,
)


Expand All @@ -38,14 +34,10 @@ def group_for_role(role_key):
group = Group.objects.get(name=TRUST_AUDIT_TEAM_EDIT_ACCESS)
elif role_key == AUDIT_CENTRE_ADMINISTRATOR:
group = Group.objects.get(name=TRUST_AUDIT_TEAM_VIEW_ONLY)
elif role_key == AUDIT_CENTRE_MANAGER:
group = Group.objects.get(name=TRUST_AUDIT_TEAM_VIEW_ONLY)
elif role_key == RCPCH_AUDIT_LEAD:

elif role_key == RCPCH_AUDIT_TEAM:
group = Group.objects.get(name=EPILEPSY12_AUDIT_TEAM_FULL_ACCESS)
elif role_key == RCPCH_AUDIT_ANALYST:
group = Group.objects.get(name=EPILEPSY12_AUDIT_TEAM_EDIT_ACCESS)
elif role_key == RCPCH_AUDIT_ADMINISTRATOR:
group = Group.objects.get(name=EPILEPSY12_AUDIT_TEAM_VIEW_ONLY)

elif role_key == RCPCH_AUDIT_PATIENT_FAMILY:
group = Group.objects.get(name=PATIENT_ACCESS)
else:
Expand Down
Loading

0 comments on commit c59f5e3

Please sign in to comment.