~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 00:58:59 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-20090217005859-q3m4zrca69x8h9e5
Move serve over to the new framework. It sort of works, except not.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from ivle import (util, studpath, interpret)
31
31
import ivle.conf
32
32
from ivle.database import User
 
33
from ivle.webapp.base.views import BaseView
 
34
from ivle.webapp.base.plugins import ViewPlugin
 
35
from ivle.webapp.errors import NotFound, Unauthorized, Forbidden
33
36
 
34
37
serveservice_path = os.path.join(ivle.conf.share_path, 'services/serveservice')
35
38
interpretservice_path = os.path.join(ivle.conf.share_path,
40
43
default_mimetype = "application/octet-stream"
41
44
zip_mimetype = "application/zip"
42
45
 
43
 
def handle(req):
44
 
    """Handler for the Server application which serves pages."""
45
 
    req.write_html_head_foot = False
46
 
 
47
 
    # Get the username of the student whose work we are browsing, and the path
48
 
    # on the local machine where the file is stored.
49
 
    (login, path) = studpath.url_to_local(req.path)
50
 
 
51
 
    owner = User.get_by_login(req.store, login)
52
 
    if not owner:
53
 
        # There is no user.
54
 
        req.throw_error(req.HTTP_NOT_FOUND,
55
 
            "The path specified is invalid.")
56
 
 
57
 
    serve_file(req, owner, path)
 
46
class ServeView(BaseView):
 
47
    def __init__(self, req, path):
 
48
        self.path = path
 
49
 
 
50
    def authorize(self, req):
 
51
        return req.user is not None
 
52
 
 
53
    def render(self, req):
 
54
        """Handler for the Server application which serves pages."""
 
55
        # Get the username of the student whose work we are browsing, and the
 
56
        # path on the local machine where the file is stored.
 
57
        (login, path) = studpath.url_to_local(self.path)
 
58
 
 
59
        owner = User.get_by_login(req.store, login)
 
60
        if not owner:
 
61
            # There is no user.
 
62
            raise NotFound()
 
63
 
 
64
        serve_file(req, owner, path)
58
65
 
59
66
def authorize(req):
60
67
    """Given a request, checks whether req.username is allowed to
61
 
    access req.path. Returns None on authorization success. Raises
62
 
    HTTP_FORBIDDEN on failure.
 
68
    access req.path. Returns True on authorization success, False on failure.
63
69
    """
64
 
    if req.publicmode:
 
70
    # TODO: Reactivate public mode.
 
71
    #if req.publicmode:
65
72
        # Public mode authorization: any user can access any other user's
66
73
        # files, BUT the accessed file needs to have its "ivle:published" flag
67
74
        # turned on in the SVN status.
68
 
        studpath.authorize_public(req)
69
 
    else:
70
 
        # Private mode authorization: standard (only logged in user can access
71
 
        # their own files, and can access all of them).
72
 
        studpath.authorize(req)
 
75
    #    studpath.authorize_public(req)
 
76
 
 
77
    # Private mode authorization: standard (only logged in user can access
 
78
    # their own files, and can access all of them).
 
79
    return studpath.authorize(req, req.user)
73
80
 
74
81
def serve_file(req, owner, filename, download=False):
75
82
    """Serves a file, using one of three possibilities: interpreting the file,
90
97
    interpret.interpret_file(req, owner, user_jail_dir, '', noop_object)
91
98
 
92
99
    # Authorize access. If failure, this throws a HTTP_FORBIDDEN error.
93
 
    authorize(req)
 
100
    if not authorize(req):
 
101
        raise Unauthorized()
94
102
 
95
103
    # Jump into the jail
96
104
    interp_object = interpret.interpreter_objects["cgi-python"]
102
110
        interpret.interpret_file(req, owner, user_jail_dir,
103
111
            interpretservice_path, interp_object, gentle=True)
104
112
 
105
 
def serve_file_direct(req, filename, type):
106
 
    """Serves a file by directly writing it out to the response.
107
 
 
108
 
    req: An IVLE request object.
109
 
    filename: Filename in the local file system.
110
 
    type: String. Mime type to serve the file with.
111
 
    """
112
 
    if not os.access(filename, os.R_OK):
113
 
        req.throw_error(req.HTTP_NOT_FOUND,
114
 
            "The specified file does not exist.")
115
 
    req.content_type = type
116
 
    req.sendfile(filename)
 
113
class Plugin(ViewPlugin):
 
114
    urls = [
 
115
        ('serve/*path', ServeView)
 
116
    ]