-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple referrals #353
Open
fredkingham
wants to merge
27
commits into
multiple-episodes-branch
Choose a base branch
from
multiple-referrals
base: multiple-episodes-branch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Multiple referrals #353
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
07d25b0
Remove the field employment from the referral
1904150
Changes employment into a singleton and removers the ability to edit …
6e0eb2a
Renames summary to referrals
b424b2a
Iterate over different episodes as different referrals in the UI unde…
c39fc7d
Adds in a custom javascript controller to order the referrals by the …
3869adc
Adds a migration to make a new episodes from patients with multiple r…
1aa2a1b
Only show the clinic information if there is any clinic information t…
6c22fe8
Adds the ability to delete an episode
9721dac
Do not show the delete button if there is only a single referral
3508727
Show the full name of the patient in the delete episode modal
4ca2b7e
Fix the cancel button on the delete episode modal, and fix the error …
50c4f8c
Adds in the add episode (or add referral) flow
028e440
Change referrals to be a singleton
4017e12
Merge branch 'v1.30' into multiple-referrals
861af8f
the create singletons signals do not fire in the migrations when we c…
4be56f8
Fixes the word casing in the add episode/delete episode modals
4fc0cfb
Invert the order of the referrals so the first one would be 3/3 and s…
7c3b106
Shrink the referral bar and make the buttons look like the links in t…
94b8252
Adds a missing migration to rbhl that changes the choice 'no relevant…
9587bf4
Adds consistency tokens to singletons that have been imported via imp…
b955cd2
Add a full list of everything that will be deleted when you delete a …
3d6c077
After we create an episode, open the edit item modal
e90fc35
switch the order of the add referral/delete referral buttons
3d79585
Fix up the lab views to work correctly with the reference to employme…
dccdaf6
if they patient does not have any tests make sure we don't fail when …
13f23db
Make the add clinic data button small
0b5b8a7
change the multiple episode load to use multiple referral dates when …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 2.2.16 on 2022-05-16 13:29 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('rbhl', '0063_create_multiple_episodes'), | ||
('lab', '0015_auto_20220511_1354'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='bloods', | ||
name='employment', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Generated by Django 2.2.16 on 2022-05-16 16:43 | ||
|
||
from django.db import migrations | ||
from django.db.models import Count | ||
from django.core import management | ||
|
||
|
||
|
||
# Generated by Django 2.2.16 on 2021-11-26 13:50 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def get_episode_ids_with_duplicate_referrals(Episode): | ||
""" | ||
Returns episode ids of patients that seem to have | ||
duplicate referrals because they have the same | ||
date of referral or date of first appointment. | ||
""" | ||
result = set() | ||
multiple_referral_episodes = Episode.objects.annotate(cnt=Count('referral')).filter(cnt__gt=1) | ||
multiple_referral_episodes = multiple_referral_episodes.prefetch_related('referral_set') | ||
for episode in multiple_referral_episodes: | ||
referrals = episode.referral_set.all() | ||
referral_dates = [i.date_of_referral for i in referrals if i.date_of_referral] | ||
if referral_dates and not len(referral_dates) == len(set(referral_dates)): | ||
result.add(episode.id) | ||
else: | ||
dates_of_first_clinic = [ | ||
i.date_first_appointment for i in referrals if i.date_first_appointment | ||
] | ||
if dates_of_first_clinic: | ||
if not len(dates_of_first_clinic) == len(set(dates_of_first_clinic)): | ||
result.add(episode.id) | ||
return result | ||
|
||
|
||
def get_closest_referral(episode): | ||
""" | ||
Returns the closest referral within a year before the clinic date | ||
""" | ||
referrals = episode.referral_set.all() | ||
clinic_log = episode.cliniclog_set.all()[0] | ||
if not clinic_log.clinic_date: | ||
return | ||
referrals_with_dates = [ | ||
i for i in referrals if i.date_of_referral if i.date_of_referral | ||
] | ||
closest = None | ||
closest_diff = None | ||
for referral in referrals_with_dates: | ||
if not referral.date_of_referral: | ||
continue | ||
if referral.date_of_referral > clinic_log.clinic_date: | ||
continue | ||
days_diff = (clinic_log.clinic_date - referral.date_of_referral).days | ||
if days_diff > 365: | ||
continue | ||
if not closest: | ||
closest = referral | ||
closest_diff = days_diff | ||
elif closest_diff > days_diff: | ||
closest = referral | ||
closest_diff = days_diff | ||
return closest | ||
|
||
|
||
def create_episode_from_referral(patient, referral): | ||
episode = patient.episode_set.create() | ||
bloods = referral.bloods_set.all() | ||
referral.episode = episode | ||
referral.save() | ||
if bloods: | ||
for blood in bloods: | ||
if blood.employment: | ||
employment = blood.employment | ||
employment.episode = episode | ||
employment.save() | ||
|
||
|
||
def create_episodes(episode): | ||
""" | ||
If there is no clinic log date then this is a sign that its | ||
a patient who has only been seen by the lab. Create an episode | ||
for each referral after the initial. | ||
|
||
If there is a clinic log date then create episides for all | ||
referrals apart from the one closest the clinic log date. | ||
|
||
Otherwise create an episode for each referral after the initial | ||
""" | ||
clinic_log = episode.cliniclog_set.all()[0] | ||
|
||
if not clinic_log.clinic_date: | ||
referrals_that_need_episodes = list(episode.referral_set.all())[1:] | ||
else: | ||
closest_referral = get_closest_referral(episode) | ||
if closest_referral: | ||
referrals_that_need_episodes = [ | ||
i for i in episode.referral_set.all() if not i.id == closest_referral.id | ||
] | ||
else: | ||
referrals_that_need_episodes = list(episode.referral_set.all())[1:] | ||
for referral in referrals_that_need_episodes: | ||
create_episode_from_referral(episode.patient, referral) | ||
|
||
|
||
def forwards(apps, schema_editor): | ||
Episode = apps.get_model('opal', 'Episode') | ||
episodes_with_duplicate_referrals = get_episode_ids_with_duplicate_referrals(Episode) | ||
episodes_with_multiple_referrals = Episode.objects.annotate( | ||
cnt=Count('referral') | ||
).filter( | ||
cnt__gt=1 | ||
).exclude( | ||
id__in=episodes_with_duplicate_referrals | ||
).prefetch_related( | ||
'referral_set', 'cliniclog_set' | ||
) | ||
for episode in episodes_with_multiple_referrals: | ||
create_episodes(episode) | ||
management.call_command('create_singletons') | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('rbhl', '0062_cliniclog_sword'), | ||
] | ||
|
||
|
||
operations = [ | ||
migrations.RunPython( | ||
forwards | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.2.16 on 2022-05-18 11:41 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this migration from something else? |
||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('rbhl', '0063_create_multiple_episodes'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='cliniclog', | ||
name='sword', | ||
field=models.CharField(blank=True, choices=[('Not relevant', 'Not relevant'), ('Needs to be reported', 'Needs to be reported'), ('Reported', 'Reported')], max_length=256, null=True, verbose_name='SWORD'), | ||
), | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we still want this?