Skip to content

Commit

Permalink
Merge pull request #87 from rcpch:eatyourpeas/issue75
Browse files Browse the repository at this point in the history
Eatyourpeas/issue75
  • Loading branch information
eatyourpeas authored Jun 2, 2024
2 parents 1ec4af8 + a331029 commit 22c7e98
Show file tree
Hide file tree
Showing 25 changed files with 19,251 additions and 1,204 deletions.
47 changes: 42 additions & 5 deletions project/npda/forms/npda_user_form.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django import forms
from django.contrib.auth.forms import SetPasswordForm, AuthenticationForm
from ..models import NPDAUser
from ...constants.styles.form_styles import *
# python imports
import logging

# django imports
from django.conf import settings
from django.contrib.auth.forms import SetPasswordForm, AuthenticationForm
from django import forms
from django.utils import timezone
from django.utils.translation import gettext as _
from django.conf import settings

# third party imports
from captcha.fields import CaptchaField

# RCPCH imports
from ...constants.styles.form_styles import *
from ..models import NPDAUser
from project.npda.general_functions import get_all_nhs_organisations


# Logging setup
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -43,6 +52,34 @@ class Meta:
"organisation_employer": forms.Select(attrs={"class": SELECT}),
}

def __init__(self, *args, **kwargs) -> None:
# get the request object from the kwargs
self.request = kwargs.pop("request", None)

super().__init__(*args, **kwargs)
self.fields["title"].required = True
self.fields["first_name"].required = True
self.fields["surname"].required = True
self.fields["email"].required = True
self.fields["role"].required = True
# retrieve all organisations from the RCPCH NHS Organisations API
if (
self.request.user.is_superuser
or self.request.user.is_rcpch_audit_team_member
or self.request.user.is_rcpch_staff
):
# this is an ordered list of tuples from the API
self.fields["organisation_employer"].choices = get_all_nhs_organisations()
else:
# create list of choices from the session data
sibling_organisations = [
(sibling["ods_code"], sibling["name"])
for sibling in self.request.session.get("sibling_organisations")[
"organisations"
]
]
self.fields["organisation_employer"].choices = sibling_organisations


class NPDAUpdatePasswordForm(SetPasswordForm):
# form show when setting or resetting password
Expand Down
2 changes: 2 additions & 0 deletions project/npda/general_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .group_for_group import *
from .index_multiple_deprivation import *
from .nhs_ods_requests import *
from .rcpch_nhs_organisations import *
from .retrieve_pdu import *
from .time_elapsed import *
from .validate_dates import *
from .validate_postcode import *
Expand Down
49 changes: 21 additions & 28 deletions project/npda/general_functions/csv_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
logger = logging.getLogger(__name__)


def csv_upload(csv_file=None):
def csv_upload(csv_file=None, organisation_ods_code=None, pdu_pz_code=None):
"""
Processes standardised NPDA csv file and persists results in NPDA tables
Expand Down Expand Up @@ -781,12 +781,32 @@ def save_row(row):
site_errors,
) = validate_row(row)

try:
# save site
site, created = Site.objects.get_or_create(
date_leaving_service=(
row["Date of leaving service"]
if not pd.isnull(row["Date of leaving service"])
else None
),
reason_leaving_service=(
row["Reason for leaving service"]
if not pd.isnull(row["Reason for leaving service"])
else None
),
paediatric_diabetes_unit_pz_code=pdu_pz_code,
organisation_ods_code=organisation_ods_code,
)
except Exception as error:
raise Exception(f"Could not save site: {error}")

nhs_number = row["NHS Number"].replace(" ", "")

try:
patient, created = Patient.objects.update_or_create(
nhs_number=nhs_number,
defaults={
"site": site,
"date_of_birth": row["Date of Birth"],
"postcode": row["Postcode of usual address"],
"sex": row["Stated gender"],
Expand All @@ -804,33 +824,6 @@ def save_row(row):
except Exception as error:
raise Exception(f"Could not save patient: {error}")

# This is a temporizing step while we have no Organisations models available
"""
try:
pdu = PaediatricDiabetesUnit.objects.get(pz_code=row["PDU Number"])
except Exception as error:
raise Exception(f"Could not find PDU: {error}")
"""
pdu = None

try:
site, created = Site.objects.get_or_create(
date_leaving_service=(
row["Date of leaving service"]
if not pd.isnull(row["Date of leaving service"])
else None
),
reason_leaving_service=(
row["Reason for leaving service"]
if not pd.isnull(row["Reason for leaving service"])
else None
),
pdu=pdu,
patient=patient,
)
except Exception as error:
raise Exception(f"Could not save site: {error}")

try:
obj = {
"patient": patient,
Expand Down
Loading

0 comments on commit 22c7e98

Please sign in to comment.