Ejemplo n.º 1
0
def post_metadata(request):
    """
    Post the metadata.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    LOGGING.info('Post metadata')
    try:
        _doi = _get_doi_from_xml_body(request.body)
    except ET.ParseError as ex:
        LOGGING.info('Error parsing xml from users request: %s', ex)
        return get_response("Bad Request - error parsing xml: %s" % ex, 400)
    if _doi == None:
        return get_response("Bad Request - doi not found in XML", 400)
    LOGGING.debug('Post metadata, doi: %s', _doi)
    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response("Bad Request - wrong prefix, doi should start " \
                            "with %s" % DOI_PREFIX, 400)

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)

    url = urljoin(DATACITE_URL, request.get_full_path())
    return _post(url, request.body, _get_content_type_header(request))
Ejemplo n.º 2
0
def post_media(request):
    """
    Post the media.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    LOGGING.info("Post media")
    _doi = get_doi_from_request(request, "media")
    if _doi is None:
        return get_response("Bad Request - doi not found in URL", 400)
    LOGGING.debug("Post media, doi: %s", _doi)
    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response(
            "Bad Request - wrong prefix, doi should start "
            "with %s" % DOI_PREFIX, 400)

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)

    url = urljoin(DATACITE_URL, request.get_full_path())
    return _post(url, request.body, _get_content_type_header(request))
Ejemplo n.º 3
0
def post_doi(request):
    """
    Post the DOI.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    LOGGING.info('Post doi')
    try:
        _doi = _get_doi_from_text_body(request.body)
    except IndexError:
        return get_response("Bad Request - request body must be exactly two " \
                            "lines: DOI and URL", 400)
    LOGGING.debug('Post doi, doi: %s', _doi)

    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response("Bad Request - wrong prefix, doi should start " \
                            "with %s" % DOI_PREFIX, 400)

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)

    url = urljoin(DATACITE_URL, request.get_full_path())
    return _post(url, request.body, _get_content_type_header(request))
Ejemplo n.º 4
0
def post_doi(request, method="POST"):
    """
    Post the DOI.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    LOGGING.info("Post doi")
    try:
        _doi = _get_doi_from_text_body(request.body.decode("utf-8"))
    except IndexError:
        return get_response(
            "Bad Request - request body must be exactly two "
            "lines: DOI and URL", 400)
    LOGGING.debug("Post doi, doi: %s", _doi)

    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response(
            "Bad Request - wrong prefix, doi should start "
            "with %s" % DOI_PREFIX, 400)

    try:
        # The URL can contain the DOI - check that it matches
        url_doi = request.get_full_path().split("doi/", 1)[1]
        if len(url_doi) > 0 and url_doi != _doi:
            return get_response(
                "Bad Request - DOI in URL does not match DOI in request body\n",
                400)
    except IndexError:
        # There is no DOI in the URL, which is fine
        pass

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)

    url = urljoin(DATACITE_URL, request.get_full_path())
    return _post(url,
                 request.body,
                 _get_content_type_header(request),
                 method=method)
Ejemplo n.º 5
0
def post_metadata(request, method="POST"):
    """
    Post the metadata.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    LOGGING.info("Post metadata")
    try:
        _doi = _get_doi_from_xml_body(request.body)
    except ET.ParseError as ex:
        LOGGING.info("Error parsing xml from users request: %s", ex)
        return get_response("Bad Request - error parsing xml: %s" % ex, 400)
    if _doi is None:
        return get_response("Bad Request - doi not found in XML", 400)
    LOGGING.debug("Post metadata, doi: %s", _doi)
    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response(
            "Bad Request - wrong prefix, doi should start "
            "with %s" % DOI_PREFIX, 400)

    try:
        # The URL can contain the DOI - check that it matches the metadata
        url_doi = request.get_full_path().split("metadata/", 1)[1]
        if len(url_doi) > 0 and url_doi != _doi:
            return get_response(
                "Bad Request - DOI in URL does not match "
                "DOI in metadata\n", 400)
    except IndexError:
        # There is no DOI in the URL, which is fine
        pass

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)

    url = urljoin(DATACITE_URL, request.get_full_path())
    return _post(url,
                 request.body,
                 _get_content_type_header(request),
                 method=method)
Ejemplo n.º 6
0
def delete_metadata(request):
    """
    Delete the metadata for the DOI.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    _doi = get_doi_from_request(request, 'metadata')
    LOGGING.info('Delete metadata doi: %s', _doi)
    url = urljoin(DATACITE_URL, request.get_full_path())
    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response("Bad Request - wrong prefix, doi should start " \
                            "with %s" % DOI_PREFIX, 400)

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)
    return _delete(url)
Ejemplo n.º 7
0
def delete_metadata(request):
    """
    Delete the metadata for the DOI.

    Args:
        request (HTTPRequest): The HTTP request

    Return:
        a HTTPResponse

    """
    _doi = get_doi_from_request(request, 'metadata')
    LOGGING.info('Delete metadata doi: %s', _doi)
    url = urljoin(DATACITE_URL, request.get_full_path())
    try:
        doi_suffix = _doi.split(DOI_PREFIX, 1)[1]
    except IndexError:
        return get_response("Bad Request - wrong prefix, doi should start " \
                            "with %s" % DOI_PREFIX, 400)

    if not is_authorized(request, doi_suffix):
        return get_response("Unauthorized - insufficient privileges", 403)
    return _delete(url)