def get_audience_flags(self, current_audience):
        audience_flags = {}
        for item in AUDIENCE_TYPES.values():
            audience_flags["is_%s" % slugify(item['name']).replace('-', '_')] = (current_audience == item['name'])
            if 'code' in item:
                audience_flags["is_%s" % item['code']] = (current_audience == item['name'])

        return audience_flags
def _get_audience_field(audience_id, field, default=''):
    """
    Get a field for the given audience id passed
    """
    from audience.settings import AUDIENCE_TYPES
    try:
        return AUDIENCE_TYPES[int(audience_id)].get(field, default)
    except KeyError:
        index = int(audience_id - 1)
        keys = AUDIENCE_TYPES.keys()
        if index < len(keys):
            key = AUDIENCE_TYPES.keys()[int(audience_id - 1)]

            return AUDIENCE_TYPES[key].get(field, default)
    except ValueError:
        pass
    # The id wasn't passed. Lets see if we can find it by name
    for values in AUDIENCE_TYPES.values():
        if values['name'] == audience_id:
            if field in values:
                return values[field]
            else:
                return default
    return default
def audience(request):
    """
    Adds the current audience type dict into an ``audience`` context
    variable.

    Also puts flags for each audience in a ``is_FOO`` variable
    for easy testing of the current audience.
    """
    # QS_PARAM_NAME determines whether or not we are using a querystring parameter
    # to get the audience, or using the path.
    if QS_PARAM_NAME:
        aud_type = request.GET.get(QS_PARAM_NAME, DEFAULT_AUDIENCE)
        current_audience = AUDIENCE_TYPES[int(aud_type)]
    else:
        bits = request.META['PATH_INFO'].strip('/').split('/')
        if bits and bits[-1] == 'print':
            del bits[-1]
        if bits and bits[-1] in AUDIENCE_NAMES and AUDIENCE_NAMES[bits[-1]]['id'] in VALID_AUDIENCES:
            current_audience = AUDIENCE_NAMES[bits[-1]]
        elif bits and bits[-1] in AUDIENCE_CODES and AUDIENCE_CODES[bits[-1]]['id'] in VALID_AUDIENCES:
            current_audience = AUDIENCE_CODES[bits[-1]]
        else:
            current_audience = AUDIENCE_TYPES[DEFAULT_AUDIENCE]
    context = {}
    context['current_audience'] = current_audience.copy()
    context['default_audience'] = AUDIENCE_TYPES[DEFAULT_AUDIENCE].copy()
    context['VALID_AUDIENCES'] = VALID_AUDIENCES

    if hasattr(request, 'session'):
        request.session['AUDIENCE'] = current_audience.copy()

    audience_flags = {}
    for item in AUDIENCE_TYPES.values():
        audience_flags["is_%s" % slugify(item['name']).replace('-', '_')] = (current_audience['name'] == item['name'])
        audience_flags["is_%s" % item['code']] = (current_audience['name'] == item['name'])
    context.update(audience_flags)

    return context