def _upsert_organizations(self, orgs): """ :param orgs: dict mapping ``{org_name : CkanOrganization()}`` :return: a map of source/ckan ids of organizations :rtype: IDMap """ idmap = IDMap() for org_name, org in orgs.iteritems(): if not isinstance(org, CkanOrganization): raise TypeError("Expected CkanOrganization, got {0!r}" .format(type(org))) if org.name is None: org.name = org_name if org.name != org_name: raise ValueError("Mismatching org name!") try: ckan_org = self._client.get_organization_by_name( org_name, allow_deleted=True) except HTTPError, e: if e.status_code != 404: raise # We need to create the org org.id = None org.state = 'active' created_org = self._client.create_organization(org) idmap.add(IDPair(source_id=org.name, ckan_id=created_org.id)) else: # We only want to update if state != 'active' org_id = ckan_org.id if self._conf['organization_merge_strategy'] == 'update': # If merge strategy is 'update', we should update # the group. org.state = 'active' org.id = ckan_org.id updated_org = self._client.update_organization(org) org_id = updated_org.id elif org.state != 'active': # We only want to update the **original** org to set it # as active, but preserving original values. ckan_org.state = 'active' updated_org = self._client.update_organization(ckan_org) org_id = updated_org.id idmap.add(IDPair(source_id=org_name, ckan_id=org_id))
def _upsert_groups(self, groups): """ :param groups: dict mapping ``{org_name : CkanGroup()}`` :return: a map of source/ckan ids of groups :rtype: IDMap """ idmap = IDMap() for group_name, group in groups.iteritems(): if not isinstance(group, CkanGroup): raise TypeError("Expected CkanGroup, got {0!r}" .format(type(group))) if group.name is None: group.name = group_name if group.name != group_name: raise ValueError("Mismatching group name!") try: ckan_group = self._client.get_group_by_name( group_name, allow_deleted=True) except HTTPError, e: if e.status_code != 404: raise # We need to create the group group.id = None group.state = 'active' created_group = self._client.create_group(group) idmap.add(IDPair(source_id=group.name, ckan_id=created_group.id)) else: # The group already exist. It might be logically # deleted, but we don't care -> just update and # make sure it is marked as active. # todo: make sure we don't need to preserve users and stuff, # otherwise we need to workaround that in hi-lev client group_id = ckan_group.id if self._conf['group_merge_strategy'] == 'update': # If merge strategy is 'update', we should update # the group. group.state = 'active' group.id = ckan_group.id updated_group = self._client.update_group(group) group_id = updated_group.id elif group.state != 'active': # We only want to update the **original** group to set it # as active, but preserving original values. ckan_group.state = 'active' updated_group = self._client.update_group(ckan_group) group_id = updated_group.id idmap.add(IDPair(source_id=group.name, ckan_id=group_id))