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

« back to all changes in this revision

Viewing changes to ivle/fileservice_lib/action.py

  • Committer: William Grant
  • Date: 2009-02-24 13:29:44 UTC
  • Revision ID: grantw@unimelb.edu.au-20090224132944-lm29zd1li1rjg77p
Privileges (apart from admin) are now offering-local, not global.

Offering privileges are granted by Enrolment.role, and global admin
by User.admin. ivle.caps is dead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
import pysvn
136
136
 
137
137
from ivle import (util, studpath, zip)
 
138
from ivle.fileservice_lib.exceptions import WillNotOverwrite
138
139
import ivle.conf
139
140
 
 
141
 
140
142
def get_login(_realm, existing_login, _may_save):
141
143
    """Callback function used by pysvn for authentication.
142
144
    realm, existing_login, _may_save: The 3 arguments passed by pysvn to
264
266
    except shutil.Error:
265
267
        raise ActionError("Could not move the file specified")
266
268
 
 
269
def svn_movefile(req, frompath, topath, copy=False):
 
270
    """Performs an svn move, resolving filenames, checking for any errors,
 
271
    and throwing ActionErrors if necessary. Can also be used to do a copy
 
272
    operation instead.
 
273
 
 
274
    frompath and topath are straight paths from the client. Will be checked.
 
275
    """
 
276
    if frompath is None or topath is None:
 
277
        raise ActionError("Required field missing")
 
278
    frompath = actionpath_to_local(req, frompath)
 
279
    topath = actionpath_to_local(req, topath)
 
280
    if not os.path.exists(frompath):
 
281
        raise ActionError("The source file does not exist")
 
282
    if os.path.exists(topath):
 
283
        if frompath == topath:
 
284
            raise ActionError("Source and destination are the same")
 
285
        raise ActionError("A file already exists with that name")
 
286
 
 
287
    try:
 
288
        if copy:
 
289
            svnclient.copy(frompath, topath)
 
290
        else:
 
291
            svnclient.move(frompath, topath)
 
292
    except OSError:
 
293
        raise ActionError("Could not move the file specified")
 
294
    except pysvn.ClientError:
 
295
        raise ActionError("Could not move the file specified")  
 
296
 
 
297
 
267
298
### ACTIONS ###
268
299
 
269
300
def action_delete(req, fields):
371
402
 
372
403
    Reads fields: 'path', 'data' (file upload, multiple), 'unpack'
373
404
    """
374
 
 
375
405
    # Important: Data is "None" if the file submitted is empty.
376
406
    path = fields.getfirst('path')
377
407
    data = fields['data']
390
420
    for datum in data:
391
421
        # Each of the uploaded files
392
422
        filepath = os.path.join(path, datum.filename)
 
423
        (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
 
424
        if os.path.isdir(filepath_local):
 
425
            raise ActionError("A directory already exists "
 
426
                    + "with that name")
 
427
        else:
 
428
            if os.path.exists(filepath_local):
 
429
                raise ActionError("A file already exists with that name")
393
430
        filedata = datum.file
394
431
 
395
432
        if unpack and datum.filename.lower().endswith(".zip"):
404
441
                zip.unzip(abspath, filedata)
405
442
            except (OSError, IOError):
406
443
                goterror = True
 
444
            except WillNotOverwrite, e:
 
445
                raise ActionError("File '" + e.filename + "' already exists.")
407
446
        else:
408
447
            # Not a zip file
409
448
            (_, _, filepath_local) = studpath.url_to_jailpaths(filepath)
445
484
    files = fields.getlist('file')
446
485
    if dst is None or src is None or mode is None:
447
486
        raise ActionError("Required field missing")
448
 
    if mode == "copy":
449
 
        copy = True
450
 
    elif mode == "move":
451
 
        copy = False
452
 
    else:
453
 
        raise ActionError("Invalid mode (must be 'copy' or 'move')")
 
487
 
454
488
    dst_local = actionpath_to_local(req, dst)
455
489
    if not os.path.isdir(dst_local):
456
490
        raise ActionError("dst is not a directory")
465
499
        # The destination is found by taking just the basename of the file
466
500
        topath = os.path.join(dst, os.path.basename(file))
467
501
        try:
468
 
            movefile(req, frompath, topath, copy)
 
502
            if mode == "copy":
 
503
                movefile(req, frompath, topath, True)
 
504
            elif mode == "move":
 
505
                movefile(req, frompath, topath, False)
 
506
            elif mode == "svncopy":
 
507
                svn_movefile(req, frompath, topath, True)
 
508
            elif mode == "svnmove":
 
509
                svn_movefile(req, frompath, topath, False)
 
510
            else:
 
511
                raise ActionError("Invalid mode (must be '(svn)copy' or '(svn)move')")
469
512
        except ActionError, message:
470
513
            # Store the error for later; we want to copy as many as possible
471
514
            if errormsg is None:
710
753
            raise util.IVLEError(404, 'The specified repository path does not exist')
711
754
        else:
712
755
            raise ActionError(str(e[0]))
 
756
            
713
757
 
714
758
# Table of all action functions #
715
759
# Each function has the interface f(req, fields).