~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-03 11:38:23 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:387
Implemented file uploads.
fileservice: Added "putfiles" action (slightly different to putfile action;
    see comments at top which discuss the difference).
browser/listing: Added "upload" button to side panel, which opens an upload
    panel with a file selection dialog. This uses a little trick (documented
    in the code) to get the upload to work without refreshing the page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
288
288
 
289
289
    Reads fields: 'path', 'data' (file upload)
290
290
    """
 
291
    # TODO: Read field "unpack".
291
292
    # Important: Data is "None" if the file submitted is empty.
292
293
    path = fields.getfirst('path')
293
294
    data = fields.getfirst('data')
305
306
    except OSError:
306
307
        raise ActionError("Could not write to target file")
307
308
 
 
309
def action_putfiles(req, fields):
 
310
    """Writes data to one or more files in a directory, overwriting them if
 
311
    it they exist.
 
312
 
 
313
    Reads fields: 'path', 'data' (file upload)
 
314
    """
 
315
    # TODO: Read field "unpack".
 
316
    # Important: Data is "None" if the file submitted is empty.
 
317
    path = fields.getfirst('path')
 
318
    data = fields.getlist('data')
 
319
    if path is None:
 
320
        raise ActionError("Required field missing")
 
321
    path = actionpath_to_local(req, path)
 
322
    goterror = False
 
323
 
 
324
    for datum in data:
 
325
        # Each of the uploaded files
 
326
        filepath = os.path.join(path, datum.filename)
 
327
        filedata = datum.file
 
328
 
 
329
        # Copy the contents of file object 'data' to the path 'path'
 
330
        try:
 
331
            dest = open(filepath, 'wb')
 
332
            if data is not None:
 
333
                shutil.copyfileobj(filedata, dest)
 
334
        except OSError:
 
335
            goterror = True
 
336
 
 
337
    if goterror:
 
338
        if len(data) == 1:
 
339
            raise ActionError("Could not write to target file")
 
340
        else:
 
341
            raise ActionError(
 
342
                "Could not write to one or more of the target files")
 
343
 
308
344
def action_copy_or_cut(req, fields, mode):
309
345
    """Marks specified files on the clipboard, stored in the
310
346
    browser session. Sets clipboard for either a cut or copy operation
491
527
    "remove" : action_remove,
492
528
    "move" : action_move,
493
529
    "putfile" : action_putfile,
 
530
    "putfiles" : action_putfiles,
494
531
 
495
532
    "copy" : action_copy,
496
533
    "cut" : action_cut,