Ejemplo n.º 1
0
Archivo: tasks.py Proyecto: velsa/mdbox
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)
Ejemplo n.º 2
0
Archivo: tasks.py Proyecto: velsa/mdbox
def create_new_file(parent_node_id, filename, content, use_ust=True, callback=None):
	"""
	Add new file TO dropbox and also to our database and to server
	NOTE: filename should be just the name of the file, without any
	path elements (it will be added as file in parent_node)
	Arguments:
		parent_node_id: 	dir where the file should be created
		filename:			name of new file
		content:			initial content
		use_ust: 			if True, process remove in User Synchronous mode
	"""
	parent_node, db_sess = _get_node(parent_node_id)
	l.info("create_new_file: '%s' in '%s' (%d bytes)" %\
		   (filename, parent_node.db_path, len(content), ))

	if use_ust:
		if django_settings.ASYNC_DROPBOX:
			yield gen.Task(async_ust_start, parent_node.user)
		else:
			ust_start(parent_node.user)

	try:
		# We first create file ON dropbox and then sync it back
		# to our database and server to keep the exec logic consistent
		db_file_path = get_dropbox_path(parent_node.db_path, filename)
		if django_settings.ASYNC_DROPBOX:
			yield_key = object()
			db_sess.write_file(encode_if_unicode(db_file_path), content,
				callback=(yield gen.Callback(yield_key)))
			ret_dict = yield gen.Wait(yield_key)
		else:
			ret_dict = db_sess.write_file(encode_if_unicode(db_file_path), content)
		l.info("create_new_file: '%s' in '%s' DONE. Now syncing dir..." %\
			   (filename, parent_node.db_path,  ))

		if django_settings.ASYNC_DROPBOX:
			yield_key = object()
			add_file_from_dropbox(parent_node.id, db_file_path, datetime.utcnow(), use_ust=False,
				callback=(yield gen.Callback(yield_key)))
			ret_dict = yield gen.Wait(yield_key)
		else:
			add_file_from_dropbox(parent_node.id, db_file_path, datetime.utcnow(), use_ust=False)
	except Exception, e:
		err_msg = "create_new_file: '%s' in '%s' (%s)" %\
				  (filename, parent_node.db_path, e,)
		l.error(err_msg)
		raise MDBException("ERROR: "+err_msg)
Ejemplo n.º 3
0
Archivo: tasks.py Proyecto: velsa/mdbox
def create_new_dir(parent_node_id, dir_name, use_ust=True, callback=None):
	"""
	Add new directory TO dropbox and also to our database and to server
	NOTE: directory should be just the name of the directory, without any
	path elements (it will be added in parent_node)
	Arguments:
		parent_node_id: 	dir where the new dir should be created
		filename:			name of new dir
		use_ust: 			if True, process remove in User Synchronous mode
	"""
	parent_node, db_sess = _get_node(parent_node_id)
	l.info("create_new_dir: '%s' in '%s'" %\
		   (dir_name, parent_node.db_path, ))

	if use_ust:
		if django_settings.ASYNC_DROPBOX:
			yield gen.Task(async_ust_start, parent_node.user)
		else:
			ust_start(parent_node.user)

	try:
		# First save to dropbox
		db_dir_path = get_dropbox_path(parent_node.db_path, dir_name)
		if django_settings.ASYNC_DROPBOX:
			yield_key = object()
			db_sess.create_folder(encode_if_unicode(db_dir_path),
				callback=(yield gen.Callback(yield_key)))
			ret_dict = yield gen.Wait(yield_key)
		else:
			ret_dict = db_sess.db_client.file_create_folder(encode_if_unicode(db_dir_path))
		l.info("create_new_dir: '%s' in '%s' DONE. Now syncing parent..." %\
			   (dir_name, parent_node.db_path, ))

		if django_settings.ASYNC_DROPBOX:
			yield_key = object()
			add_dir_from_dropbox(parent_node.id, db_dir_path, use_ust=False,
				callback=(yield gen.Callback(yield_key)))
			yield gen.Wait(yield_key)
		else:
			add_dir_from_dropbox(parent_node.id, db_dir_path, use_ust=False)
	except Exception, e:
		err_msg = "create_new_dir: '%s' -> '%s' (%s)" %\
				  (dir_name, parent_node.db_path, e,)
		l.error(err_msg)
		raise MDBException("ERROR: "+err_msg)