@@ -0,0 +1,2068 @@
+from django.core.management.base import BaseCommand%0Afrom django.db import transaction%0Afrom organisations.models import OrganisationDivisionSet, OrganisationDivision%0A%0A%0Aclass Command(BaseCommand):%0A    help = (%0A        %22Copy all of the division and geography objects from one DivisionSet to another%22%0A    )%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument(%0A            %22src_id%22, action=%22store%22, help=%22PK for the source DivisionSet%22%0A        )%0A        parser.add_argument(%0A            %22dst_id%22, action=%22store%22, help=%22PK for the destination DivisionSet%22%0A        )%0A%0A    @transaction.atomic%0A    def copy_divsions(self, old_divset_id, new_divset_id):%0A        try:%0A            old_divset = OrganisationDivisionSet.objects.get(pk=old_divset_id)%0A        except OrganisationDivisionSet.DoesNotExist:%0A            raise Exception(%22Invalid Source DivisionSet%22)%0A        try:%0A            new_divset = OrganisationDivisionSet.objects.get(pk=new_divset_id)%0A        except OrganisationDivisionSet.DoesNotExist:%0A            raise Exception(%22Invalid Destination DivisionSet%22)%0A%0A        if len(new_divset.divisions.all()) %3E 0:%0A            raise Exception(%22Target DivisionSet must be empty%22)%0A%0A        # copy the divisions%0A        for div in old_divset.divisions.all():%0A            div.pk = None%0A            div.divisionset = new_divset%0A            div.save()%0A%0A        # copy the geographies%0A        geographies = %5B%0A            (div.official_identifier, div.geography)%0A            for div in old_divset.divisions.all()%0A        %5D%0A        for gss, geog in geographies:%0A            div = OrganisationDivision.objects.get(%0A                official_identifier=gss, divisionset=new_divset%0A            )%0A%0A            geog.pk = None%0A            geog.division_id = div.id%0A            geog.save()%0A            # attach it to the target division%0A            div.geography = geog%0A            div.save()%0A%0A        assert len(old_divset.divisions.all()) == len(new_divset.divisions.all())%0A%0A    def handle(self, *args, **options):%0A        self.copy_divsions(options%5B%22src_id%22%5D, options%5B%22dst_id%22%5D)%0A

add management command for copying divisions from one DivSet to another