@@ -0,0 +1,3097 @@
+from collections import defaultdict%0Afrom datetime import date%0Aimport re%0A%0Afrom slumber.exceptions import HttpServerError%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.utils.text import slugify%0A%0Afrom candidates.popit import create_popit_api_object, popit_unwrap_pagination%0A%0Adef fix_joint_party_name(joint_party_name):%0A    return %7B%0A        'The Christian Party Christian Peoples Alliance':%0A        'Christian Party Christian Peoples Alliance'%0A    %7D.get(joint_party_name, joint_party_name)%0A%0Adef extract_number_from_id(party_id):%0A    m = re.search('%5Cd+', party_id)%0A    if m:%0A        return int(m.group(0), 10)%0A    return -1%0A%0Adef create_or_update_party(api, joint_party_name, sub_parties):%0A    party_ids = sorted(%0A        extract_number_from_id(p%5B'id'%5D)%0A        for p in sub_parties%0A    )%0A    registers = set(p%5B'register'%5D for p in sub_parties)%0A    if len(registers) %3E 1:%0A        raise Exception(%22Multiple registers found %22 + repr(registers))%0A    joint_party_id = 'joint-party:' + '-'.join(str(i) for i in party_ids)%0A    party_data = %7B%0A        'id': joint_party_id,%0A        'name': joint_party_name,%0A        'slug': slugify(joint_party_name),%0A        'classification': 'Party',%0A        'descriptions': %5B%5D,%0A        'dissolution_date': '9999-12-31',%0A        'register': next(iter(registers)),%0A    %7D%0A    try:%0A        api.organizations.post(party_data)%0A    except HttpServerError as e:%0A        if 'E11000' in e.content:%0A            # Duplicate Party Found%0A            api.organizations(joint_party_id).put(party_data)%0A        else:%0A            raise%0A%0Ajoint_description_re = re.compile(%0A    r'%5E(?P%3Cjoint_name%3E.*?) %5C(%5BjJ%5Doint %5BdD%5Descriptions? with (?P%3Csub_party%3E.*)%5C)'%0A)%0A%0A%0Aclass Command(BaseCommand):%0A    help = %22Create joint pseudo-parties based on parties with joint descriptions%22%0A%0A    def handle(self, **options):%0A        joint_party_to_sub_parties = defaultdict(list)%0A        api = create_popit_api_object()%0A        for party in popit_unwrap_pagination(%0A                api.organizations,%0A                embed='',%0A                per_page=200%0A        ):%0A            if party%5B'classification'%5D != 'Party':%0A                continue%0A            if 'dissolution_date' in party:%0A                dissolution_date = party%5B'dissolution_date'%5D%0A                if dissolution_date %3C str(date.today()):%0A                    continue%0A            for d in party.get('descriptions', %5B%5D):%0A                m = joint_description_re.search(d%5B'description'%5D)%0A                if not m:%0A                    continue%0A                joint_name = fix_joint_party_name(m.group('joint_name'))%0A                joint_party_to_sub_parties%5Bjoint_name%5D.append(party)%0A        for joint_name, sub_parties in joint_party_to_sub_parties.items():%0A            if len(sub_parties) %3C 2:%0A                message = 'Ignoring %22%7Bjoint_name%7D%22 (only made up of one party: %22%7Bsub_party%7D%22)'%0A                print message.format(%0A                    joint_name=joint_name,%0A                    sub_party=sub_parties%5B0%5D%5B'name'%5D%0A                )%0A                continue%0A            create_or_update_party(api, joint_name, sub_parties)%0A

Add a command to create joint parties based on party descriptions