Ejemplo n.º 1
0
    def post(self, type, identifier):
        """
        ---
        summary: Add object attribute
        description: |
            Adds attribute to specified object.

            User must have `set` access to the attribute key or `adding_all_attributes` capability.
        security:
            - bearerAuth: []
        tags:
            - attribute
        parameters:
            - in: path
              name: type
              schema:
                type: string
                enum: [file, config, blob, object]
              description: Type of object
            - in: path
              name: identifier
              schema:
                type: string
              description: Object identifier
        requestBody:
            description: Attribute key and value
            content:
              application/json:
                schema: MetakeyItemRequestSchema
        responses:
            200:
                description: When metakey was added successfully
                content:
                  application/json:
                    schema: MetakeyListResponseSchema
            404:
                description: |
                    When object doesn't exist or user doesn't have access to this object.
                    When attribute key is not defined or user doesn't have privileges to set that one.
        """
        schema = MetakeyItemRequestSchema()
        obj = loads_schema(request.get_data(as_text=True), schema)

        db_object = access_object(type, identifier)
        if db_object is None:
            raise NotFound("Object not found")

        key = obj['key']
        value = obj['value']
        is_new = db_object.add_metakey(key, value)
        if is_new is None:
            raise NotFound(
                f"Metakey '{key}' is not defined or you have insufficient permissions to set it"
            )

        db.session.commit()
        db.session.refresh(db_object)
        metakeys = db_object.get_metakeys()
        schema = MetakeyListResponseSchema()
        return schema.dump({"metakeys": metakeys})
Ejemplo n.º 2
0
    def delete(self, type, identifier):
        """
        ---
        summary: Delete object attribute
        description: |
            Deletes attribute from specified object.

            User must have `removing_attributes` capability.
        security:
            - bearerAuth: []
        tags:
            - attribute
        parameters:
            - in: path
              name: type
              schema:
                type: string
                enum: [file, config, blob, object]
              description: Type of object
            - in: path
              name: identifier
              schema:
                type: string
              description: Object identifier
            - in: query
              name: key
              schema:
                type: string
              description: Key of attribute object to be deleted
              required: true
            - in: query
              name: value
              schema:
                type: string
              description: Value of attribute key object to be deleted
              required: true
        responses:
            200:
                description: When metakey was deleted successfully
            404:
                description: |
                    When object doesn't exist or user doesn't have access to this object.
                    When attribute key is not defined or user doesn't have privileges to set that one.
        """
        schema = MetakeyItemRequestSchema()
        obj = load_schema(request.args, schema)

        db_object = access_object(type, identifier)
        if db_object is None:
            raise NotFound("Object not found")

        key = obj['key']
        value = obj['value']

        deleted_object = db_object.remove_metakey(key, value)
        if deleted_object is False:
            raise NotFound(
                f"Metakey '{key}' is not defined or you have insufficient permissions to delete it"
            )
        db.session.commit()
Ejemplo n.º 3
0
    def post(self, type, identifier):
        """
        ---
        summary: Add object attribute
        description: |
            Adds attribute to specified object.

            User must have `set` access to the attribute key
            or `adding_all_attributes` capability.
        security:
            - bearerAuth: []
        tags:
            - deprecated
        parameters:
            - in: path
              name: type
              schema:
                type: string
                enum: [file, config, blob, object]
              description: Type of object
            - in: path
              name: identifier
              schema:
                type: string
              description: Object identifier
        requestBody:
            description: Attribute key and value
            content:
              application/json:
                schema: MetakeyItemRequestSchema
        responses:
            200:
                description: When metakey was added successfully
                content:
                  application/json:
                    schema: MetakeyListResponseSchema
            400:
                description: For karton attribute when value is not UUID
            404:
                description: |
                    When object doesn't exist or user doesn't have
                    access to this object.

                    When attribute key is not defined or user doesn't have
                    privileges to set that one.
            503:
                description: |
                    Request canceled due to database statement timeout.
        """
        schema = MetakeyItemRequestSchema()
        obj = loads_schema(request.get_data(as_text=True), schema)

        db_object = access_object(type, identifier)
        if db_object is None:
            raise NotFound("Object not found")

        key = obj["key"]
        value = obj["value"]

        if key == "karton" and not is_valid_uuid(value):
            raise BadRequest("'karton' attribute accepts only UUID values")

        is_new = db_object.add_attribute(key, value)
        if is_new is None:
            raise NotFound(
                f"Attribute '{key}' is not defined or you have "
                f"insufficient permissions to set it"
            )

        db.session.commit()
        db.session.refresh(db_object)
        metakeys = db_object.get_attributes()
        schema = MetakeyListResponseSchema()
        return schema.dump({"metakeys": metakeys})