def get_roles(user, obj):
    """
    Get a list of roles assigned to a user for a specific instance from the
    cache, or builds such a list if it is not found.
    """
    # get roles for the user, if present:
    roles = cache.get(roles_key(user, obj))
    if isinstance(roles, list):
        # Cache hit (a miss returns NoneType rather than an empty list)
        return roles
    else:
        # we need to recompute roles for this model
        user_roles = []
        if not hasattr(obj, 'relevant_roles'):
            raise RulesException(
                'Cannot build roles cache for %s instance. Did you forget to \
                define a "relevant_roles()" method on %s?' % (obj.__class__,
                                                              obj.__class__))

        relevant = obj.relevant_roles()
        for role in relevant:
            if role.is_member(user, obj):
                user_roles.append(role)
        cache.set(roles_key(user, obj), user_roles, 1*HOUR)
        return user_roles
dataset/ETHPy150Open chrisglass/django-rulez/rulez/rolez/cache_helper.py/get_roles/original