'Bulk lookups for cards
@param resource: the resource
@param records: the records as returned from S3Resource.select'

def prep(self, resource, records):

    db = current.db
    s3db = current.s3db
    tablename = resource.tablename
    if (tablename == 'deploy_alert'):
        record_ids = set((record['_row']['deploy_alert.id'] for record in records))
        htable = s3db.hrm_human_resource
        number_of_recipients = htable.id.count()
        rtable = s3db.deploy_alert_recipient
        alert_id = rtable.alert_id
        use_regions = self.use_regions
        if use_regions:
            otable = s3db.org_organisation
            region_id = otable.region_id
            fields = [alert_id, region_id, number_of_recipients]
            left = [htable.on((htable.id == rtable.human_resource_id)), otable.on((otable.id == htable.organisation_id))]
            groupby = [alert_id, region_id]
        else:
            fields = [alert_id, number_of_recipients]
            left = [htable.on((htable.id == rtable.human_resource_id))]
            groupby = [alert_id]
        query = (alert_id.belongs(record_ids) & (rtable.deleted != True))
        rows = current.db(query).select(left=left, groupby=groupby, *fields)
        recipient_numbers = {}
        for row in rows:
            alert = row[alert_id]
            if (alert in recipient_numbers):
                recipient_numbers[alert].append(row)
            else:
                recipient_numbers[alert] = [row]
        self.recipient_numbers = recipient_numbers
        if use_regions:
            represent = otable.region_id.represent
            represent.none = current.T('No    Region')
            region_ids = [row[region_id] for row in rows]
            self.region_names = represent.bulk(region_ids)
        else:
            self.region_names = {}
    elif (tablename == 'deploy_response'):
        dcount = self.dcount
        avgrat = self.avgrat
        deployed = self.deployed
        mission_id = None
        for record in records:
            raw = record['_row']
            human_resource_id = raw['hrm_human_resource.id']
            if human_resource_id:
                dcount[human_resource_id] = 0
                avgrat[human_resource_id] = None
            if (not mission_id):
                mission_id = raw['deploy_response.mission_id']
        hr_ids = dcount.keys()
        if hr_ids:
            table = s3db.deploy_assignment
            human_resource_id = table.human_resource_id
            deployment_count = table.id.count()
            query = (human_resource_id.belongs(hr_ids) & (table.deleted != True))
            rows = db(query).select(human_resource_id, deployment_count, groupby=human_resource_id)
            for row in rows:
                dcount[row[human_resource_id]] = row[deployment_count]
            query = ((human_resource_id.belongs(hr_ids) & (table.mission_id == mission_id)) & (table.deleted != True))
            rows = db(query).select(human_resource_id)
            for row in rows:
                deployed.add(row[human_resource_id])
            atable = s3db.hrm_appraisal
            htable = s3db.hrm_human_resource
            human_resource_id = htable.id
            average_rating = atable.rating.avg()
            query = ((((human_resource_id.belongs(hr_ids) & (htable.person_id == atable.person_id)) & (atable.deleted != True)) & (atable.rating != None)) & (atable.rating > 0))
            rows = db(query).select(human_resource_id, average_rating, groupby=human_resource_id)
            for row in rows:
                avgrat[row[human_resource_id]] = row[average_rating]
    elif (tablename == 'deploy_assignment'):
        record_ids = set((record['_row']['deploy_assignment.id'] for record in records))
        atable = s3db.hrm_appraisal
        ltable = s3db.deploy_assignment_appraisal
        query = ((ltable.assignment_id.belongs(record_ids) & (ltable.deleted != True)) & (atable.id == ltable.appraisal_id))
        rows = current.db(query).select(ltable.assignment_id, ltable.appraisal_id)
        appraisals = {}
        for row in rows:
            appraisals[row.assignment_id] = row.appraisal_id
        self.appraisals = appraisals
    return
