Beispiel #1
0
    def get_record_schema(self, resource_cls, method):
        schema = super(ShareableViewSet,
                       self).get_record_schema(resource_cls, method)

        if method.lower() not in map(str.lower, self.validate_schema_for):
            return schema

        try:
            empty_allowed = False
            schema.deserialize({"data": {}})
            empty_allowed = True
        except colander.Invalid:
            pass

        if empty_allowed or method.lower() == 'patch':
            # Data is optional when patching permissions.
            schema.children[-1].missing = colander.drop

        allowed_permissions = resource_cls.permissions
        permissions_node = PermissionsSchema(missing=colander.drop,
                                             permissions=allowed_permissions,
                                             name='permissions')
        schema.add(permissions_node)

        # XXX: If Cornice wouldn't recreate the schema on the fly.
        # We could make sure using a validator that at least one of `data` or
        # `permissions` is provided.
        # There is a huge work in progress, with several pull-requests
        # addressing this issue:
        # https://github.com/mozilla-services/cornice/labels/schema-validation

        return schema
Beispiel #2
0
    def get_record_schema(self, resource_cls, method):
        """Return the Cornice schema for the given method.
        """
        if method.lower() == 'patch':
            resource_schema = SimpleSchema
        else:
            resource_schema = resource_cls.schema
            if hasattr(resource_cls, 'mapping'):
                message = "Resource `mapping` is deprecated, use `schema`"
                warnings.warn(message, DeprecationWarning)
                resource_schema = resource_cls.mapping.__class__

        try:
            # Check if empty record is allowed.
            # (e.g every schema fields have defaults)
            resource_schema().deserialize({})
        except colander.Invalid:
            schema_kw = dict(missing=colander.required)
        else:
            schema_kw = dict(default={}, missing=colander.drop)

        allowed_permissions = resource_cls.permissions

        payload_schema = StrictSchema()
        payload_schema.add(resource_schema(name='data', **schema_kw))
        payload_schema.add(
            PermissionsSchema(name='permissions',
                              missing=colander.drop,
                              permissions=allowed_permissions))
        return payload_schema
Beispiel #3
0
 def get_record_schema(self, resource_cls, method):
     """Return the Cornice schema for the given method.
     """
     record_schema = super(ShareableViewSet,
                           self).get_record_schema(resource_cls, method)
     allowed_permissions = resource_cls.permissions
     permissions = PermissionsSchema(name='permissions',
                                     missing=colander.drop,
                                     permissions=allowed_permissions)
     record_schema = record_schema.bind(permissions=permissions)
     return record_schema
Beispiel #4
0
 class PayloadSchema(StrictSchema):
     data = resource_schema(**schema_kw)
     permissions = PermissionsSchema(missing=colander.drop,
                                     permissions=allowed_permissions)