def test_field_schema(self, patient_list): patient_list.return_value = [test_patient_lists.TaggingTestPatientList] expected = [ {'name': 'eater', 'title': 'Eater', 'type': 'boolean'}, {'type': 'boolean', 'name': 'herbivore', 'title': 'Herbivore'} ] schema = Tagging.build_field_schema() self.assertEqual(expected, schema)
def ever_tagged(self, team): """ Return a list of episodes that were ever tagged to TEAM """ from opal.models import Tagging team_name = team.lower().replace(' ', '_') current = self.filter(tagging__team__name=team_name) historic = Tagging.historic_episodes_for_tag(team_name) return list(historic) + list(current)
def test_form_template(self): self.assertEqual('tagging_modal.html', Tagging.get_form_template())
def test_display_template(self): self.assertEqual('tagging.html', Tagging.get_display_template())
def test_tagging_import_from_reversion(self): import reversion from django.db import transaction patient = Patient.objects.create() with transaction.atomic(), reversion.create_revision(): team_1 = Team.objects.create(name='team 1', title='team 1') team_2 = Team.objects.create(name='team 2', title='team 2') mine = Team.objects.create(name='mine', title='mine') user_1 = self.user user_2 = User.objects.create(username="******", is_staff=False, is_superuser=False) # episode 1 has a tag and an archived tag episode_1 = patient.create_episode(category="testepisode", ) # episode 2 had 1 tag, it was deleted, then it was readded episode_2 = patient.create_episode(category="testepisode", ) # episode 3 has 2 tags and no new tags episode_3 = patient.create_episode(category="testepisode", ) # episode 4 has no tags and 2 deleted tags episode_4 = patient.create_episode(category="testepisode", ) # episode 5 has only user tags, 1 is deleted the other is not episode_5 = patient.create_episode(category="testepisode", ) # episode 6 has only 1 user tag which has been deleted and replaced episode_6 = patient.create_episode(category="testepisode", ) # episode 7 has a mixture of both types of user tags and episode tags episode_7 = patient.create_episode(category="testepisode", ) # episode 1 Tagging.objects.create(episode=episode_1, team=team_1) deleted_tag = Tagging.objects.create(episode=episode_1, team=team_2) with transaction.atomic(), reversion.create_revision(): deleted_tag.delete() with transaction.atomic(), reversion.create_revision(): deleted_tag = Tagging.objects.create(episode=episode_2, team=team_1) with transaction.atomic(), reversion.create_revision(): deleted_tag.delete() # episode 2 Tagging.objects.create(episode=episode_2, team=team_1) # episode 3 Tagging.objects.create(episode=episode_3, team=team_1) Tagging.objects.create(episode=episode_3, team=team_2) # episode 4 to_delete_1 = Tagging.objects.create(episode=episode_4, team=team_1) to_delete_2 = Tagging.objects.create(episode=episode_4, team=team_2) with transaction.atomic(), reversion.create_revision(): to_delete_1.delete() to_delete_2.delete() # episode 5 with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_5, team=mine, user=user_1) to_delete = Tagging.objects.create(episode=episode_5, team=mine, user=user_2) with transaction.atomic(), reversion.create_revision(): to_delete.delete() # episode 6 with transaction.atomic(), reversion.create_revision(): to_delete = Tagging.objects.create(episode=episode_6, team=mine, user=user_1) with transaction.atomic(), reversion.create_revision(): to_delete.delete() with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_6, team=mine, user=user_1) # episode 7 with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_7, team=mine, user=user_1) to_delete_1 = Tagging.objects.create(episode=episode_7, team=mine, user=user_2) Tagging.objects.create(episode=episode_7, team=team_1) to_delete_2 = Tagging.objects.create(episode=episode_7, team=team_2) with transaction.atomic(), reversion.create_revision(): to_delete_1.delete() to_delete_2.delete() Tagging.import_from_reversion() self.assertTrue( Tagging.objects.filter(episode=episode_1, team=team_1, archived=False).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_1, team=team_2, archived=True).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue( Tagging.objects.filter(episode=episode_2, team=team_1, archived=False).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_2).count(), 1) self.assertTrue( Tagging.objects.filter(episode=episode_3, team=team_1, archived=False).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_3, team=team_2, archived=False).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue( Tagging.objects.filter(episode=episode_4, team=team_1, archived=True).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_4, team=team_2, archived=True).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue( Tagging.objects.filter(episode=episode_5, team=mine, user=user_1, archived=False).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_5, team=mine, user=user_2, archived=True).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_5).count(), 2) self.assertTrue( Tagging.objects.filter(episode=episode_6, team=mine, user=user_1, archived=False).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_6).count(), 1) self.assertTrue( Tagging.objects.filter(episode=episode_7, team=mine, user=user_1, archived=False).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_7, team=mine, user=user_2, archived=True).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_7, team=team_1, archived=False).exists()) self.assertTrue( Tagging.objects.filter(episode=episode_7, team=team_2, archived=True).exists()) self.assertEqual(Tagging.objects.filter(episode=episode_7).count(), 4)
def test_field_schema(self): names = ['eater', 'herbivore', 'carnivore'] fields = [{'name': tagname, 'type': 'boolean'} for tagname in names] schema = Tagging.build_field_schema() for field in fields: self.assertIn(field, schema)
def test_tagging_import(self): import reversion from django.db import transaction patient = Patient.objects.create() with transaction.atomic(), reversion.create_revision(): team_1 = Team.objects.create(name='team 0', title='team 1') team_2 = Team.objects.create(name='team 2', title='team 2') mine = Team.objects.create(name='mine', title='mine') user_1 = self.user user_2 = User.objects.create( username="******", is_staff=False, is_superuser=False ) # episode 1 has a tag and an archived tag episode_1 = patient.create_episode( category="testepisode", ) # episode 2 had 1 tag, it was deleted, then it was readded episode_2 = patient.create_episode( category="testepisode", ) # episode 3 has 2 tags and no new tags episode_3 = patient.create_episode( category="testepisode", ) # episode 4 has no tags and 2 deleted tags episode_4 = patient.create_episode( category="testepisode", ) # episode 5 has only user tags, 1 is deleted the other is not episode_5 = patient.create_episode( category="testepisode", ) # episode 6 has only 1 user tag which has been deleted and replaced episode_6 = patient.create_episode( category="testepisode", ) # episode 7 has a mixture of both types of user tags and episode tags episode_7 = patient.create_episode( category="testepisode", ) # episode 1 Tagging.objects.create(episode=episode_1, team=team_1) deleted_tag = Tagging.objects.create(episode=episode_1, team=team_2) with transaction.atomic(), reversion.create_revision(): deleted_tag.delete() with transaction.atomic(), reversion.create_revision(): deleted_tag = Tagging.objects.create(episode=episode_2, team=team_1) with transaction.atomic(), reversion.create_revision(): deleted_tag.delete() # episode 2 Tagging.objects.create(episode=episode_2, team=team_1) # episode 3 Tagging.objects.create(episode=episode_3, team=team_1) Tagging.objects.create(episode=episode_3, team=team_2) # episode 4 to_delete_1 = Tagging.objects.create(episode=episode_4, team=team_1) to_delete_2 = Tagging.objects.create(episode=episode_4, team=team_2) with transaction.atomic(), reversion.create_revision(): to_delete_1.delete() to_delete_2.delete() # episode 5 with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_5, team=mine, user=user_1) to_delete = Tagging.objects.create(episode=episode_5, team=mine, user=user_2) with transaction.atomic(), reversion.create_revision(): to_delete.delete() # episode 6 with transaction.atomic(), reversion.create_revision(): to_delete = Tagging.objects.create(episode=episode_6, team=mine, user=user_1) with transaction.atomic(), reversion.create_revision(): to_delete.delete() with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_6, team=mine, user=user_1) # episode 7 with transaction.atomic(), reversion.create_revision(): Tagging.objects.create(episode=episode_7, team=mine, user=user_1) to_delete_1 = Tagging.objects.create(episode=episode_7, team=mine, user=user_2) Tagging.objects.create(episode=episode_7, team=team_1) to_delete_2 = Tagging.objects.create(episode=episode_7, team=team_2) with transaction.atomic(), reversion.create_revision(): to_delete_1.delete() to_delete_2.delete() Tagging.import_from_reversion() self.assertTrue(Tagging.objects.filter( episode=episode_1, team=team_1, archived=False ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_1, team=team_2, archived=True ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue(Tagging.objects.filter( episode=episode_2, team=team_1, archived=False ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_2).count(), 1) self.assertTrue(Tagging.objects.filter( episode=episode_3, team=team_1, archived=False ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_3, team=team_2, archived=False ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue(Tagging.objects.filter( episode=episode_4, team=team_1, archived=True ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_4, team=team_2, archived=True ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_1).count(), 2) self.assertTrue(Tagging.objects.filter( episode=episode_5, team=mine, user=user_1, archived=False ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_5, team=mine, user=user_2, archived=True ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_5).count(), 2) self.assertTrue(Tagging.objects.filter( episode=episode_6, team=mine, user=user_1, archived=False ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_6).count(), 1) self.assertTrue(Tagging.objects.filter( episode=episode_7, team=mine, user=user_1, archived=False ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_7, team=mine, user=user_2, archived=True ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_7, team=team_1, archived=False ).exists() ) self.assertTrue(Tagging.objects.filter( episode=episode_7, team=team_2, archived=True ).exists() ) self.assertEqual(Tagging.objects.filter(episode=episode_7).count(), 4)
def serialised(self, user, episodes, historic_tags=False, episode_history=False): """ Return a set of serialised EPISODES. If HISTORIC_TAGS is Truthy, return deleted tags as well. If EPISODE_HISTORY is Truthy return historic episodes as well. """ patient_ids = [e.patient_id for e in episodes] patient_subs = defaultdict(lambda: defaultdict(list)) episode_subs = self.serialised_episode_subrecords(episodes, user) for model in patient_subrecords(): name = model.get_api_name() subrecords = model.objects.filter(patient__in=patient_ids) for sub in subrecords: patient_subs[sub.patient_id][name].append(sub.to_dict(user)) # We do this here because it's an order of magnitude quicker than hitting # episode.tagging_dict() for each episode in a loop. taggings = defaultdict(dict) from opal.models import Tagging for tag in Tagging.objects.filter( episode__in=episodes).select_related('team'): if tag.team.name == 'mine' and tag.user != user: continue taggings[tag.episode_id][tag.team.name] = True serialised = [] for e in episodes: d = { 'id': e.id, 'category': e.category, 'active': e.active, 'date_of_admission': e.date_of_admission, 'date_of_episode': e.date_of_episode, 'discharge_date': e.discharge_date, 'consistency_token': e.consistency_token } for key, value in episode_subs[e.id].items(): d[key] = value for key, value in patient_subs[e.patient_id].items(): d[key] = value d['tagging'] = [taggings[e.id]] serialised.append(d) if episode_history: d['episode_history'] = e._episode_history_to_dict(user) if historic_tags: print 'Historic Tags' historic = Tagging.historic_tags_for_episodes(episodes) for episode in serialised: if episode['id'] in historic: historic_tags = historic[episode['id']] for t in historic_tags.keys(): episode['tagging'][0][t] = True return serialised
def serialised(self, user, episodes, historic_tags=False, episode_history=False): """ Return a set of serialised EPISODES. If HISTORIC_TAGS is Truthy, return deleted tags as well. If EPISODE_HISTORY is Truthy return historic episodes as well. """ patient_ids = [e.patient_id for e in episodes] patient_subs = defaultdict(lambda: defaultdict(list)) episode_subs = self.serialised_episode_subrecords(episodes, user) for model in patient_subrecords(): name = model.get_api_name() subrecords = model.objects.filter(patient__in=patient_ids) for sub in subrecords: patient_subs[sub.patient_id][name].append(sub.to_dict(user)) # We do this here because it's an order of magnitude quicker than hitting # episode.tagging_dict() for each episode in a loop. taggings = defaultdict(dict) from opal.models import Tagging for tag in Tagging.objects.filter(episode__in=episodes).select_related('team'): if tag.team.name == 'mine' and tag.user != user: continue taggings[tag.episode_id][tag.team.name] = True serialised = [] for e in episodes: d = { 'id' : e.id, 'category' : e.category, 'active' : e.active, 'date_of_admission': e.date_of_admission, 'date_of_episode' : e.date_of_episode, 'discharge_date' : e.discharge_date, 'consistency_token': e.consistency_token } for key, value in episode_subs[e.id].items(): d[key] = value for key, value in patient_subs[e.patient_id].items(): d[key] = value d['tagging'] = [taggings[e.id]] serialised.append(d) if episode_history: d['episode_history'] = e._episode_history_to_dict(user) if historic_tags: print 'Historic Tags' historic = Tagging.historic_tags_for_episodes(episodes) for episode in serialised: if episode['id'] in historic: historic_tags = historic[episode['id']] for t in historic_tags.keys(): episode['tagging'][0][t] = True return serialised