def ubatch_to_csv(batch):
    """
    Utility function to convert a batch of APIUser data to CSV.
    """
    permkey = 'permissions_dict'
    fields = [k for k in batch[0].keys() if k != permkey]
    fields.extend(batch[0][permkey].keys())
    return '{}\n{}'.format(','.join(fields), '\n'.join([
        ','.join([str(r.get(f, r[permkey].get(f, None))) for f in fields])
            for r in batch
    ]))