'Called when users are imported from CSV
Lookups Pseudo-reference Integer fields from Names
e.g.:
auth_membership.pe_id from organisation.name=<Org Name>'

def s3_import_prep(self, data):

    db = current.db
    s3db = current.s3db
    set_record_owner = self.s3_set_record_owner
    update_super = s3db.update_super
    otable = s3db.org_organisation
    btable = s3db.org_organisation_branch
    (resource, tree) = data
    ORG_ADMIN = (not self.s3_has_role('ADMIN'))
    TRANSLATE = current.deployment_settings.get_L10n_translate_org_organisation()
    if TRANSLATE:
        ltable = s3db.org_organisation_name
    def add_org(name, parent=None):
        '    Helper    to    add    a    New    Organisation    '
        organisation_id = otable.insert(name=name)
        record = Storage(id=organisation_id)
        update_super(otable, record)
        set_record_owner(otable, organisation_id)
        if parent:
            records = db((otable.name == parent)).select(otable.id)
            if (len(records) == 1):
                link_id = btable.insert(organisation_id=records.first().id, branch_id=organisation_id)
                onaccept = s3db.get_config('org_organisation_branch', 'onaccept')
                callback(onaccept, Storage(vars=Storage(id=link_id)))
            elif (len(records) > 1):
                current.log.debug(('Cannot    set    branch    link    for    new    Organisation    %s    as    there    are    multiple    matches    for    parent    %s' % (name, parent)))
            else:
                parent_id = otable.insert(name=parent)
                update_super(otable, Storage(id=parent_id))
                set_record_owner(otable, parent_id)
                link_id = btable.insert(organisation_id=parent_id, branch_id=organisation_id)
                onaccept = s3db.get_config('org_organisation_branch', 'onaccept')
                callback(onaccept, Storage(vars=Storage(id=link_id)))
        return (organisation_id, record.pe_id)
    def org_lookup(org_full):
        '    Helper    to    lookup    an    Organisation    '
        if ('+BRANCH+' in org_full):
            (parent, org) = org_full.split('+BRANCH+')
        else:
            parent = None
            org = org_full
        query = ((otable.name.lower() == org.lower()) & (otable.deleted != True))
        if parent:
            btable = s3db.org_organisation_branch
            ptable = db.org_organisation.with_alias('org_parent_organisation')
            query &= (((ptable.name == parent) & (btable.organisation_id == ptable.id)) & (btable.branch_id == otable.id))
        records = db(query).select(otable.id, otable.pe_id, limitby=(0, 2))
        if (len(records) == 1):
            record = records.first()
            organisation_id = record.id
            pe_id = record.pe_id
        elif (len(records) > 1):
            current.log.debug(('Cannot    set    Organisation    %s    for    user    as    there    are    multiple    matches' % org))
            organisation_id = ''
            pe_id = ''
        elif TRANSLATE:
            query = (((ltable.name_l10n.lower() == org.lower()) & (ltable.organisation_id == otable.id)) & (ltable.deleted != True))
            records = db(query).select(otable.id, otable.pe_id, limitby=(0, 2))
            if (len(records) == 1):
                record = records.first()
                organisation_id = record.id
                pe_id = record.pe_id
            elif (len(records) > 1):
                current.log.debug(('Cannot    set    Organisation    %s    for    user    as    there    are    multiple    matches' % org))
                organisation_id = ''
                pe_id = ''
            elif ORG_ADMIN:
                current.log.debug(('Cannot    create    new    Organisation    %s    as    ORG_ADMIN    cannot    create    new    Orgs    during    User    Imports' % org))
                organisation_id = ''
                pe_id = ''
            else:
                (organisation_id, pe_id) = add_org(org, parent)
        elif ORG_ADMIN:
            current.log.debug(('Cannot    create    new    Organisation    %s    as    ORG_ADMIN    cannot    create    new    Orgs    during    User    Imports' % org))
            organisation_id = ''
            pe_id = ''
        else:
            (organisation_id, pe_id) = add_org(org, parent)
        return (organisation_id, pe_id)
    elements = tree.getroot().xpath("/s3xml//resource[@name='auth_membership']/data[@field='pe_id']")
    looked_up = dict(org_organisation={})
    for element in elements:
        pe_string = element.text
        if (pe_string and ('=' in pe_string)):
            (pe_type, pe_value) = pe_string.split('=')
            (pe_tablename, pe_field) = pe_type.split('.')
            if ((pe_tablename in looked_up) and (pe_value in looked_up[pe_tablename])):
                element.text = looked_up[pe_tablename][pe_value]['pe_id']
                continue
            if (pe_tablename == 'org_organisation'):
                (record_id, pe_id) = org_lookup(pe_value)
            else:
                table = s3db[pe_tablename]
                if (pe_tablename not in looked_up):
                    looked_up[pe_tablename] = {}
                record = db((table[pe_field] == pe_value)).select(table.id, table.pe_id, limitby=(0, 1)).first()
                if record:
                    record_id = record.id
                else:
                    record_id = table.insert(**{pe_field: pe_value})
                    record = Storage(id=record_id)
                    update_super(table, record)
                    set_record_owner(table, record_id)
                pe_id = record.pe_id
            new_value = str(pe_id)
            element.text = new_value
            looked_up[pe_tablename][pe_value] = dict(pe_id=new_value, id=str(record_id))
