Ejemplo n.º 1
0
def serve(request, path, **kwargs):
    if not settings.DEBUG and not kwargs.get('insecure'):
        raise ImproperlyConfigured(
            "The gears view can only be used in debug mode or if the "
            "--insecure option of 'runserver' is used.")

    # It is only required check because we generate
    # version arg for each file
    if 'HTTP_IF_MODIFIED_SINCE' in request.META:
        response = HttpResponse()
        response['Expires'] = http_date(time.time() + MAX_AGE)
        response.status_code = 304
        return response

    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
    try:
        asset = build_asset(environment, normalized_path)
    except FileNotFound:
        return staticfiles_serve(request, path, **kwargs)

    last_modified = asset.mtime
    if request.GET.get('body'):
        asset = asset.processed_source
    mimetype, encoding = mimetypes.guess_type(normalized_path)
    mimetype = mimetype or 'application/octet-stream'
    response = HttpResponse(bytes(asset), mimetype=mimetype)
    if encoding:
        response['Content-Encoding'] = encoding
    response['Last-Modified'] = http_date(last_modified)
    return response
Ejemplo n.º 2
0
def serve(request, path, **kwargs):
    if not settings.DEBUG and not kwargs.get("insecure"):
        raise ImproperlyConfigured(
            "The gears view can only be used in debug mode or if the " "--insecure option of 'runserver' is used."
        )

    # It is only required check because we generate
    # version arg for each file
    if "HTTP_IF_MODIFIED_SINCE" in request.META:
        response = HttpResponse()
        response["Expires"] = http_date(time.time() + MAX_AGE)
        response.status_code = 304
        return response

    normalized_path = posixpath.normpath(parse.unquote(path)).lstrip("/")
    try:
        asset = build_asset(environment, normalized_path)
    except FileNotFound:
        return staticfiles_serve(request, path, **kwargs)

    last_modified = asset.mtime
    if request.GET.get("body"):
        asset = asset.processed_source
    mimetype, encoding = mimetypes.guess_type(normalized_path)
    mimetype = mimetype or "application/octet-stream"
    response = HttpResponse(asset, mimetype=mimetype)
    if encoding:
        response["Content-Encoding"] = encoding
    response["Last-Modified"] = http_date(last_modified)
    return response
Ejemplo n.º 3
0
def serve(request, path, document_root=None, insecure=False, **kwargs):
    """
    Proxies static request to middleman
    """
    if not settings.DEBUG and not insecure:
        raise ImproperlyConfigured("The staticfiles view can only be used in "
                                   "debug mode or if the the --insecure "
                                   "option of 'runserver' is used")
    url = os.path.join(settings.MIDDLEMAN_URL, path)
    try:
        response = urllib2.urlopen(url)
    except urllib2.HTTPError, exc:
        if not exc.code == 404:
            raise
        return staticfiles_serve(request, path, document_root=document_root, insecure=insecure, **kwargs)
Ejemplo n.º 4
0
def serve(request, path, **kwargs):
    if not settings.DEBUG and not kwargs.get('insecure'):
        raise ImproperlyConfigured(
            "The gears view can only be used in debug mode or if the "
            "--insecure option of 'runserver' is used.")
    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
    try:
        asset = build_asset(environment, normalized_path)
    except FileNotFound:
        return staticfiles_serve(request, path, **kwargs)
    if request.GET.get('body'):
        asset = asset.processed_source
    mimetype, encoding = mimetypes.guess_type(normalized_path)
    mimetype = mimetype or 'application/octet-stream'
    response = HttpResponse(str(asset), mimetype=mimetype)
    if encoding:
        response['Content-Encoding'] = encoding
    return response
Ejemplo n.º 5
0
def serve(request, path, document_root=None, show_indexes=False, insecure=False):
    """
    Serve static files below a given point in the directory structure.

    To use, put a URL pattern such as::

        (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})

    in your URLconf. You must provide the ``document_root`` param. You may
    also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
    of the directory.  This index view will use the template hardcoded below,
    but if you'd like to override it, you can create a template called
    ``static/directory_index.html``.
    """
    warnings.warn("The view at `django.views.static.serve` is deprecated; "
                  "use the path `django.contrib.staticfiles.views.serve` "
                  "instead.", PendingDeprecationWarning)
    return staticfiles_serve(request, path, document_root, show_indexes, insecure)
Ejemplo n.º 6
0
def serve(request, path, document_root=None, show_indexes=False, insecure=False):
    """
    Serve static files below a given point in the directory structure.

    To use, put a URL pattern such as::

        (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root' : '/path/to/my/files/'})

    in your URLconf. You must provide the ``document_root`` param. You may
    also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
    of the directory.  This index view will use the template hardcoded below,
    but if you'd like to override it, you can create a template called
    ``static/directory_index.html``.
    """
    warnings.warn("The view at `django.views.static.serve` is deprecated; "
                  "use the path `django.contrib.staticfiles.views.serve` "
                  "instead.", PendingDeprecationWarning)
    return staticfiles_serve(request, path, document_root, show_indexes, insecure)
Ejemplo n.º 7
0
def serve(request, path, **kwargs):
    mimetype, encoding = mimetypes.guess_type(path)
    if not mimetype in settings.MIMETYPES.values():
        try:
            return staticfiles_serve(request, path, **kwargs)
        except Http404:
            return static.serve(request, path, document_root=django_settings.STATIC_ROOT)

    bundle = request.GET.get("bundle") in ("1", "t", "true") or not settings.DEBUG
    try:
        asset = finder.find(path, bundle=bundle)
    except AssetNotFound:
        return static.serve(request, path, document_root=django_settings.STATIC_ROOT)

    # Respect the If-Modified-Since header.
    modified_since = request.META.get("HTTP_IF_MODIFIED_SINCE")
    if not static.was_modified_since(modified_since, asset.mtime, asset.size):
        return HttpResponseNotModified(content_type=asset.attributes.content_type)

    response = HttpResponse(asset.content, content_type=asset.attributes.content_type)
    response["Last-Modified"] = static.http_date(asset.mtime)
    response["Content-Length"] = asset.size

    return response
Ejemplo n.º 8
0
 def serve_static(request: HttpRequest, path: str) -> HttpResponse:
     response = staticfiles_serve(request, path)
     response["Access-Control-Allow-Origin"] = "*"
     return response