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

« back to all changes in this revision

Viewing changes to www/common/studpath.py

  • Committer: William Grant
  • Date: 2010-07-28 05:06:15 UTC
  • Revision ID: grantw@unimelb.edu.au-20100728050615-uwbxn9frla3pdw8m
Encode content_type when downloading files. cjson made us write bad code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# IVLE - Informatics Virtual Learning Environment
2
 
# Copyright (C) 2007-2008 The University of Melbourne
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 
 
18
 
# Module: common.studpath
19
 
# Author: Matt Giuca
20
 
# Date:   14/12/2007
21
 
 
22
 
# Provides functions for translating URLs into physical locations in the
23
 
# student directories in the local file system.
24
 
 
25
 
import os
26
 
 
27
 
import conf
28
 
from common import util
29
 
 
30
 
def url_to_local(urlpath):
31
 
    """Given a URL path (part of a URL query string, see below), returns a
32
 
    tuple of
33
 
        * the username of the student whose directory is being browsed
34
 
        * the absolute path in the file system where that file will be
35
 
            found within the student directories.
36
 
 
37
 
    urlpath: Part of the URL, but only the part *after* the application. For
38
 
    instance, given the URL "/ivle/browse/joe/home/mydir/myfile", urlpath will
39
 
    be just "joe/home/mydir/myfile". The expected result is something like
40
 
    ("joe", "/home/informatics/jails/joe/home/joe/home/mydir/myfile").
41
 
    Note that the actual location is not guaranteed by this interface (this
42
 
    function serves as a single point of control as to how URLs map onto
43
 
    student directories).
44
 
 
45
 
    Returns (None, None) if the path is empty.
46
 
 
47
 
    See also: conf.jail_base
48
 
    """
49
 
    # First normalise the path
50
 
    urlpath = os.path.normpath(urlpath)
51
 
    # Now if it begins with ".." or separator, then it's illegal
52
 
    if urlpath.startswith("..") or urlpath.startswith(os.sep):
53
 
        return (None, None)
54
 
    # Note: User can be a group name. There is absolutely no difference in our
55
 
    # current directory scheme.
56
 
    (user, subpath) = util.split_path(urlpath)
57
 
    if user is None: return (None, None)
58
 
 
59
 
    # Join the user onto 'home' then the full path specified.
60
 
    # This results in the user's name being repeated twice, which is in
61
 
    # accordance with our directory scheme.
62
 
    # (The first time is the name of the jail, the second is the user's home
63
 
    # directory within the jail).
64
 
    path = os.path.join(conf.jail_base, user, 'home', urlpath)
65
 
 
66
 
    return (user, path)
67
 
 
68
 
def url_to_jailpaths(urlpath):
69
 
    """Given a URL path (part of a URL query string), returns a tuple of
70
 
        * the username of the student whose directory is being browsed
71
 
        * the absolute path where the jail will be located.
72
 
        * the path of the file relative to the jail.
73
 
 
74
 
    urlpath: See urlpath in url_to_local.
75
 
 
76
 
    >>> url_to_jailpaths("joe/home/mydir/myfile")
77
 
    ("joe", "/home/informatics/jails/joe", "home/joe/home/mydir/myfile")
78
 
 
79
 
    >>> url_to_jailpaths("")
80
 
    (None, None, None)
81
 
    """
82
 
    # First normalise the path
83
 
    urlpath = os.path.normpath(urlpath)
84
 
    # Now if it begins with ".." then it's illegal
85
 
    if urlpath.startswith(".."):
86
 
        return (None, None, None)
87
 
    # Note: User can be a group name. There is absolutely no difference in our
88
 
    # current directory scheme.
89
 
    (user, subpath) = util.split_path(urlpath)
90
 
    if user is None: return (None, None, None)
91
 
 
92
 
    jail = os.path.join(conf.jail_base, user)
93
 
    path = os.path.join('home', urlpath)
94
 
 
95
 
    return (user, jail, path)