Skip to content

Commit

Permalink
Merge pull request #233 from rcpch:mbarton/use-forms-for-csv-upload-v…
Browse files Browse the repository at this point in the history
…alidation

Mbarton/use-forms-for-csv-upload-validation
  • Loading branch information
eatyourpeas committed Aug 5, 2024
2 parents 197d2b7 + dddbc8f commit e46965a
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 1,117 deletions.
52 changes: 28 additions & 24 deletions project/npda/forms/patient_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django import forms

# project imports
import nhs_number
from ..models import Patient
from ...constants.styles.form_styles import *
from ..general_functions import (
Expand All @@ -20,6 +21,19 @@ class DateInput(forms.DateInput):
input_type = "date"


class NHSNumberField(forms.CharField):
def to_python(self, value):
number = super().to_python(value)
normalised = nhs_number.normalise_number(number)

# For some combinations we get back an empty string (eg '719-573 0220')
return normalised or value

def validate(self, value):
if not nhs_number.is_valid(value):
raise ValidationError("Invalid NHS number")


class PatientForm(forms.ModelForm):

quarter = forms.ChoiceField(
Expand Down Expand Up @@ -49,6 +63,7 @@ class Meta:
"gp_practice_postcode",
"quarter",
]
field_classes = {"nhs_number": NHSNumberField}
widgets = {
"nhs_number": forms.TextInput(
attrs={"class": TEXT_INPUT},
Expand Down Expand Up @@ -98,40 +113,29 @@ def clean(self):
gp_practice_ods_code = cleaned_data.get("gp_practice_ods_code")
gp_practice_postcode = cleaned_data.get("gp_practice_postcode")

if diagnosis_date is None:
raise ValidationError(
{"diagnosis_date": ["'Date of Diabetes Diagnosis' cannot be empty"]}
)

if date_of_birth is None:
raise ValidationError(
{"date_of_birth": ["'Date of Birth' cannot be empty"]}
)

if diagnosis_date is not None and date_of_birth is not None:
if diagnosis_date < date_of_birth:
raise ValidationError(
{
"diagnosis_date": [
"'Date of Diabetes Diagnosis' cannot be before 'Date of Birth'"
]
}
self.add_error(
"diagnosis_date",
ValidationError(
"'Date of Diabetes Diagnosis' cannot be before 'Date of Birth'"
),
)

if death_date is not None and date_of_birth is not None:
if death_date < date_of_birth:
raise ValidationError(
{"death_date": ["'Death Date' cannot be before 'Date of Birth'"]}
self.add_error(
"death_date",
ValidationError("'Death Date' cannot be before 'Date of Birth'"),
)

if death_date is not None and diagnosis_date is not None:
if death_date < diagnosis_date:
raise ValidationError(
{
"death_date": [
"'Death Date' cannot be before 'Date of Diabetes Diagnosis'"
]
}
self.add_error(
"death_date",
ValidationError(
"'Death Date' cannot be before 'Date of Diabetes Diagnosis'"
),
)

if gp_practice_ods_code is None and gp_practice_postcode is None:
Expand Down
66 changes: 65 additions & 1 deletion project/npda/forms/visit_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ def clean_height(self):
raise ValidationError("Please enter a valid height. Cannot be greater than 240cm")
return data

def clean_weight(self):
data = self.cleaned_data["height"]
if data is not None:
if data < 1:
raise ValidationError("Patient Weight (kg)' invalid. Cannot be below 1kg")
if data > 200:
raise ValidationError("Patient Weight (kg)' invalid. Cannot be above 200kg")
return data

def clean_visit_date(self):
data = self.cleaned_data['visit_date']
Expand Down Expand Up @@ -188,6 +196,42 @@ def clean_hba1c_date(self):

return self.cleaned_data['hba1c_date']

def clean_systolic_blood_pressure(self):
systolic_blood_pressure = self.cleaned_data['systolic_blood_pressure']

if systolic_blood_pressure:
if systolic_blood_pressure < 80:
raise ValidationError("Systolic Blood Pressure out of range. Cannot be below 80")
elif systolic_blood_pressure > 240:
raise ValidationError("Systolic Blood Pressure out of range. Cannot be above 240")

def clean_diastolic_blood_pressure(self):
diastolic_blood_pressure = self.cleaned_data['diastolic_blood_pressure']

if diastolic_blood_pressure:
if diastolic_blood_pressure < 20:
raise ValidationError("Diastolic Blood pressure out of range. Cannot be below 20")
elif diastolic_blood_pressure > 120:
raise ValidationError("Diastolic Blood pressure out of range. Cannot be above 120")

def clean_albumin_creatinine_ratio(self):
albumin_creatinine_ratio = self.cleaned_data['albumin_creatinine_ratio']

if albumin_creatinine_ratio:
if albumin_creatinine_ratio < 20:
raise ValidationError("Urinary Albumin Level (ACR) out of range. Cannot be below 0")
elif albumin_creatinine_ratio > 50:
raise ValidationError("Urinary Albumin Level (ACR) out of range. Cannot be above 50")

def clean_total_cholesterol(self):
total_cholesterol = self.cleaned_data['total_cholesterol']

if total_cholesterol:
if total_cholesterol < 2:
raise ValidationError("Total Cholesterol Level (mmol/l) out of range. Cannot be below 2")
elif total_cholesterol > 12:
raise ValidationError("Total Cholesterol Level (mmol/l) out of range. Cannot be above 12")

def clean_blood_pressure_observation_date(self):
data = self.cleaned_data['blood_pressure_observation_date']
valid, error = validate_date(
Expand Down Expand Up @@ -411,4 +455,24 @@ def clean_hospital_discharge_date(self):
if valid==False:
raise ValidationError(error)

return self.cleaned_data['hospital_discharge_date']
return self.cleaned_data['hospital_discharge_date']

def clean(self):
cleaned_data = super().clean()

hba1c_value = cleaned_data['hba1c']
hba1c_format = cleaned_data['hba1c_format']

if hba1c_value:
if hba1c_format == 1:
# mmol/mol
if hba1c_value < 20:
raise ValidationError("Hba1c Value out of range (mmol/mol). Cannot be below 20")
elif hba1c_value > 195:
raise ValidationError("Hba1c Value out of range (mmol/mol). Cannot be above 195")
elif hba1c_format == 2:
# %
if hba1c_value < 3:
raise ValidationError("Hba1c Value out of range (%). Cannot be below 3")
elif hba1c_value > 20:
raise ValidationError("Hba1c Value out of range (%). Cannot be above 20")
1 change: 0 additions & 1 deletion project/npda/general_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .quarter_for_date import *
from .csv_upload import *
from .email import *
from .group_for_group import *
from .index_multiple_deprivation import *
Expand Down
Loading

0 comments on commit e46965a

Please sign in to comment.