def rename_node(node_id, new_filename, use_ust=True, callback=None): """ Rename node to new_filename Arguments: node_id: node to rename new_filename: filename to remove to (can be both directory and file) use_ust: if True, process remove in User Synchronous mode """ node, db_sess = _get_node(node_id) l.info("rename_node: '%s' -> '%s'" % (node.db_path, new_filename, )) if use_ust: if django_settings.ASYNC_DROPBOX: yield gen.Task(async_ust_start, node.user) else: ust_start(node.user) # TODO: Dropbox exception's text contains HTML, which can't be parsed # TODO: correctly by frontend, use Reason (part of exception dict) try: # First rename on dropbox new_db_path = get_dropbox_path(os.path.split(node.db_path)[0], new_filename) from_path = encode_if_unicode(node.db_path) to_path = encode_if_unicode(new_db_path) if django_settings.ASYNC_DROPBOX: yield_key = object() db_sess.move_file(from_path, to_path, callback=(yield gen.Callback(yield_key))) ret_dict = yield gen.Wait(yield_key) else: db_sess.db_client.file_move(from_path, to_path) # Then on local storage new_src = get_server_src_file_path(node.user, new_db_path) forced_rename(node.srv_src, new_src) new_html = get_server_html_file_path(node.user, new_db_path) if new_html != "": forced_rename(node.srv_html, new_html) # And the object itself MDBTree.objects.filter(pk=node.pk).update( db_path = new_db_path, srv_src = new_src, srv_html = new_html, ) except Exception, e: err_msg = "rename_node: '%s' -> '%s' (%s)" %\ (node.db_path, new_filename, e,) l.error(err_msg) raise MDBException("ERROR: "+err_msg)
def add_file_from_dropbox(parent_node_id, db_file_path, db_last_modified, use_ust=True, callback=None): """ Add new file from dropbox to our database and sync it to server Arguments: parent_node_id: dir where the new file should be created db_file_path: full path to dropbox file db_last_modified: taken from dropbox to remember last mod date for next sync use_ust: if True, process remove in User Synchronous mode """ parent_node, db_sess = _get_node(parent_node_id, need_db_sess=False) if use_ust: if django_settings.ASYNC_DROPBOX: yield gen.Task(async_ust_start, parent_node.user) else: ust_start(parent_node.user) # Add new node to treebeard node = parent_node.add_child ( db_path = db_file_path, is_file = True, last_modified = db_last_modified, srv_src = get_server_src_file_path(parent_node.user, db_file_path), srv_html = get_server_html_file_path(parent_node.user, db_file_path), user = parent_node.user ) try: # Sync also creates the file on server if it does not exists if django_settings.ASYNC_DROPBOX: yield_key = object() sync_file_from_dropbox(node.id, use_ust=False, callback=(yield gen.Callback(yield_key))) yield gen.Wait(yield_key) else: sync_file_from_dropbox(node.id, use_ust=False) except Exception, e: err_msg = "ADD_file_from_dropbox: '%s' (%s)" %\ (node.db_path, e,) l.error(err_msg) raise MDBException("ERROR: "+err_msg)