Skip to content

Instantly share code, notes, and snippets.

@fredkingham
Last active February 3, 2020 15:38
Show Gist options
  • Save fredkingham/632650c55cf4afa46d9ba697f5d058bf to your computer and use it in GitHub Desktop.
Save fredkingham/632650c55cf4afa46d9ba697f5d058bf to your computer and use it in GitHub Desktop.
from django.db.models import Count
from opal.models import Episode, Patient
from odonto.episode_categories import *
def get_dates(episode):
dates = [
# it does not seem to be the case that this causes conflicts
# episode.orthodonticassessment_set.all()[0].date_of_referral,
# episode.orthodonticassessment_set.all()[0].date_of_assessment,
# episode.orthodonticassessment_set.all()[0].date_of_appliance_fitted,
# episode.orthodontictreatment_set.all()[0].date_of_completion,
episode.fp17incompletetreatment_set.all()[0].date_of_acceptance,
episode.fp17incompletetreatment_set.all()[0].completion_or_last_visit,
]
dates = [i for i in dates if i]
if len(dates) > 2:
dates = [min(dates), max(dates)]
return sorted(dates)
def compare_patient(patient):
eps= patient.episode_set.prefetch_related(
# 'orthodontictreatment_set',
# 'orthodonticassessment_set',
'fp17incompletetreatment_set'
)
for category in [FP17Episode.display_name, FP17OEpisode.display_name]:
episodes = eps.filter(category_name=category)
if episodes.count() < 2:
continue
episode_to_dates = {}
for i in episodes:
current_dates = get_dates(i)
if not current_dates:
continue
for y in episodes:
if y.id == i.id:
continue
other_dates = get_dates(y)
if not other_dates:
continue
if min(current_dates) <= min(other_dates) <= max(current_dates):
return patient
if min(current_dates) <= max(other_dates) <= max(current_dates):
return patient
def get_queryset():
episodes = Episode.objects.filter(stage__iexact=FP17Episode.SUBMITTED).exclude(
submission=None
)
patients = Patient.objects.filter(episode__in=episodes).distinct()
result = []
for patient in patients:
p = compare_patient(patient)
if p:
result.append(p)
if result:
return Patient.objects.filter(id__in=[i.id for i in result])
def print_episode(episode):
sub = episode.category.submission()
rejection = None
if sub and sub.rejection:
rejection = sub.rejection
ic = episode.fp17incompletetreatment_set.all()[0]
tc = episode.fp17treatmentcategory_set.all()[0]
ods = episode.fp17otherdentalservices_set.all()[0]
print(
f"{episode.id} {episode.stage} {ic.date_of_acceptance} {ic.completion_or_last_visit} {tc.treatment_category} {ods.further_treatment_within_2_months} {rejection}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment