def serialised_episode_subrecords(self, episodes, user): """ Return all serialised subrecords for this set of EPISODES in a nested hashtable where the outer key is the episode id, the inner key the subrecord API name. """ episode_subs = defaultdict(lambda: defaultdict(list)) for model in episode_subrecords(): name = model.get_api_name() subrecords = model.objects.filter(episode__in=episodes) for sub in subrecords: episode_subs[sub.episode_id][name].append(sub.to_dict(user)) return episode_subs
def handle(self, *args, **options): print "Creating Singletons" for patient in Patient.objects.all(): print 'Examining', patient for subclass in patient_subrecords(): if subclass._is_singleton: if subclass.objects.filter(patient=patient).count() == 0: print 'Creating', subclass subclass.objects.create(patient=patient) for episode in Episode.objects.all(): print 'Examining', episode for subclass in episode_subrecords(): if subclass._is_singleton: if subclass.objects.filter(episode=episode).count() == 0: print 'Creating', subclass subclass.objects.create(episode=episode) return
def to_dict(self, user, shallow=False, with_context=False): """ Serialisation to JSON for Episodes """ d = { 'id': self.id, 'active': self.active, 'date_of_admission': self.date_of_admission, 'discharge_date': self.discharge_date, 'consistency_token': self.consistency_token } if shallow: return d for model in patient_subrecords(): subrecords = model.objects.filter(patient_id=self.patient.id) d[model.get_api_name()] = [ subrecord.to_dict(user) for subrecord in subrecords ] for model in episode_subrecords(): subrecords = model.objects.filter(episode_id=self.id) d[model.get_api_name()] = [ subrecord.to_dict(user) for subrecord in subrecords ] d['tagging'] = self.tagging_dict(user) d['prev_episodes'] = [] d['next_episodes'] = [] if self.date_of_admission: eset = self.patient.episode_set d['prev_episodes'] = [ e.to_dict(user, shallow=True) for e in eset.filter( date_of_admission__lt=self.date_of_admission).order_by( 'date_of_admission') ] d['next_episodes'] = [ e.to_dict(user, shallow=True) for e in eset.filter( date_of_admission__gt=self.date_of_admission).order_by( 'date_of_admission') ] return d
class OptionAdmin(admin.ModelAdmin): ordering = ['name'] search_fields = ['name'] inlines = [SynonymInline] for model in LookupList.__subclasses__(): admin.site.register(model, OptionAdmin) admin.site.register(User, UserProfileAdmin) admin.site.register(models.Patient, PatientAdmin) admin.site.register(models.Episode, EpisodeAdmin) admin.site.register(models.Tagging, TaggingAdmin) for subclass in patient_subrecords(): if not subclass._meta.abstract: admin.site.register(subclass, PatientSubRecordAdmin) for subclass in episode_subrecords(): if not subclass._meta.abstract: admin.site.register(subclass, EpisodeSubRecordAdmin) admin.site.register(models.GP, MyAdmin) admin.site.register(models.CommunityNurse, MyAdmin) admin.site.register(models.ContactNumber, MyAdmin) admin.site.register(models.Team, TeamAdmin) admin.site.register(models.Role, MyAdmin) admin.site.register(models.Macro, MyAdmin)
def create_episode_singletons(sender, **kwargs): if kwargs['created']: episode = kwargs['instance'] for subclass in episode_subrecords(): if subclass._is_singleton: subclass.objects.create(episode=episode)