@@ -31,22 +31,28 @@ def __init__(self, parent=None):
3131 super (AssetModel , self ).__init__ (parent = parent )
3232 self .refresh ()
3333
34- def _add_hierarchy (self , parent = None ):
34+ def _add_hierarchy (self , parent = None , assets = {} ):
3535
3636 # Find the assets under the parent
37- find_data = {
38- "type" : "asset"
39- }
37+ current_assets = []
4038 if parent is None :
41- find_data ['$or' ] = [
42- {'data.visualParent' : {'$exists' : False }},
43- {'data.visualParent' : None }
44- ]
39+ db_assets = io .find ({
40+ "type" : "asset"
41+ }).sort ('name' , 1 )
42+ assets = {}
43+ for asset in db_assets :
44+ parent_id = asset .get ("data" , {}).get ("visualParent" )
45+ if parent_id :
46+ if parent_id not in assets :
47+ assets [parent_id ] = []
48+ assets [parent_id ].append (asset )
49+ else :
50+ current_assets .append (asset )
4551 else :
46- find_data ["data.visualParent" ] = parent ['_id' ]
52+ if parent ["_id" ] in assets :
53+ current_assets = assets .pop (parent ["_id" ])
4754
48- assets = io .find (find_data ).sort ('name' , 1 )
49- for asset in assets :
55+ for asset in current_assets :
5056 # get label from data, otherwise use name
5157 data = asset .get ("data" , {})
5258 label = data .get ("label" , asset ['name' ])
@@ -67,7 +73,10 @@ def _add_hierarchy(self, parent=None):
6773 self .add_child (node , parent = parent )
6874
6975 # Add asset's children recursively
70- self ._add_hierarchy (node )
76+ if asset ['_id' ] in assets :
77+ assets = self ._add_hierarchy (node , assets )
78+
79+ return assets
7180
7281 def refresh (self ):
7382 """Refresh the data for the model."""
0 commit comments