def organisation_data_from_etree(self, tree): def find_text(tree, str): element = tree.find(str) if element is None: return '' return element.text.strip() if element.text else "" def location_data(location_tree): if location_tree is None: return [] iso_code = find_text(location_tree, 'iso_code').lower() country, created = Country.objects.get_or_create(**Country.fields_from_iso_code(iso_code)) country = country.id latitude = find_text(location_tree, 'latitude') or 0 longitude = find_text(location_tree, 'longitude') or 0 primary = True return [dict(latitude=latitude, longitude=longitude, country=country, primary=primary)] #id = find_text(tree, 'org_id') long_name = find_text(tree, 'name') name = long_name[:25] description = find_text(tree, 'description') url = find_text(tree, 'url') iati_type = find_text(tree, 'iati_organisation_type') new_organisation_type = int(iati_type) if iati_type else 22 organisation_type = Organisation.org_type_from_iati_type(new_organisation_type) locations = location_data(tree.find('location/object')) return dict( name=name, long_name=long_name, description=description, url=url, organisation_type=organisation_type, new_organisation_type=new_organisation_type, locations=locations )
def organisation_data_from_etree(self, tree): def find_text(tree, str): element = tree.find(str) if element is None: return '' return element.text.strip() if element.text else "" def location_data(location_tree): if location_tree is None: return [] iso_code = find_text(location_tree, 'iso_code').lower() country, created = Country.objects.get_or_create(**Country.fields_from_iso_code(iso_code)) country = country.id latitude = find_text(location_tree, 'latitude') or 0 longitude = find_text(location_tree, 'longitude') or 0 primary = True return [dict(latitude=latitude, longitude=longitude, country=country, primary=primary)] #id = find_text(tree, 'org_id') long_name = find_text(tree, 'name') name = long_name[:25] description = find_text(tree, 'description') url = find_text(tree, 'url') iati_type = find_text(tree, 'iati_organisation_type') new_organisation_type = int(iati_type) if iati_type else 22 organisation_type = Organisation.org_type_from_iati_type(new_organisation_type) locations = location_data(tree.find('location/object')) return dict( name=name, long_name=long_name, description=description, url=url, organisation_type=organisation_type, new_organisation_type=new_organisation_type, locations=locations )
def python_organisation(tree): """ Create a python data structure representing an organisation from the etree representation """ def location_data(location_tree): if location_tree is None: return [] iso_code = find_text(location_tree, 'iso_code').lower() if iso_code: country, created = Country.objects.get_or_create( **Country.fields_from_iso_code(iso_code)) country = country.id else: # TODO: error reporting return [] latitude = find_text(location_tree, 'latitude') or 0 longitude = find_text(location_tree, 'longitude') or 0 primary = True return [ dict( latitude=latitude, longitude=longitude, country=country, primary=primary, ) ] def import_logo(tree): logo = find_text(tree, 'logo') if logo: image = ImageImporter(logo) image.get_image() return image.to_base64() return None long_name = find_text(tree, 'name') name = long_name[:25] description = find_text(tree, 'description') url = find_text(tree, 'url') iati_type = find_text(tree, 'iati_organisation_type') new_organisation_type = int(iati_type) if iati_type else 22 organisation_type = Organisation.org_type_from_iati_type( new_organisation_type) content_owner = RAIN_ORG_ID locations = location_data(tree.find('locations/object')) org_dict = dict( name=name, long_name=long_name, description=description, url=url, organisation_type=organisation_type, new_organisation_type=new_organisation_type, content_owner=content_owner, locations=locations, ) logo = import_logo(tree) if logo: org_dict['logo'] = logo return org_dict
def python_organisation(tree): """ Create a python data structure representing an organisation from the etree representation """ def location_data(location_tree): if location_tree is None: return [] iso_code = find_text(location_tree, 'iso_code').lower() if iso_code: country, created = Country.objects.get_or_create(**Country.fields_from_iso_code(iso_code)) country = country.id else: # TODO: error reporting return [] latitude = find_text(location_tree, 'latitude') or 0 longitude = find_text(location_tree, 'longitude') or 0 primary = True return [dict( latitude=latitude, longitude=longitude, country=country, primary=primary, )] def import_logo(tree): logo = find_text(tree, 'logo') if logo: image = ImageImporter(logo) image.get_image() return image.to_base64() return None long_name = find_text(tree, 'name') name = long_name[:25] description = find_text(tree, 'description') url = find_text(tree, 'url') iati_type = find_text(tree, 'iati_organisation_type') new_organisation_type = int(iati_type) if iati_type else 22 organisation_type = Organisation.org_type_from_iati_type(new_organisation_type) content_owner = RAIN_ORG_ID locations = location_data(tree.find('locations/object')) org_dict = dict( name=name, long_name=long_name, description=description, url=url, organisation_type=organisation_type, new_organisation_type=new_organisation_type, content_owner=content_owner, locations=locations, ) logo = import_logo(tree) if logo: org_dict['logo'] = logo return org_dict
def org_data_from_xml(org_etree): # keys are Organisation field names, values are xpath expressions for getting those values from the org_etree xpaths = dict( name='name', description='description', url='url', new_organisation_type='iati_organisation_type', ) # get the raw data from the org_etree org_data = data_from_xpaths(xpaths, org_etree) # transform data org_data['long_name'] = org_data['name'] org_data['name'] = org_data['name'][:25] org_data['organisation_type'] = Organisation.org_type_from_iati_type(int(org_data['new_organisation_type'])) return org_data
def organisation_data_from_etree(self, tree): def find_text(tree, str): element = tree.find(str) if element is None: return '' return element.text.strip() if element.text else "" long_name = find_text(tree, 'name') name = long_name[:25] description = find_text(tree, 'description') url = find_text(tree, 'url') iati_type = find_text(tree, 'iati_organisation_type') new_organisation_type = int(iati_type) if iati_type else 22 organisation_type = Organisation.org_type_from_iati_type(new_organisation_type) return dict( name=name, long_name=long_name, description=description, url=url, organisation_type=organisation_type, new_organisation_type=new_organisation_type, )