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

« back to all changes in this revision

Viewing changes to ivle/webapp/filesystem/serve/__init__.py

  • Committer: William Grant
  • Date: 2009-07-04 13:26:01 UTC
  • mto: (1294.4.2 ui-the-third)
  • mto: This revision was merged to the branch mainline in revision 1353.
  • Revision ID: grantw@unimelb.edu.au-20090704132601-ree3krvgipef4uqz
Move the already-ported filesystem views to use subpaths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
from ivle.webapp.base.xhtml import XHTMLErrorView
35
35
from ivle.webapp.base.plugins import ViewPlugin, PublicViewPlugin
36
36
from ivle.webapp.errors import NotFound, Unauthorized, Forbidden
37
 
from ivle.webapp.urls import INF
38
37
from ivle.webapp import ApplicationRoot
39
38
 
40
 
class ServeFile(object):
41
 
    def __init__(self, root, path):
42
 
        self.root = root
43
 
        self.path = path
44
 
 
45
39
class ServeView(BaseView):
 
40
    subpath_allowed = True
 
41
 
46
42
    def authorize(self, req):
47
43
        return req.user is not None
48
44
 
51
47
        # Get the username of the student whose work we are browsing, and the
52
48
        # path on the local machine where the file is stored.
53
49
        (login, jail, path) = studpath.url_to_jailpaths(req.config,
54
 
                                                        self.context.path)
 
50
                                                        self.path)
55
51
 
56
52
        owner = User.get_by_login(req.store, login)
57
53
        if not owner:
60
56
 
61
57
        self.serve(req, owner, jail, path)
62
58
 
 
59
    @property
 
60
    def path(self):
 
61
        return os.path.join(*self.subpath) if self.subpath else ''
 
62
 
63
63
    def serve(self, req, owner, jail, path):
64
64
        self.serve_file(req, owner, jail, path)
65
65
 
136
136
        req.content_type = response['type']
137
137
        req.write(out)
138
138
 
139
 
class DownloadFile(object):
140
 
    def __init__(self, root, path):
141
 
        self.root = root
142
 
        self.path = path
143
 
 
144
139
class DownloadView(ServeView):
145
 
    def __init__(self, req, context):
146
 
        super(DownloadView, self).__init__(req, context)
 
140
    def __init__(self, req, context, subpath=None):
 
141
        super(DownloadView, self).__init__(req, context, subpath)
147
142
        filelist = req.get_fieldstorage().getlist('path')
148
143
        if filelist:
149
144
            self.files = [f.value for f in filelist]
173
168
    def get_error_view(cls, e):
174
169
        return XHTMLErrorView
175
170
 
176
 
def root_to_servefile(root, *path):
177
 
    return ServeFile(root, os.path.join(*path) if path else '')
178
 
 
179
 
def root_to_downloadfile(root, *path):
180
 
    return DownloadFile(root, os.path.join(*path) if path else '')
181
 
 
182
171
class Plugin(ViewPlugin, PublicViewPlugin):
183
 
    forward_routes = [(ApplicationRoot, 'serve', root_to_servefile, INF),
184
 
                      (ApplicationRoot, 'download', root_to_downloadfile, INF),
185
 
                      ]
186
 
 
187
 
    views = [(ServeFile, '+index', ServeView),
188
 
             (DownloadFile, '+index', DownloadView)
 
172
    views = [(ApplicationRoot, 'serve', ServeView),
 
173
             (ApplicationRoot, 'download', DownloadView)
189
174
             ]
190
175
 
191
176
    # TODO: Need to restore this.