def _computed(self, attribute, **query_kwargs):
        """
            Returns this instance's attribute's related values or through values (for attributes with an explicit through class)
             or the donor's if this instance hasn't overridden its values
        :param attribute: 'db_entities', etc. Not the through attribute name (e.g. dbentities_set)
        :param **query_kwargs: optionally specify query arguments to use with filter() on the results.
            One special param is with_deleted, which enables deleted objects to return, normally omited
        :return: this attribute's collection or its parents, with optional filtering applied after
        """
        resolved_attribute = self.through_attribute(self.many_field(attribute)) if has_explicit_through_class(self, attribute) else attribute
        params = (dict(deleted=query_kwargs.get('deleted', False)) if not query_kwargs.get('with_deleted') else dict())
        q_kwargs = remove_keys(query_kwargs, ['with_deleted'])

        if self.donor():
            results = get_list_or_if_empty(
                # Get instance's own Many items.
                # The instance will either have the donor's items added to it or not. If they are added this will
                # have items, obviously. If the instance has it's own items. The donor's items will already be here
                # as well.
                self._filter_computed(
                    getattr(self, resolved_attribute).filter(**params),
                    **q_kwargs),
                # If none exist get donor's
                lambda: self.donor()._computed(attribute, **q_kwargs)
            )
            #if self.donor().key=='layer_library__default':
                #name = self.donor().name
                #print '1 %s: %s' % (self.name, ', '.join(map(lambda x: x.db_entity_key, self._filter_computed(getattr(self, resolved_attribute).filter(**params), **q_kwargs))))
                #print '2 %s: %s' % (name, ', '.join(map(lambda x: x.db_entity_key, self.donor()._computed(attribute, **q_kwargs))))
                #print '3 %s: %s' % (self.name, ', '.join(map(lambda x: x.db_entity_key, results)))
            return results
        else:
            # No donor is defined so just consider this instance's items
            return self._filter_computed(getattr(self, resolved_attribute).filter(**params), **q_kwargs)
    def test_permissions(self):

        for configuration in self.test_configuration:
            instance = configuration['instance']
            for group_name, permission_key in configuration['groups'].items():
                group = Group.objects.get(name=group_name)
                class_permissions_keys = PermissionKey.permission_keys(
                    permission_key,
                    instance.__class__) if permission_key else []
                for permission in class_permissions_keys:
                    # See if the group has all of the expected permissions to the instance
                    assert ObjectPermissionChecker(group).has_perm(permission, instance), \
                        "Group %s expected to have %s permission to instance %s with id %s and key %s but doesn't. %s" % \
                        (group.name,
                         permission,
                         instance,
                         instance.id,
                         self.key_class.Fab.remove(self.instance_key_lambda(instance)),
                         get_list_or_if_empty(
                             ObjectPermissionChecker(group).get_perms(instance),
                             lambda: 'It has no permissions',
                             lambda lisst: 'It has permissions: %s' % ', '.join(lisst)
                         ))
                for nopermission in set(
                        PermissionKey.permission_keys(
                            PermissionKey.ALL,
                            instance.__class__)) - set(class_permissions_keys):
                    # Make sure the other permissions are not set
                    assert not ObjectPermissionChecker(group).has_perm(nopermission, instance), \
                        "Group %s should not have %s permission to instance %s with id %s and key %s but does. %s" % \
                        (group.name,
                         nopermission,
                         instance,
                         instance.id,
                         self.key_class.Fab.remove(self.instance_key_lambda(instance)),
                         'It has permissions: %s' % ', '.join(ObjectPermissionChecker(group).get_perms(instance))
                        )

            # Test that any configured users are in the expected group
            for group_name, user_name in configuration.get('users',
                                                           {}).items():
                user = User.objects.get(username=user_name)
                group = Group.objects.get(name=group_name)
                assert_equal(
                    user.groups.filter(name=group).count(), 1,
                    "Expected user %s to be in group %s, but it wasn't. User is in the following groups %s"
                    % (user_name, group_name, ', '.join(
                        user.groups.values_list('name', flat=True))))
Beispiel #3
0
    def test_permissions(self):

        for configuration in self.test_configuration:
            instance = configuration['instance']
            for group_name, permission_key in configuration['groups'].items():
                group = Group.objects.get(name=group_name)
                class_permissions_keys = PermissionKey.permission_keys(permission_key, instance.__class__) if permission_key else []
                for permission in class_permissions_keys:
                    # See if the group has all of the expected permissions to the instance
                    assert ObjectPermissionChecker(group).has_perm(permission, instance), \
                        "Group %s expected to have %s permission to instance %s with id %s and key %s but doesn't. %s" % \
                        (group.name,
                         permission,
                         instance,
                         instance.id,
                         self.key_class.Fab.remove(self.instance_key_lambda(instance)),
                         get_list_or_if_empty(
                             ObjectPermissionChecker(group).get_perms(instance),
                             lambda: 'It has no permissions',
                             lambda lisst: 'It has permissions: %s' % ', '.join(lisst)
                         ))
                for nopermission in set(PermissionKey.permission_keys(PermissionKey.ALL, instance.__class__)) - set(class_permissions_keys):
                # Make sure the other permissions are not set
                    assert not ObjectPermissionChecker(group).has_perm(nopermission, instance), \
                        "Group %s should not have %s permission to instance %s with id %s and key %s but does. %s" % \
                        (group.name,
                         nopermission,
                         instance,
                         instance.id,
                         self.key_class.Fab.remove(self.instance_key_lambda(instance)),
                         'It has permissions: %s' % ', '.join(ObjectPermissionChecker(group).get_perms(instance))
                        )

            # Test that any configured users are in the expected group
            for group_name, user_name in configuration.get('users', {}).items():
                user = User.objects.get(username=user_name)
                group = Group.objects.get(name=group_name)
                assert_equal(user.groups.filter(name=group).count(), 1,
                             "Expected user %s to be in group %s, but it wasn't. User is in the following groups %s" %
                             (user_name, group_name, ', '.join(user.groups.values_list('name', flat=True))))
 def _computed_related(self, attribute, **query_kwargs):
     """
         Like _computed, but returns the related item of the through class instances for attributes having a through class. Attributes without an explict through class behave just like _computed()
     :param attribute: 'db_entities', etc. Not the through attribute name (e.g. dbentityinterest_set)
     :param query_kwargs: optional args to filter the results
     :return: The related class instances
     """
     modified_query_kwargs = remove_keys(query_kwargs, ['with_deleted'])
     deleted_kwargs = (dict(deleted=False) if not query_kwargs.get('with_deleted') else dict())
     if self.donor():
         return get_list_or_if_empty(
             # Get instance's own Many items
             self._filter_computed(
                 getattr(self, attribute).filter(**deleted_kwargs),
                 **modified_query_kwargs),
             # If none exist get donor's
             lambda: self.donor()._computed_related(attribute, **modified_query_kwargs))
     else:
         # No donor is defined so just consider this instance's items
         return self._filter_computed(
             getattr(self, attribute).filter(**deleted_kwargs),
             **modified_query_kwargs)