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

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/__init__.py

ivle.webapp.tutorial#SubjectMediaView now subclasses
ivle.webapp.media#MediaFileView

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
from ivle.webapp.base.views import BaseView
41
41
from ivle.webapp.base.xhtml import XHTMLView
42
42
from ivle.webapp.base.plugins import BasePlugin
 
43
from ivle.webapp.media import MediaFileView
43
44
from ivle.webapp.errors import NotFound, Forbidden
44
45
from ivle.webapp.tutorial.rst import rst
45
46
from ivle.webapp.tutorial.service import AttemptsRESTView, \
197
198
 
198
199
        ctx['worksheetstream'] = add_exercises(ctx['worksheetstream'], ctx, req)
199
200
 
200
 
class SubjectMediaView(BaseView):
 
201
class SubjectMediaView(MediaFileView):
201
202
    '''The view of subject media files.
202
203
 
203
204
    URIs pointing here will just be served directly, from the subject's
208
209
        self.subject = req.store.find(Subject, code=subject).one()
209
210
        self.path = os.path.normpath(path)
210
211
 
211
 
    def render(self, req):
 
212
    def _make_filename(self, req):
212
213
        # If the subject doesn't exist, self.subject will be None. Die.
213
214
        if not self.subject:
214
215
            raise NotFound()
215
216
 
216
 
        # If it begins with ".." or separator, it's illegal. Die.
217
 
        if self.path.startswith("..") or self.path.startswith('/'):
218
 
            raise Forbidden()
219
217
        subjectdir = os.path.join(ivle.conf.subjects_base,
220
218
                                  self.subject.code, 'media')
221
 
        filename = os.path.join(subjectdir, self.path)
222
 
 
223
 
        # Find an appropriate MIME type.
224
 
        (type, _) = mimetypes.guess_type(filename)
225
 
        if type is None:
226
 
            type = ivle.conf.mimetypes.default_mimetype
227
 
 
228
 
        # Get out if it is unreadable or a directory.
229
 
        if not os.access(filename, os.F_OK):
230
 
            raise NotFound()
231
 
        if not os.access(filename, os.R_OK) or os.path.isdir(filename):
232
 
            raise Forbidden()
233
 
 
234
 
        req.content_type = type
235
 
        req.sendfile(filename)
 
219
        return os.path.join(subjectdir, self.path)
236
220
 
237
221
def is_valid_subjname(subject):
238
222
    m = re_ident.match(subject)