@@ -0,0 +1,1202 @@
+%22%22%22%0ACreate singletons that may have been dropped%0A%22%22%22%0Aimport collections%0Aimport json%0Afrom optparse import make_option%0A%0Afrom django.contrib.contenttypes.models import ContentType%0Afrom django.core.management.base import BaseCommand%0A%0Afrom opal.models import Patient, Episode, PatientSubrecord, EpisodeSubrecord%0A%0Aclass Command(BaseCommand):%0A    %0A    def handle(self, *args, **options):%0A        print %22Creating Singletons%22%0A        for patient in Patient.objects.all():%0A            print 'Examining', patient%0A            for subclass in PatientSubrecord.__subclasses__():%0A                if subclass._is_singleton:%0A                    if subclass.objects.filter(patient=patient).count() == 0:%0A                        print 'Creating', subclass%0A                        subclass.objects.create(patient=patient)%0A        for episode in Episode.objects.all():%0A            print 'Examining', episode%0A            for subclass in EpisodeSubrecord.__subclasses__():%0A                if subclass._is_singleton:%0A                    if subclass.objects.filter(episode=episode).count() == 0:%0A                        print 'Creating', subclass%0A                        subclass.objects.create(episode=episode)%0A        return%0A

Add a management command to create trailing singletons.