diff --git a/CHANGELOG.md b/CHANGELOG.md index 6908a83a6..4a88d0253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/docs/reference/subrecords.md b/doc/docs/reference/subrecords.md index c53af55be..d61cb3726 100644 --- a/doc/docs/reference/subrecords.md +++ b/doc/docs/reference/subrecords.md @@ -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 diff --git a/opal/core/pathway/steps.py b/opal/core/pathway/steps.py index d97677809..b638f7a47 100644 --- a/opal/core/pathway/steps.py +++ b/opal/core/pathway/steps.py @@ -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__)) diff --git a/opal/managers.py b/opal/managers.py index 8e5a7d34d..2bc19e818 100644 --- a/opal/managers.py +++ b/opal/managers.py @@ -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): """ diff --git a/opal/tests/test_managers.py b/opal/tests/test_managers.py index 24fd5c913..54fdecb11 100644 --- a/opal/tests/test_managers.py +++ b/opal/tests/test_managers.py @@ -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(