~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/media.py

Give console and tutorial services security declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
'''Media file support for the framework.'''
21
21
 
22
22
import os
23
 
import time
24
23
import inspect
25
24
import mimetypes
26
 
import email.utils
27
25
 
 
26
import ivle.conf
28
27
from ivle.webapp.base.views import BaseView
29
 
from ivle.webapp.base.plugins import PublicViewPlugin, ViewPlugin, MediaPlugin
 
28
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
30
29
from ivle.webapp.errors import NotFound, Forbidden
31
30
 
32
31
def media_url(req, plugin, path):
33
32
    '''Generates a URL to a media file.
34
 
 
 
33
    
35
34
    Plugin can be a string, in which case it is put into the path literally,
36
 
    or a plugin object, in which case its name is looked up.
37
 
 
38
 
    If a version is specified in the IVLE configuration, a versioned URL will
39
 
    be generated.
40
 
    '''
 
35
    or a plugin object, in which case its name is looked up.'''
41
36
    if not isinstance(plugin, basestring):
42
 
        plugin = req.config.reverse_plugins[plugin]
43
 
 
44
 
    media_path = os.path.join('+media', '+' + req.config['media']['version']) \
45
 
                    if req.config['media']['version'] else '+media'
46
 
 
47
 
    return req.make_path(os.path.join(media_path, plugin, path))
 
37
        plugin = req.reverse_plugins[plugin]
 
38
 
 
39
    return os.path.join(ivle.conf.root_dir, '+media', plugin, path)
48
40
 
49
41
class BaseMediaFileView(BaseView):
50
42
    '''A view for media files.
99
91
 
100
92
    def _make_filename(self, req):
101
93
        try:
102
 
            plugin = req.config.plugins[self.ns]
 
94
            plugin = req.plugins[self.ns]
103
95
        except KeyError:
104
96
            raise NotFound()
105
97
 
114
106
    def get_permissions(self, user):
115
107
        return set()
116
108
 
117
 
class VersionedMediaFileView(MediaFileView):
118
 
    '''A view for versioned media files, with aggressive caching.
119
 
 
120
 
    This serves static media files with a version string, and requests that
121
 
    browsers cache them for a long time.
122
 
    '''
123
 
 
124
 
    def __init__(self, req, ns, path, version):
125
 
        super(VersionedMediaFileView, self).__init__(req, ns, path)
126
 
        self.version = version
127
 
 
128
 
    def _make_filename(self, req):
129
 
        if self.version != req.config['media']['version']:
130
 
            raise NotFound()
131
 
 
132
 
        # Don't expire for a year.
133
 
        req.headers_out['Expires'] = email.utils.formatdate(
134
 
                                    timeval=time.time() + (60*60*24*365),
135
 
                                    localtime=False,
136
 
                                    usegmt=True)
137
 
        return super(VersionedMediaFileView, self)._make_filename(req)
138
 
 
139
 
 
140
 
# This maps a media namespace to an external dependency directory (in this
141
 
# case specified by the configuration option media/externals/jquery) and a
142
 
# list of permitted subpaths.
143
 
EXTERNAL_MEDIA_MAP = {'jquery': ('jquery', ['jquery.js'])}
144
 
 
145
 
class ExternalMediaFileView(BaseMediaFileView):
146
 
    '''A view for media files from external dependencies.
147
 
 
148
 
    This serves specific static files from external dependencies as defined in
149
 
    the IVLE configuration.
150
 
    '''
151
 
    permission = None
152
 
 
153
 
    def _make_filename(self, req):
154
 
        try:
155
 
            extern = EXTERNAL_MEDIA_MAP[self.ns]
156
 
        except KeyError:
157
 
            raise NotFound()
158
 
 
159
 
        # Unless it's a whitelisted path, we don't want to hear about it.
160
 
        if self.path not in extern[1]:
161
 
            raise NotFound()
162
 
 
163
 
        # Grab the admin-configured path for this particular external dep.
164
 
        externdir = req.config['media']['externals'][extern[0]]
165
 
 
166
 
        assert isinstance(externdir, basestring)
167
 
 
168
 
        return os.path.join(externdir, self.path)
169
 
 
170
 
    def get_permissions(self, user):
171
 
        return set()
172
 
 
173
 
class ExternalVersionedMediaFileView(ExternalMediaFileView):
174
 
    '''A view for versioned media files from external dependencies, with
175
 
    aggressive caching.
176
 
 
177
 
    This serves specific static media files from external dependencies with a
178
 
    version string, and requests that browsers cache them for a long time.
179
 
    '''
180
 
 
181
 
    def __init__(self, req, ns, path, version):
182
 
        super(ExternalVersionedMediaFileView, self).__init__(req, ns, path)
183
 
        self.version = version
184
 
 
185
 
    def _make_filename(self, req):
186
 
        if self.version != req.config['media']['version']:
187
 
            raise NotFound()
188
 
 
189
 
        # Don't expire for a year.
190
 
        req.headers_out['Expires'] = email.utils.formatdate(
191
 
                                    timeval=time.time() + (60*60*24*365),
192
 
                                    localtime=False,
193
 
                                    usegmt=True)
194
 
        return super(ExternalVersionedMediaFileView, self)._make_filename(req)
195
 
 
196
 
 
197
 
class Plugin(ViewPlugin, PublicViewPlugin):
 
109
class Plugin(ViewPlugin):
198
110
    urls = [
199
 
        ('+media/+:version/+external/:ns/*path', ExternalVersionedMediaFileView),
200
 
        ('+media/+external/:ns/*path', ExternalMediaFileView),
201
 
        ('+media/+:version/:ns/*path', VersionedMediaFileView),
202
111
        ('+media/:ns/*path', MediaFileView),
203
112
    ]
204
 
 
205
 
    public_urls = urls