Esempio n. 1
0
def check_precondition_headers(viewfunc, request):
    """View decorator to check X-If-[Unm|M]odified-Since headers.

    This decorator checks pre-validated vlaues from the X-If-Modified-Since
    and X-If-Unmodified-Since headers against the actual last-modified
    time of the target resource.  If the preconditions are not met then
    it raises the appropriate error response.

    In addition, any retreived value for the last-modified time will be
    stored in the response headers for return to the client.  This may save
    having to look it up again when the response is being rendered.
    """
    if "if_modified_since" in request.validated:
        ts = get_resource_timestamp(request)
        request.response.headers["X-Last-Modified"] = str(ts)
        if ts <= request.validated["if_modified_since"]:
            raise HTTPNotModified(headers={
                "X-Last-Modified": str(ts),
            })

    if "if_unmodified_since" in request.validated:
        ts = get_resource_timestamp(request)
        request.response.headers["X-Last-Modified"] = str(ts)
        if ts > request.validated["if_unmodified_since"]:
            raise HTTPPreconditionFailed(headers={
                "X-Last-Modified": str(ts),
            })

    return viewfunc(request)
def check_precondition_headers(viewfunc, request):
    """View decorator to check X-If-[Unm|M]odified-Since headers.

    This decorator checks pre-validated values from the X-If-Modified-Since
    and X-If-Unmodified-Since headers against the actual last-modified
    time of the target resource.  If the preconditions are not met then
    it raises the appropriate error response.

    In addition, any retrieved value for the last-modified time will be
    stored in the response headers for return to the client.  This may save
    having to look it up again when the response is being rendered.
    """
    if "if_modified_since" in request.validated:
        ts = get_resource_timestamp(request)
        request.response.headers["X-Last-Modified"] = str(ts)
        if ts <= request.validated["if_modified_since"]:
            raise HTTPNotModified(headers={
                "X-Last-Modified": str(ts),
            })

    if "if_unmodified_since" in request.validated:
        ts = get_resource_timestamp(request)
        request.response.headers["X-Last-Modified"] = str(ts)
        if ts > request.validated["if_unmodified_since"]:
            raise HTTPPreconditionFailed(headers={
                "X-Last-Modified": str(ts),
            })

    return viewfunc(request)
def get_collection(request):
    storage = request.validated["storage"]
    user = request.user
    collection = request.validated["collection"]

    filters = {}
    filter_names = ("ids", "newer", "older", "limit", "offset", "sort")
    for name in filter_names:
        if name in request.validated:
            filters[name] = request.validated[name]

    if request.validated.get("full", False):
        res = storage.get_items(user, collection, **filters)
        for bso in res["items"]:
            bso.pop("ttl", None)
    else:
        res = storage.get_item_ids(user, collection, **filters)
    next_offset = res.get("next_offset")
    if next_offset is not None:
        request.response.headers["X-Weave-Next-Offset"] = str(next_offset)
    # Ensure that X-Last-Modified is present, since it's needed when
    # doing pagination.  This lookup is essentially free since we already
    # loaded and cached the timestamp when taking the collection lock.
    ts = get_resource_timestamp(request)
    request.response.headers["X-Last-Modified"] = str(ts)
    return res["items"]
def get_collection(request):
    storage = request.validated["storage"]
    user = request.user
    collection = request.validated["collection"]

    filters = {}
    filter_names = ("ids", "newer", "older", "limit", "offset", "sort")
    for name in filter_names:
        if name in request.validated:
            filters[name] = request.validated[name]

    if request.validated.get("full", False):
        res = storage.get_items(user, collection, **filters)
        for bso in res["items"]:
            bso.pop("ttl", None)
    else:
        res = storage.get_item_ids(user, collection, **filters)
    next_offset = res.get("next_offset")
    if next_offset is not None:
        request.response.headers["X-Weave-Next-Offset"] = str(next_offset)
    # Ensure that X-Last-Modified is present, since it's needed when
    # doing pagination.  This lookup is essentially free since we already
    # loaded and cached the timestamp when taking the collection lock.
    ts = get_resource_timestamp(request)
    request.response.headers["X-Last-Modified"] = str(ts)
    return res["items"]
 def adjust_response(self, value, request, response):
     # Ensure that every response reports the last-modified timestamp.
     # In most cases this will have already been set when we looked it
     # up during processing of the request.
     if "X-Last-Modified" not in response.headers:
         ts = get_resource_timestamp(request)
         response.headers["X-Last-Modified"] = str(ts)