예제 #1
0
 def test_get_counter_does_not_return_spaces(self):
     obj = Mock()
     user = MockUser()
     roles_key(user, obj) # The first time, the counter == 0
     increment_counter(obj) # Now there should be a timestamp
     res = roles_key(user, obj)
     self.assertTrue(' ' not in res)
예제 #2
0
 def test_get_counter_does_not_return_spaces(self):
     obj = Mock()
     user = MockUser()
     roles_key(user, obj)  # The first time, the counter == 0
     increment_counter(obj)  # Now there should be a timestamp
     res = roles_key(user, obj)
     self.assertTrue(' ' not in res)
예제 #3
0
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