~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-02-17 01:54:18 UTC
  • mto: (1099.1.143 new-dispatch)
  • mto: This revision was merged to the branch mainline in revision 1100.
  • Revision ID: grantw@unimelb.edu.au-20090217015418-9ogs10pe3lfmpo09
Implement a DownloadView. It mostly works.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
            # There is no user.
64
64
            raise NotFound()
65
65
 
 
66
        self.serve(req, owner, jail, path)
 
67
 
 
68
    def serve(self, req, owner, jail, path):
66
69
        serve_file(req, owner, jail, path)
67
70
 
 
71
class DownloadView(ServeView):
 
72
    def serve(self, req, owner, jail, path):
 
73
        serve_file(req, owner, jail, path, download=True)
 
74
 
68
75
def authorize(req):
69
76
    """Given a request, checks whether req.username is allowed to
70
77
    access req.path. Returns True on authorization success, False on failure.
103
110
    if not authorize(req):
104
111
        raise Unauthorized()
105
112
 
 
113
    args = [path]
 
114
    if download:
 
115
        args.insert(0, '-d')
 
116
 
106
117
    # TODO: Download. Content-Disposition, etc.
107
118
    (out, err) = ivle.interpret.execute_raw(req.user, jail, '/home',
108
119
                os.path.join(ivle.conf.share_path, 'services/serveservice'),
109
 
                [path])
 
120
                args)
110
121
    assert not err
111
122
 
112
123
    # Remove the JSON from the front of the response, and decode it.
123
134
            raise AssertionError('Unknown error from serveservice: %s' %
124
135
                                 response['error'])
125
136
 
 
137
    if download:
 
138
        req.headers_out["Content-Disposition"] = \
 
139
                     "attachment; filename=%s" % response['name']
126
140
    req.content_type = response['type']
127
141
    req.write(out)
128
142
 
129
143
class Plugin(ViewPlugin):
130
144
    urls = [
131
 
        ('serve/*path', ServeView)
 
145
        ('serve/*path', ServeView),
 
146
        ('download/*path', DownloadView),
132
147
    ]