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

1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
1
# IVLE
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
# App: File Service (AJAX server)
19
# Author: Matt Giuca
20
# Date: 9/1/2008
21
22
# This application is an AJAX service. Receives file handling instructions as
23
# requests. Performs actions on the student's workspace, and returns directory
24
# listings in JSON.
25
26
# This rather large documentation explains the request and response to the
27
# file service app (it should probably be taken to a separate document).
28
29
# This is not intended to be accessed directly by the user. It is targeted by
30
# AJAX calls in applications such as browser and editor.
31
32
# Application usage: The input to the application is determined by the fields
33
# passed in as HTTP variables (either in the URL or message body). Also, in
34
# keeping with REST, actions only take effect if this is a POST request as
35
# opposed to a GET request (although a GET request is still allowed to just
36
# get a listing or file dump). Also, the "path" (the part of the URL
37
# after "fileservice" and before the GET variables) is taken into account.
38
39
# Aside from the side-effects to the server (note: side-effects are only
40
# possible for POST requests), the response takes two parts. The response
41
# header contains information about success or failure of the operation. The
42
# response body may contain the requested file.
43
44
# Fileservice has two separate roles: First, an action is performed. This may
45
# be a copy, write, or svn up operation. Then, either a directory listing or
46
# file contents are returned.
47
# This listing/contents may be completely separate from the action,
48
# but they are performed together because the client will usually want to
49
# perform some action, then update its display as a result of the action.
50
51
# The special "return" variable can be "listing" or "contents" - listing is
52
# the default if unspecified. If "listing", it will return a directory listing
53
# of the file specified. If the file is not a directory, it just returns a
54
# single "." object, with details about the file.
55
# If "contents", it will return the contents of the file specified. If the
56
# file is a directory, it will simply return the listing again.
57
58
# GET requests will have all variables other than "return" ignored, and the
59
# only behaviour will be to generate the directory or file listing. POST
60
# requests will result in an action if one is specified. If the action is
61
# UNSUCCESSFUL, returns the header "X-IVLE-Action-Error: <errormessage>".
62
# Successful actions succeed silently. Note that the action does not affect
63
# the HTTP response code (it may be 200 even upon failure).
64
65
# The path (req.path) controls which file or directory will be
66
# returned. If it is a file, returns the header "X-IVLE-Return: File" and
67
# status 200 OK. The response body is either a verbatim dump of the file
68
# specified, or a single-file directory listing, as described above.
69
# The Content-Type will probably be text/plain but should not be relied upon.
70
# If it is a directory, returns the header "X-IVLE-Return: Dir" and status
71
# 200 OK. The response body is a JSON directory listing (see below). The
72
# Content-Type cannot be relied upon. If the file is not found or there is
73
# some other read error, returns no X-IVLE-Return header, a 400-level
74
# response status. (404 File Not Found, 403 Forbidden, etc), and a header
75
# "X-IVLE-Return-Error: <errormessage>".
76
77
# See action.py for a full description of the actions.
78
# See listing.py for a full description of the output format of the directory
79
# listing.
80
81
import urllib
1633 by Matt Giuca
Correct locale setting for Subversion. Previously pysvn would throw a nasty
82
import locale
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
83
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
84
try:
85
    import json
86
except ImportError:
87
    import simplejson as json
1165.1.38 by William Grant
Allow fileservice actions to return custom JSON.
88
1099.1.156 by William Grant
Clean up ivle.fileservice_lib a bit.
89
import ivle.fileservice_lib.action
90
import ivle.fileservice_lib.listing
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
91
92
# Mime types
93
# application/json is the "best" content type but is not good for
94
# debugging because Firefox just tries to download it
95
mime_dirlisting = "text/html"
96
#mime_dirlisting = "application/json"
97
1633 by Matt Giuca
Correct locale setting for Subversion. Previously pysvn would throw a nasty
98
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
99
def handle(req):
100
    """Handler for the File Services application."""
101
1650 by David Coles
Set local inside handle function to prevent it breaking testcases when development enviroment is not en_US
102
    # Set locale to UTF-8 (required by PySVN)
103
    locale.setlocale(locale.LC_CTYPE, "en_US.UTF-8")
104
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
105
    # We really, really don't want the responses to be cached.
1310 by William Grant
Specify no-cache in fileservice responses, or Opera caches aggressively.
106
    req.headers_out['Cache-Control'] = 'no-store, no-cache, must-revalidate'
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
107
108
    # Get all the arguments, if POST.
109
    # Ignore arguments if not POST, since we aren't allowed to cause
110
    # side-effects on the server.
111
    act = None
112
    fields = req.get_fieldstorage()
113
    if req.method == 'POST':
114
        act = fields.getfirst('action')
1165.1.38 by William Grant
Allow fileservice actions to return custom JSON.
115
116
    out = None
117
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
118
    if act is not None:
119
        try:
1165.1.38 by William Grant
Allow fileservice actions to return custom JSON.
120
            out = ivle.fileservice_lib.action.handle_action(req, act, fields)
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
121
        except action.ActionError, message:
1089 by chadnickbok
Fixes Issue #14
122
            req.headers_out['X-IVLE-Action-Error'] = urllib.quote(str(message))
1079 by William Grant
Merge setup-refactor branch. This completely breaks existing installations;
123
1165.1.38 by William Grant
Allow fileservice actions to return custom JSON.
124
    if out:
125
        req.content_type = 'application/json'
1801.1.1 by William Grant
Replace cjson with json, or simplejson if json is not available (Python <2.6)
126
        req.write(json.dumps(out))
1165.1.38 by William Grant
Allow fileservice actions to return custom JSON.
127
    else:
128
        return_type = fields.getfirst('return')
129
        ivle.fileservice_lib.listing.handle_return(req,
130
                                                   return_type == "contents")