def with_role(self, roles=ANY, target=ANY, force_separate=False):
        """
        This filters permission agents by the given target.
        """
        validate_roles_with_targets(roles, target)

        return self._query_perms(
            roles=roles,
            get_related_prefixes=get_related_agent_prefixes,
            perms_name='agent_perms',
            force_separate=force_separate,
            target=target)
示例#2
0
def add_perm(role, agent, target):
    """
    Adds a single Permission matching the arguments.
    Accepts role, agent, and target kwargs.
    If the Permission already exists, it will retrieve it instead.
    """
    # Make sure we're not adding bad data
    validate_roles_with_targets(role, target)

    # If it already exists, this is a duplicate, so ignore it.
    query_kwargs = get_single_crud_kwargs(role, agent, target)
    return Permission.objects.get_or_create(**query_kwargs)[0]
    def test_validate_roles_with_targets(self):
        zoo = Zoo.objects.first()
        exhibit = Exhibit.objects.first()

        # Singular

        validate_roles_with_targets('zoo.open', zoo)

        with self.assertRaises(ValueError):
            validate_roles_with_targets('zoo.open', exhibit)

        # Plural

        validate_roles_with_targets(['zoo.open'], [zoo])

        with self.assertRaises(ValueError):
            validate_roles_with_targets(['zoo.open'], [exhibit])