def get_effective_theme(rattail_config, theme=None, session=None):
    """
    Validates and returns the "effective" theme.  If you provide a theme, that
    will be used; otherwise it is read from database setting.
    """
    from rattail.db import api
    if not theme:
        theme = api.get_setting(session, 'tailbone.theme') or 'default'
    available = rattail_config.getlist('tailbone', 'themes', default=['bobcat']
        )
    available.append('default')
    if theme not in available:
        raise ValueError('theme not available: {}'.format(theme))
    return theme
