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

« back to all changes in this revision

Viewing changes to www/apps/fileservice/action.py

  • Committer: mattgiuca
  • Date: 2008-02-04 00:10:25 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:395
Tutorial: split subjects directory into subjects and problems.
    Subjects now contains only subject XML and worksheet XML files.
    Problems are in a separate directory hierarchy.
Setup / Conf: Added new option "problems_base". This is used by tutorial to
    find the problem sheets.
In sample, moved problems to a "problems" directory. Setup installer correctly
    installs all the problems in this directory to the correct sample place.

Show diffs side-by-side

added added

removed removed

Lines of Context:
134
134
 
135
135
import pysvn
136
136
 
137
 
from common import (util, studpath)
 
137
from common import (util, studpath, zip)
138
138
 
139
139
# Make a Subversion client object
140
140
svnclient = pysvn.Client()
179
179
        raise ActionError("Unknown action")
180
180
    action(req, fields)
181
181
 
 
182
def actionpath_to_urlpath(req, path):
 
183
    """Determines the URL path (relative to the student home) upon which the
 
184
    action is intended to act. See actionpath_to_local.
 
185
    """
 
186
    if path is None:
 
187
        return req.path
 
188
    elif len(path) > 0 and path[0] == os.sep:
 
189
        # Relative to student home
 
190
        return path[1:]
 
191
    else:
 
192
        # Relative to req.path
 
193
        return os.path.join(req.path, path)
 
194
 
182
195
def actionpath_to_local(req, path):
183
196
    """Determines the local path upon which an action is intended to act.
184
197
    Note that fileservice actions accept two paths: the request path,
195
208
 
196
209
    Does not mutate req.
197
210
    """
198
 
    if path is None:
199
 
        path = req.path
200
 
    elif len(path) > 0 and path[0] == os.sep:
201
 
        # Relative to student home
202
 
        path = path[1:]
203
 
    else:
204
 
        # Relative to req.path
205
 
        path = os.path.join(req.path, path)
206
 
 
207
 
    _, r = studpath.url_to_local(path)
 
211
    _, r = studpath.url_to_local(actionpath_to_urlpath(req, path))
208
212
    if r is None:
209
213
        raise ActionError("Invalid path")
210
214
    return r
288
292
 
289
293
    Reads fields: 'path', 'data' (file upload)
290
294
    """
 
295
    # TODO: Read field "unpack".
291
296
    # Important: Data is "None" if the file submitted is empty.
292
297
    path = fields.getfirst('path')
293
298
    data = fields.getfirst('data')
305
310
    except OSError:
306
311
        raise ActionError("Could not write to target file")
307
312
 
 
313
def action_putfiles(req, fields):
 
314
    """Writes data to one or more files in a directory, overwriting them if
 
315
    it they exist.
 
316
 
 
317
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
 
318
    """
 
319
    # Important: Data is "None" if the file submitted is empty.
 
320
    path = fields.getfirst('path')
 
321
    data = fields.getlist('data')
 
322
    unpack = fields.getfirst('unpack')
 
323
    if unpack is None:
 
324
        unpack = False
 
325
    else:
 
326
        unpack = True
 
327
    if path is None:
 
328
        raise ActionError("Required field missing")
 
329
    path = actionpath_to_urlpath(req, path)
 
330
    goterror = False
 
331
 
 
332
    for datum in data:
 
333
        # Each of the uploaded files
 
334
        filepath = os.path.join(path, datum.filename)
 
335
        filedata = datum.file
 
336
 
 
337
        if unpack and datum.filename.lower().endswith(".zip"):
 
338
            # A zip file - unpack it instead of just copying
 
339
            # TODO: Use the magic number instead of file extension
 
340
            # Note: Just unzip into the current directory (ignore the
 
341
            # filename)
 
342
            try:
 
343
                zip.unzip(path, filedata)
 
344
            except (OSError, IOError):
 
345
                goterror = True
 
346
        else:
 
347
            # Not a zip file
 
348
            _, filepath_local = studpath.url_to_local(filepath)
 
349
            if filepath_local is None:
 
350
                raise ActionError("Invalid path")
 
351
 
 
352
            # Copy the contents of file object 'data' to the path 'path'
 
353
            try:
 
354
                dest = open(filepath_local, 'wb')
 
355
                if data is not None:
 
356
                    shutil.copyfileobj(filedata, dest)
 
357
            except OSError:
 
358
                goterror = True
 
359
 
 
360
    if goterror:
 
361
        if len(data) == 1:
 
362
            raise ActionError("Could not write to target file")
 
363
        else:
 
364
            raise ActionError(
 
365
                "Could not write to one or more of the target files")
 
366
 
308
367
def action_copy_or_cut(req, fields, mode):
309
368
    """Marks specified files on the clipboard, stored in the
310
369
    browser session. Sets clipboard for either a cut or copy operation
491
550
    "remove" : action_remove,
492
551
    "move" : action_move,
493
552
    "putfile" : action_putfile,
 
553
    "putfiles" : action_putfiles,
494
554
 
495
555
    "copy" : action_copy,
496
556
    "cut" : action_cut,