Skip to content

Commit

Permalink
Merge pull request #479 from rcpch/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
eatyourpeas authored May 7, 2023
2 parents 172c183 + e203c63 commit c3ade85
Show file tree
Hide file tree
Showing 31 changed files with 1,221 additions and 485 deletions.
42 changes: 27 additions & 15 deletions epilepsy12/common_view_functions/report_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,36 @@ def all_registered_cases_for_cohort_and_abstraction_level(
& Q(site__site_is_primary_centre_of_epilepsy_care=True)
)
elif abstraction_level == "icb":
q_filter = (
Q(
site__organisation__integrated_care_board__ODS_ICB_Code=organisation_instance.integrated_care_board.ODS_ICB_Code
if organisation_instance.integrated_care_board is not None:
"""
Wales has no ICBs
"""
q_filter = (
Q(
site__organisation__integrated_care_board__ODS_ICB_Code=organisation_instance.integrated_care_board.ODS_ICB_Code
)
& Q(site__site_is_actively_involved_in_epilepsy_care=True)
& Q(site__site_is_primary_centre_of_epilepsy_care=True)
)
& Q(site__site_is_actively_involved_in_epilepsy_care=True)
& Q(site__site_is_primary_centre_of_epilepsy_care=True)
)
else:
return all_cases_for_cohort
elif abstraction_level == "nhs_region":
q_filter = (
Q(
site__organisation__nhs_region__NHS_Region_Code=organisation_instance.nhs_region.NHS_Region_Code
)
& Q(
site__organisation__ons_region__ons_country__Country_ONS_Name=organisation_instance.ons_region.ons_country.Country_ONS_Name
"""
Wales has no NHS regions
"""
if organisation_instance.nhs_region is not None:
q_filter = (
Q(
site__organisation__nhs_region__NHS_Region_Code=organisation_instance.nhs_region.NHS_Region_Code
)
& Q(
site__organisation__ons_region__ons_country__Country_ONS_Name=organisation_instance.ons_region.ons_country.Country_ONS_Name
)
& Q(site__site_is_actively_involved_in_epilepsy_care=True)
& Q(site__site_is_primary_centre_of_epilepsy_care=True)
)
& Q(site__site_is_actively_involved_in_epilepsy_care=True)
& Q(site__site_is_primary_centre_of_epilepsy_care=True)
)
else:
return all_cases_for_cohort
elif abstraction_level == "open_uk":
q_filter = (
Q(
Expand Down
125 changes: 74 additions & 51 deletions epilepsy12/common_view_functions/validate_form_update_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@


def validate_and_update_model(
request,
model_id,
model,
field_name,
page_element,
comparison_date_field_name=None,
is_earliest_date=None
request,
model_id,
model,
field_name,
page_element,
comparison_date_field_name=None,
is_earliest_date=None,
earliest_allowable_date=None,
):
"""
This is called from the view to update the model or return an error
Expand All @@ -25,105 +26,127 @@ def validate_and_update_model(
page_element: string one of 'date_field', 'toggle_button', 'multiple_choice_single_toggle_button', 'multiple_choic_multiple_toggle_button', 'select', 'snomed_select', 'organisation_select'
comparison_date_field_name: string corresponding to field name for date in model
is_earliest_date: boolean
first_paediatric_assessment_date: date - used to validate
It replaces the decorator @update_model as decorators can only redirect the request,
It replaces the decorator @update_model as decorators can only redirect the request,
they cannot pass parameters to the function they wrap. This means that errors raised in updating the model
cannot be passed back to the template so the logic has been added to this function instead.
It is important that this function is called early on in the view function and that an updated instance of
It is important that this function is called early on in the view function and that an updated instance of
the model AFTER UPDATE is put in the context that is passed back to the template.
"""
if page_element == 'toggle_button':
if page_element == "toggle_button":
# toggle button
# the trigger_name of the element here corresponds to whether true or false has been selected

if request.htmx.trigger_name == 'button-true':
if request.htmx.trigger_name == "button-true":
field_value = True
elif request.htmx.trigger_name == 'button-false':
elif request.htmx.trigger_name == "button-false":
field_value = False
else:
# an error has occurred
print('Error has occurred')
print("Error has occurred")

elif page_element == 'multiple_choice_multiple_toggle_button' or page_element == 'single_choice_multiple_toggle_button':
elif (
page_element == "multiple_choice_multiple_toggle_button"
or page_element == "single_choice_multiple_toggle_button"
):
# multiple_choice_multiple_toggle_button
field_value = request.htmx.trigger_name

elif page_element == 'date_field':
field_value = datetime.strptime(request.POST.get(
request.htmx.trigger_name), "%Y-%m-%d").date()
elif page_element == "date_field":
field_value = datetime.strptime(
request.POST.get(request.htmx.trigger_name), "%Y-%m-%d"
).date()

elif page_element == 'select' or page_element == 'snomed_select':
if request.htmx.trigger_name == 'syndrome_name':
elif page_element == "select" or page_element == "snomed_select":
if request.htmx.trigger_name == "syndrome_name":
syndrome_entity = SyndromeEntity.objects.get(
pk=request.POST.get(request.htmx.trigger_name))
pk=request.POST.get(request.htmx.trigger_name)
)
field_value = syndrome_entity # note field name here is syndrome
elif request.htmx.trigger_name == 'epilepsy_cause':
elif request.htmx.trigger_name == "epilepsy_cause":
epilepsy_cause_entity = EpilepsyCauseEntity.objects.get(
pk=request.POST.get(request.htmx.trigger_name))
field_value = epilepsy_cause_entity # note field name here is epilepsy_cause
elif request.htmx.trigger_name == 'medicine_id':
pk=request.POST.get(request.htmx.trigger_name)
)
field_value = (
epilepsy_cause_entity # note field name here is epilepsy_cause
)
elif request.htmx.trigger_name == "medicine_id":
medicine_entity = MedicineEntity.objects.get(
pk=request.POST.get(request.htmx.trigger_name)
)
field_value = medicine_entity
field_name = 'medicine_entity'
field_name = "medicine_entity"
else:
field_value = request.POST.get(request.htmx.trigger_name)

# validate

if page_element == 'date_field':
if page_element == "date_field":
# date tests a bit involved
# No date can be before the date of birth
# If a comparison date field is supplied, the date itself might not yet have been set.
# The earlier of the two dates cannot be in the future and cannot be later than the second if supplied.
# The later of the two dates CAN be in the future but cannot be earlier than the first if supplied.
# If there is no comparison date (eg registration_date) the only stipulation is that it not be in the future.
date_valid = None
date_error = ""

if comparison_date_field_name:
if earliest_allowable_date:
if field_value < earliest_allowable_date:
# dates cannot be before the earliest allowable date (usually the first paediatric assessment or the cohort start date)
date_error = f"The date you chose ({field_value}) cannot not be before {earliest_allowable_date}."
date_valid = False

if comparison_date_field_name and date_valid is None:
instance = model.objects.get(pk=model_id)
comparison_date = getattr(
instance, comparison_date_field_name)
comparison_date = getattr(instance, comparison_date_field_name)
if is_earliest_date:
if comparison_date:
date_valid = field_value <= comparison_date and field_value <= date.today()
date_error = f'The date you chose ({field_value}) cannot not be after {comparison_date} or in the future.'
date_valid = (
field_value <= comparison_date and field_value <= date.today()
)
if field_value > comparison_date:
date_error = f"The date you chose ({field_value}) cannot be after {comparison_date}."
if field_value > date.today():
date_error = f"You cannot choose a date in the future."
else:
date_valid = field_value <= date.today()
date_error = f'The date you chose ({field_value}) cannot not be in the future.'
if not date_valid:
errors = date_error
if not date_valid:
date_error = f"The date you chose ({field_value}) cannot be in the future."

else:
if comparison_date:
date_valid = field_value >= comparison_date
date_error = f'The date you chose ({field_value}) cannot not be before {comparison_date}'
date_valid = (
field_value >= comparison_date and field_value <= date.today()
)
if field_value < comparison_date:
date_error = f"The date you chose ({field_value}) cannot be before {comparison_date}."
if field_value > date.today():
date_error = (
f"The date you chose ({field_value}) is in the future."
)
print(f"{field_value} and {comparison_date}")
else:
# no other date supplied yet
date_valid = True
if not date_valid:
errors = date_error

elif field_value > date.today() and (is_earliest_date is None or is_earliest_date):
elif field_value > date.today():
# dates cannot be in the future unless they are the second of 2 dates
date_error = f'The date you chose ({field_value}) cannot not be in the future.'
errors = date_error
date_error = f"The date you chose ({field_value}) cannot be in the future."
date_valid = False
else:
date_valid = True

if date_valid:
pass
else:
raise ValueError(errors)
raise ValueError(date_error)

# update the model

# if saving a registration_date, this has to trigger the save() method to generate a cohort
# so update() cannot be used
# This feels like a bit of a hack so very open to suggestions on more Django way of doing this
if field_name == 'registration_date':

if field_name == "registration_date":
# registration_date cannot be before date of birth
registration = Registration.objects.get(pk=model_id)
if field_value < registration.case.date_of_birth:
Expand All @@ -133,7 +156,8 @@ def validate_and_update_model(

# the registration date cannot be before the current cohort
current_cohort_end_date = first_tuesday_in_january(
current_cohort_start_date().year+2)+relativedelta(days=7)
current_cohort_start_date().year + 2
) + relativedelta(days=7)
if field_value < current_cohort_start_date():
date_valid = False
errors = f'The date you entered cannot be before the current cohort start date ({current_cohort_start_date().strftime("%d %B %Y")})'
Expand All @@ -152,12 +176,11 @@ def validate_and_update_model(
else:
updated_field = {
field_name: field_value,
'updated_at': timezone.now(),
'updated_by': request.user
"updated_at": timezone.now(),
"updated_by": request.user,
}

try:
model.objects.filter(
pk=model_id).update(**updated_field)
model.objects.filter(pk=model_id).update(**updated_field)
except DatabaseError as error:
raise Exception(error)
14 changes: 4 additions & 10 deletions epilepsy12/general_functions/random_generator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from random import randrange
from datetime import timedelta
from datetime import timedelta, date


def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
Thanks to Boris Verkihovsky (https://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates)
"""
def random_date(start: date, end: date):
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
random_days = randrange(0, delta.days)
return start + timedelta(days=random_days)
Loading

0 comments on commit c3ade85

Please sign in to comment.