Exemplo n.º 1
0
def unlock_asset(asset_id):
    docs = request.json
    unlock_asset_response = get_sams_client().assets.unlock_asset(
        item_id=asset_id, external_user_id=get_user_id(True), external_session_id=get_auth()["_id"], docs=docs
    )
    if unlock_asset_response.status_code == 200:
        push_notification(
            "sams:asset:unlock_asset",
            item_id=asset_id,
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=unlock_asset_response.json()["_etag"],
            extension="sams",
        )
    return unlock_asset_response.json(), unlock_asset_response.status_code
Exemplo n.º 2
0
def delete(item_id):
    """
    Uses item_id and deletes the corresponding asset
    """
    try:
        etag = request.headers["If-Match"]
    except KeyError:
        raise SuperdeskApiError.badRequestError(
            "If-Match field missing in header")

    if get_attachments_from_asset_id(item_id).count():
        raise SuperdeskApiError.badRequestError(
            _("Asset is attached to a news item, cannot delete"))

    delete_response = get_sams_client().assets.delete(
        item_id=item_id, headers={"If-Match": etag})
    if delete_response.status_code != 204:
        return delete_response.json(), delete_response.status_code
    if delete_response.status_code == 204:
        push_notification(
            "sams:asset:deleted",
            item_id=item_id,
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            extension="sams",
        )
    return "", delete_response.status_code
Exemplo n.º 3
0
def update(item_id):
    """
    Uses item_id and updates the corresponding asset
    """
    try:
        etag = request.headers["If-Match"]
    except KeyError:
        raise SuperdeskApiError.badRequestError("If-Match field missing in header")

    if request.files.get("binary"):
        # The binary data was supplied so this must be a multipart request
        # Get the updates from the `request.form` attribute
        files = {"binary": request.files["binary"]}
        updates = request.form.to_dict()
    else:
        # Only the metadata was supplied so this must be a standard JSON request
        # Get the updates from the `request.get_json` function
        files = {}
        updates = request.get_json()

    update_response = get_sams_client().assets.update(
        item_id=item_id, updates=updates, headers={"If-Match": etag}, files=files, external_user_id=get_user_id(True)
    )
    if update_response.status_code == 200:
        push_notification(
            "sams:asset:updated",
            item_id=update_response.json()["_id"],
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=update_response.json()["_etag"],
            extension="sams",
        )
    return update_response.json(), update_response.status_code
Exemplo n.º 4
0
def unlock_asset_by_user(user_id, session_id):
    unlock_asset_response = get_sams_client().assets.unlock_assets_by_user(
        external_user_id=user_id, external_session_id=session_id)
    if unlock_asset_response.status_code == 200:
        push_notification("sams:asset:session_unlock",
                          user_id=get_user_id(True),
                          session_id=get_auth()["_id"],
                          extension="sams")
    return unlock_asset_response.status_code
Exemplo n.º 5
0
def create():
    """
    Creates new Asset
    """
    files = {"binary": request.files["binary"]}
    docs = request.form.to_dict()
    post_response = get_sams_client().assets.create(docs=docs, files=files, external_user_id=get_user_id(True))
    if post_response.status_code == 201:
        push_notification(
            "sams:asset:created",
            item_id=post_response.json()["_id"],
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=post_response.json()["_etag"],
            extension="sams",
        )
    return post_response.json(), post_response.status_code
Exemplo n.º 6
0
def create():
    """
    Creates new sets
    """
    docs = request.get_json()
    post_response = get_sams_client().sets.create(
        docs=docs, external_user_id=get_user_id(True))
    if post_response.status_code == 201:
        push_notification(
            "sams:set:created",
            item_id=post_response.json()["_id"],
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=post_response.json()["_etag"],
            extension="sams",
        )
    return post_response.json(), post_response.status_code
Exemplo n.º 7
0
def create():
    """
    Creates new Asset
    """
    files = {"binary": request.files["binary"]}
    docs = request.form.to_dict()
    sams_client = get_sams_client()
    post_response = sams_client.assets.create(
        docs=docs, files=files, external_user_id=get_user_id(True))
    response = post_response.json()
    if post_response.status_code == 201:
        if response.get("mimetype", "").startswith("image/"):

            # Create renditions.
            renditions = [k for k in app.config["RENDITIONS"]["sams"].keys()]
            for rendition in renditions:
                dimensions = app.config["RENDITIONS"]["sams"][rendition]
                rendition_response = sams_client.images.generate_rendition(
                    response["_id"],
                    width=dimensions.get("width"),
                    height=dimensions.get("height"),
                    name=rendition,
                    keep_proportions=True,
                )

                if not rendition_response.ok:
                    # We want to continue, even if SAMS failed to generate the rendition
                    # Instead we just log the error here and continue
                    error_json = rendition_response.json()
                    error_code = rendition_response.status_code
                    description = error_json.get(
                        "description") or f"Error [{error_code}]"
                    logger.error(
                        f"Failed to generate SAMS image rendition: {description}"
                    )

        push_notification(
            "sams:asset:created",
            item_id=response["_id"],
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=response["_etag"],
            extension="sams",
        )
    return response, post_response.status_code
Exemplo n.º 8
0
def delete(item_id):
    """
    Uses item_id and deletes the corresponding set
    """
    try:
        etag = request.headers["If-Match"]
    except KeyError:
        raise SuperdeskApiError.badRequestError(
            "If-Match field missing in header")

    delete_response = get_sams_client().sets.delete(item_id=item_id,
                                                    headers={"If-Match": etag})
    if delete_response.status_code != 204:
        return delete_response.json(), delete_response.status_code
    if delete_response.status_code == 204:
        remove_set_restriction_from_desks(item_id)
        push_notification(
            "sams:set:deleted",
            item_id=item_id,
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            extension="sams",
        )
    return "", delete_response.status_code
Exemplo n.º 9
0
def update(item_id):
    """
    Uses item_id and updates the corresponding set
    """
    try:
        etag = request.headers["If-Match"]
    except KeyError:
        raise SuperdeskApiError.badRequestError(
            "If-Match field missing in header")

    updates = request.get_json()
    update_response = get_sams_client().sets.update(item_id=item_id,
                                                    updates=updates,
                                                    headers={"If-Match": etag})
    if update_response.status_code == 200:
        push_notification(
            "sams:set:updated",
            item_id=update_response.json()["_id"],
            user_id=get_user_id(True),
            session_id=get_auth()["_id"],
            _etag=update_response.json()["_etag"],
            extension="sams",
        )
    return update_response.json(), update_response.status_code