Script to add parent property to every node
This commit is contained in:
@@ -289,14 +289,30 @@ def before_returning_resource_permissions(response):
|
||||
validate_token()
|
||||
check_permissions(item, 'GET', append_allowed_methods=True)
|
||||
|
||||
|
||||
|
||||
def before_replacing_node(item, original):
|
||||
check_permissions(original, 'PUT')
|
||||
|
||||
def before_inserting_nodes(items):
|
||||
"""Before inserting a node in the collection we check if the user is allowed
|
||||
and we append the project id to it.
|
||||
"""
|
||||
nodes_collection = app.data.driver.db['nodes']
|
||||
def find_parent_project(node):
|
||||
"""Recursive function that finds the ultimate parent of a node."""
|
||||
if node and 'parent' in node:
|
||||
parent = nodes_collection.find_one({'_id': node['parent']})
|
||||
return find_parent_project(parent)
|
||||
if node:
|
||||
return node
|
||||
else:
|
||||
return None
|
||||
for item in items:
|
||||
check_permissions(item, 'POST')
|
||||
if 'parent' in item:
|
||||
parent = nodes_collection.find_one({'_id': item['parent']})
|
||||
project = find_parent_project(parent)
|
||||
if project:
|
||||
item['project'] = project['_id']
|
||||
|
||||
|
||||
app.on_fetched_item_nodes += before_returning_item_permissions
|
||||
|
@@ -125,8 +125,8 @@ def process_file(src_file):
|
||||
file_abs_path = os.path.join(app.config['SHARED_DIR'], src_file['name'])
|
||||
src_file['length'] = os.stat(file_abs_path).st_size
|
||||
# Remove properties that do not belong in the collection
|
||||
del src_file['_status']
|
||||
del src_file['_links']
|
||||
src_file.pop('_status', None)
|
||||
src_file.pop('_links', None)
|
||||
content_type = src_file['content_type'].split('/')
|
||||
src_file['format'] = content_type[1]
|
||||
mime_type = content_type[0]
|
||||
@@ -217,7 +217,6 @@ def process_file(src_file):
|
||||
sync_path = file_abs_path
|
||||
remote_storage_sync(sync_path)
|
||||
|
||||
files_collection = app.data.driver.db['files']
|
||||
file_asset = files_collection.find_and_modify(
|
||||
{'_id': src_file['_id']},
|
||||
src_file)
|
||||
|
@@ -52,3 +52,6 @@ def remote_storage_sync(path): #can be both folder and file
|
||||
else:
|
||||
if os.path.exists(path):
|
||||
rsync(path)
|
||||
else:
|
||||
raise IOError('ERROR: path not found')
|
||||
|
||||
|
@@ -1165,5 +1165,43 @@ def add_node_permissions():
|
||||
print node['_id']
|
||||
break
|
||||
|
||||
@manager.command
|
||||
def add_parent_to_nodes():
|
||||
import codecs
|
||||
import sys
|
||||
from bson.objectid import ObjectId
|
||||
UTF8Writer = codecs.getwriter('utf8')
|
||||
sys.stdout = UTF8Writer(sys.stdout)
|
||||
|
||||
nodes_collection = app.data.driver.db['nodes']
|
||||
def find_parent_project(node):
|
||||
if node and 'parent' in node:
|
||||
parent = nodes_collection.find_one({'_id': node['parent']})
|
||||
return find_parent_project(parent)
|
||||
if node:
|
||||
return node
|
||||
else:
|
||||
return None
|
||||
nodes = nodes_collection.find()
|
||||
nodes_index = 0
|
||||
nodes_orphan = 0
|
||||
for node in nodes:
|
||||
nodes_index += 1
|
||||
if node['node_type'] == ObjectId("55a615cfea893bd7d0489f2d"):
|
||||
print u"Skipping project node - {0}".format(node['name'])
|
||||
else:
|
||||
project = find_parent_project(node)
|
||||
if project:
|
||||
nodes_collection.update({'_id': node['_id']},
|
||||
{"$set": {'project': project['_id']}})
|
||||
print u"{0} {1}".format(node['_id'], node['name'])
|
||||
else:
|
||||
nodes_orphan += 1
|
||||
nodes_collection.remove({'_id': node['_id']})
|
||||
print "Removed {0} {1}".format(node['_id'], node['name'])
|
||||
|
||||
print "Edited {0} nodes".format(nodes_index)
|
||||
print "Orphan {0} nodes".format(nodes_orphan)
|
||||
|
||||
if __name__ == '__main__':
|
||||
manager.run()
|
||||
|
Reference in New Issue
Block a user