This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
dj_static.py
114 lines (91 loc) · 3.93 KB
/
dj_static.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
import static
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
try:
from urllib.parse import urlparse
except ImportError: # Python 2
from urlparse import urlparse
from django.contrib.staticfiles import utils
try:
from django.core.handlers.wsgi import get_path_info
except ImportError: # django < 1.7
try:
from django.core.handlers.base import get_path_info
except ImportError: # django < 1.5
import sys
py3 = sys.version_info[0] == 3
def get_path_info(environ):
"""
Returns the HTTP request's PATH_INFO as a unicode string.
"""
path_info = environ.get('PATH_INFO', str('/'))
# Under Python 3, strings in environ are decoded with ISO-8859-1;
# re-encode to recover the original bytestring provided by the web server.
if py3:
path_info = path_info.encode('iso-8859-1')
# It'd be better to implement URI-to-IRI decoding, see #19508.
return path_info.decode('utf-8')
class Cling(WSGIHandler):
"""WSGI middleware that intercepts calls to the static files
directory, as defined by the STATIC_URL setting, and serves those files.
"""
def __init__(self, application, base_dir=None, base_url=None, ignore_debug=False):
self.application = application
self.ignore_debug = ignore_debug
if not base_dir:
base_dir = self.get_base_dir()
self.base_url = urlparse(base_url or self.get_base_url())
self.cling = static.Cling(base_dir)
try:
self.debug_cling = DebugHandler(application, base_dir=base_dir)
except TypeError:
self.debug_cling = DebugHandler(application)
super(Cling, self).__init__()
def get_base_dir(self):
return settings.STATIC_ROOT
def get_base_url(self):
utils.check_settings()
return settings.STATIC_URL
@property
def debug(self):
return settings.DEBUG
def _transpose_environ(self, environ):
"""Translates a given environ to static.Cling's expectations."""
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.base_url[2]) - 1:]
return environ
def _should_handle(self, path):
"""Checks if the path should be handled. Ignores the path if:
* the host is provided as part of the base_url
* the request's path isn't under the media path (or equal)
"""
return path.startswith(self.base_url[2]) and not self.base_url[1]
def __call__(self, environ, start_response):
# Hand non-static requests to Django
try:
if not self._should_handle(get_path_info(environ)):
return self.application(environ, start_response)
except UnicodeDecodeError:
# Apparently a malformed URL. Just hand it to Django
# for it to respond as it sees fit.
return self.application(environ, start_response)
# Serve static requests from static.Cling
if not self.debug or self.ignore_debug:
environ = self._transpose_environ(environ)
return self.cling(environ, start_response)
# Serve static requests in debug mode from StaticFilesHandler
else:
return self.debug_cling(environ, start_response)
class MediaCling(Cling):
def __init__(self, application, base_dir=None):
super(MediaCling, self).__init__(application, base_dir=base_dir)
# override callable attribute with method
self.debug_cling = self._debug_cling
def _debug_cling(self, environ, start_response):
environ = self._transpose_environ(environ)
return self.cling(environ, start_response)
def get_base_dir(self):
return settings.MEDIA_ROOT
def get_base_url(self):
return settings.MEDIA_URL