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

« back to all changes in this revision

Viewing changes to www/apps/tutorial/__init__.py

  • Committer: mattgiuca
  • Date: 2008-02-25 04:10:55 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:569
tutorial: Now if a special directory "media" is requested, it will plainly
    serve any file path out of the subjects/media directory.
    So "/tutorial/media/a/b/c.png" will serve "subjects/media/a/b/c.png".
    This is to allow images in tutorial worksheets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import urllib
34
34
import re
35
35
from xml.dom import minidom
 
36
import mimetypes
36
37
 
37
38
import cjson
38
39
 
90
91
    path_segs = req.path.split(os.sep)
91
92
    subject = None
92
93
    worksheet = None
93
 
    if len(path_segs) > 2:
94
 
        req.throw_error(req.HTTP_NOT_FOUND)
95
 
    elif len(req.path) > 0:
 
94
    if len(req.path) > 0:
96
95
        subject = path_segs[0]
 
96
        if subject == "media":
 
97
            # Special case: "tutorial/media" will plainly serve any path
 
98
            # relative to "subjects/media".
 
99
            handle_media_path(req)
 
100
            return
 
101
        if len(path_segs) > 2:
 
102
            req.throw_error(req.HTTP_NOT_FOUND)
97
103
        if len(path_segs) == 2:
98
104
            worksheet = path_segs[1]
99
105
 
105
111
        handle_worksheet(req, subject, worksheet)
106
112
        plugins.console.present(req, windowpane=True)
107
113
 
 
114
def handle_media_path(req):
 
115
    """
 
116
    Urls in "tutorial/media" will just be served directly, relative to
 
117
    subjects. So if we came here, we just want to serve a file relative to the
 
118
    subjects directory on the local file system.
 
119
    """
 
120
    # First normalise the path
 
121
    urlpath = os.path.normpath(req.path)
 
122
    # Now if it begins with ".." or separator, then it's illegal
 
123
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
 
124
        req.throw_error(req.HTTP_FORBIDDEN)
 
125
    filename = os.path.join(conf.subjects_base, urlpath)
 
126
    (type, _) = mimetypes.guess_type(filename)
 
127
    if type is None:
 
128
        type = conf.mimetypes.default_mimetype
 
129
    ## THIS CODE taken from apps/server/__init__.py
 
130
    if not os.access(filename, os.R_OK):
 
131
        req.throw_error(req.HTTP_NOT_FOUND)
 
132
    if os.path.isdir(filename):
 
133
        req.throw_error(req.HTTP_FORBIDDEN,
 
134
            "The requested file is a directory.")
 
135
    req.content_type = type
 
136
    req.sendfile(filename)
 
137
 
108
138
def handle_toplevel_menu(req):
109
139
    # This is represented as a directory. Redirect and add a slash if it is
110
140
    # missing.