Skip to content

Commit

Permalink
adds in the for_patient method
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Apr 9, 2018
1 parent 8c6ccc3 commit d117e09
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,9 +6,15 @@ These now have querysets that we use for serializing and episode
to json.

##### PatientSubrecordQueryset.for_patients and EpisodeSubrecordQueryset.for_episodes
Returns all subrecord for an iterable of episodes.
These methods are used for the serialisation Episode querysets to json.

##### PatientSubrecordQueryset.for_patient
Returns all patient subrecords for a specific patient


##### PatientSubrecordQueryset.for_episode and EpisodeSubrecordQueryset.for_episode
Returns all subrecords for a specific episode.
These methods are used for the serialisation of a Patient.

#### Plugin API end points can now override application end points
Expand Down
5 changes: 5 additions & 0 deletions doc/docs/reference/subrecords.md
Expand Up @@ -231,8 +231,13 @@ These fields are then often used in forms to make the data read only
### Subrecord manager methods

#### PatientSubrecordQueryset.for_patients and EpisodeSubrecordQueryset.for_episodes
Returns all subrecord for an iterable of episodes.
These methods are used for the serialisation Episode querysets to json. The preselect many to many
keys and free text or foreign key relationships.

#### PatientSubrecordQueryset.for_episode and EpisodeSubrecordQueryset.for_episode
Returns all subrecords for a specific episode.
These methods are used for the serialisation of a Patient.

#### PatientSubrecordQueryset.for_patient
Returns all patient subrecords for a specific patient
4 changes: 2 additions & 2 deletions opal/core/pathway/steps.py
Expand Up @@ -16,9 +16,9 @@ def delete_others(data, model, patient=None, episode=None):
from opal.models import EpisodeSubrecord, PatientSubrecord

if issubclass(model, EpisodeSubrecord):
existing = model.objects.filter(episode=episode)
existing = model.objects.for_episode(episode=episode)
elif issubclass(model, PatientSubrecord):
existing = model.objects.filter(patient=patient)
existing = model.objects.for_patient(patient=patient)
else:
err = "Delete others called with {} requires a subrecord"
raise exceptions.APIError(err.format(model.__name__))
Expand Down
7 changes: 6 additions & 1 deletion opal/managers.py
Expand Up @@ -137,12 +137,17 @@ def prefetch(qs):


class PatientSubrecordQueryset(models.QuerySet):
def for_patient(self, patient):
"""
"""
return self.filter(patient=patient)

def for_episode(self, episode):
"""
returns all subrecords related to an
episode.
"""
return self.filter(patient__episode=episode)
return self.for_patient(episode.patient)

def for_patients(self, patients):
"""
Expand Down
10 changes: 10 additions & 0 deletions opal/tests/test_managers.py
Expand Up @@ -240,6 +240,16 @@ def test_for_episodes(self):


class PatientSubrecordQuerysetTestCase(OpalTestCase):
def test_for_patient(self):
patient, _ = self.new_patient_and_episode_please()
test_models.InvisibleDog.objects.create(
name="Fido", patient=patient
)
result = test_models.InvisibleDog.objects.for_patient(
patient
)
self.assertEqual(result.get().name, "Fido")

def test_for_episode(self):
patient, episode = self.new_patient_and_episode_please()
test_models.InvisibleDog.objects.create(
Expand Down

0 comments on commit d117e09

Please sign in to comment.