def handle(self, **options): try: # First create all the political parties: for party_data in PARTY_DATA.all_party_data: create_or_update( self.api.organizations, party_data, ) # Now we create the organizations that all the posts are # associated with: for election_data in Election.objects.all(): create_or_update( self.api.organizations, { 'id': election_data.organization_id, 'name': election_data.organization_name, } ) except (HttpServerError, HttpClientError) as http_error: print "The body of the error was:", http_error.content raise
def handle_mapit_type(self, election, election_data, mapit_type, **options): post_label_format = _('{post_role} for {area_name}') if options['post_label']: post_label_format = options['post_label'] mapit_tuple = (mapit_type, election_data['mapit_generation']) for id, area in MAPIT_DATA.areas_by_id[mapit_tuple].items(): post_id = AREA_POST_DATA.get_post_id( election, mapit_type, id ) role = election_data['for_post_role'] area_mapit_url = settings.MAPIT_BASE_URL + 'area/' + str(area['id']) post_data = { 'role': role, 'id': post_id, 'label': post_label_format.format( post_role=role, area_name=area['name'] ), 'area': { 'name': area['name'], 'id': 'mapit:' + str(area['id']), 'identifier': area_mapit_url, }, 'organization_id': election_data['organization_id'], } create_or_update(self.api.posts, post_data)
def handle(self, **options): # First create all the political parties: for party_data in PARTY_DATA.all_party_data: create_or_update( self.api.organizations, party_data, ) # Now we create the organizations that all the posts are # associated with: for election, election_data in settings.ELECTIONS.items(): create_or_update( self.api.organizations, { 'id': election_data['organization_id'], 'name': election_data['organization_name'], })
def handle_mapit_type(self, election, election_data, mapit_type): mapit_tuple = (mapit_type, election_data['mapit_generation']) for id, area in MAPIT_DATA.areas_by_id[mapit_tuple].items(): post_id = AREA_POST_DATA.get_post_id( election, mapit_type, id ) role = election_data['for_post_role'] area_mapit_url = settings.MAPIT_BASE_URL + 'area/' + str(area['id']) post_data = { 'role': role, 'id': post_id, 'label': role + u' por ' + area['name'], 'area': { 'name': area['name'], 'id': 'mapit:' + str(area['id']), 'identifier': area_mapit_url, }, 'organization_id': election_data['organization_id'], } create_or_update(self.api.posts, post_data)
def handle(self, **options): # First create all the political parties: for party_data in PARTY_DATA.all_party_data: create_or_update( self.api.organizations, party_data, ) # Now we create the organizations that all the posts are # associated with: for election, election_data in settings.ELECTIONS.items(): create_or_update( self.api.organizations, { 'id': election_data['organization_id'], 'name': election_data['organization_name'], } )
def handle(self, **options): try: for election, election_data in settings.ELECTIONS.items(): if election == 'presidentes-argentina-paso-2015': create_or_update( self.api.posts, { 'id': 'presidente', 'label': election_data['for_post_role'], 'role': election_data['for_post_role'], 'area': { 'name': 'Argentina', 'id': 'mapit:0', 'identifier': settings.MAPIT_BASE_URL + 'area/0', }, 'organization_id': election_data['organization_id'], } ) else: for mapit_type in election_data['mapit_types']: self.handle_mapit_type(election, election_data, mapit_type) except (HttpServerError, HttpClientError) as hse: print "The body of the error was:", hse.content raise
def handle(self, **options): # Choose an appropriate format for the post labels: post_label_format = _('{post_role} for {area_name}') if options['post_label']: post_label_format = options['post_label'] # Find all the source data for each post: post_id_to_all_data = defaultdict(list) for data in all_posts_in_all_elections(): post_id_to_all_data[data['post_id']].append(data) # Now we have a mapping from each post to all the data # (e.g. elections, areas) it's associated with. Now try to # reconcile that data and create or update the posts: try: for post_id, data_list in post_id_to_all_data.items(): # Since the post's creation can be required by multiple # elections with different metadata, there might be # contradictory values for the properties we want to set # on the post. To detect this possibility, we use # get_unique_value to extract these values: role = get_unique_value( d['election_data'].for_post_role for d in data_list ) organization_id = get_unique_value( d['election_data'].organization_id for d in data_list ) area_id = get_unique_value(d['area_id'] for d in data_list) # We can have, however, have multiple values in the # 'elections' attribute, so it's fine to collect all of them: elections = sorted(set(d['election'] for d in data_list)) # At this stage we know there's a unique area (since # otherwise there wouldn't be a unique area_id) so we # don't need to check the uniqueness of area, or the # values derived from area. area = data_list[0]['area'] area_mapit_url = \ settings.MAPIT_BASE_URL + 'area/' + str(area['id']) label = post_label_format.format( post_role=role, area_name=area['name'] ) post_data = { 'elections': elections, 'role': role, 'id': post_id, 'label': label, 'area': { 'name': area['name'], 'id': 'mapit:' + str(area_id), 'identifier': area_mapit_url, }, 'organization_id': organization_id, } create_or_update(self.api.posts, post_data) except (HttpServerError, HttpClientError) as hse: print "The body of the error was:", hse.content raise