29
29
from ivle.config import Config
30
30
from ivle.webapp.base.views import BaseView
31
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
31
from ivle.webapp.base.plugins import PublicViewPlugin, ViewPlugin, MediaPlugin
32
32
from ivle.webapp.errors import NotFound, Forbidden
34
34
def media_url(req, plugin, path):
141
141
return super(VersionedMediaFileView, self)._make_filename(req)
143
class Plugin(ViewPlugin):
144
# This maps a media namespace to an external dependency directory (in this
145
# case specified by the configuration option media/externals/jquery) and a
146
# list of permitted subpaths.
147
EXTERNAL_MEDIA_MAP = {'jquery': ('jquery', ['jquery.js'])}
149
class ExternalMediaFileView(BaseMediaFileView):
150
'''A view for media files from external dependencies.
152
This serves specific static files from external dependencies as defined in
153
the IVLE configuration.
157
def _make_filename(self, req):
159
extern = EXTERNAL_MEDIA_MAP[self.ns]
163
# Unless it's a whitelisted path, we don't want to hear about it.
164
if self.path not in extern[1]:
167
# Grab the admin-configured path for this particular external dep.
169
externdir = config['media']['externals'][extern[0]]
171
assert isinstance(externdir, basestring)
173
return os.path.join(externdir, self.path)
175
def get_permissions(self, user):
178
class ExternalVersionedMediaFileView(ExternalMediaFileView):
179
'''A view for versioned media files from external dependencies, with
182
This serves specific static media files from external dependencies with a
183
version string, and requests that browsers cache them for a long time.
186
def __init__(self, req, ns, path, version):
187
super(ExternalVersionedMediaFileView, self).__init__(req, ns, path)
188
self.version = version
190
def _make_filename(self, req):
191
if self.version != Config()['media']['version']:
194
# Don't expire for a year.
195
req.headers_out['Expires'] = email.utils.formatdate(
196
timeval=time.time() + (60*60*24*365),
199
return super(ExternalVersionedMediaFileView, self)._make_filename(req)
202
class Plugin(ViewPlugin, PublicViewPlugin):
204
('+media/+:version/+external/:ns/*path', ExternalVersionedMediaFileView),
205
('+media/+external/:ns/*path', ExternalMediaFileView),
145
206
('+media/+:version/:ns/*path', VersionedMediaFileView),
146
207
('+media/:ns/*path', MediaFileView),