I tested the issue with the help of GPT, and here’s what I found:
The error appears in this part of the original code:
The code where the error appears is this:
def buildInstance(item, options, instances, depth, length, index):
isChild = True
selectedValues = []
subInstances = []
for roles in range(len(item['requiredRoles'])):
for opt in range(len(options)):
if item['requiredRoles'][roles] == options[opt]['label']:
selectedValues.append(options[opt]['value'])
if len(item['items']) > 0:
isChild = False
#dict matches the view params of Menu Items
dict = {
"viewName": item['label']['text'],
"target": item['target'],
"dropdownItems": options,
"selectedValues": selectedValues,
"isChild": isChild,
"length":length,
"index":index,
"depth": depth,
"tagPath":item['path'],
"iconPath":item['label']['icon']['path'],
"requiredRoles": item['requiredRoles']
}
instances.append(dict)
if len(item['items']) > 0:
length = len(item['items'])
for j in range(len(item['items'])):
index = j
buildInstance(item['items'][j], options, instances, depth+1, length, index)
instances = []
items = value
rolesDict = {}
roles = system.user.getRoles("default") #get all possible roles from user source
options= [] #rolesDict will be appended to this to fill in the dropdown options
depth = 0
#create dropdown items object
for r in range(len(roles)):
rolesDict = {"value":r, "label":roles[r]}
options.append(rolesDict.copy())
length = len(items)
for i in range(len(items)):
index = i
buildInstance(items[i], options, instances, depth, length, index)
return instances
The error appears in this part of the original code:
for roles in range(len(item['requiredRoles'])):
The issue is that when you delete all the rolesitem['requiredRoles']
is not an array but a boolean (False
). To handle this, I updated the code as follows:
requiredRoles = item.get('requiredRoles', [])
if isinstance(requiredRoles, bool):
requiredRoles = []
Here’s the final code I tried, and it seems to work:
def buildInstance(item, options, instances, depth, length, index):
isChild = True
selectedValues = []
subInstances = []
requiredRoles = item.get('requiredRoles', [])
if isinstance(requiredRoles, bool):
requiredRoles = []
for roles in range(len(requiredRoles)):
for opt in range(len(options)):
if requiredRoles[roles] == options[opt]['label']:
selectedValues.append(options[opt]['value'])
if len(item['items']) > 0:
isChild = False
#dict matches the view params of Menu Items
dict = {
"viewName": item['label']['text'],
"target": item['target'],
"dropdownItems": options,
"selectedValues": selectedValues,
"isChild": isChild,
"length": length,
"index": index,
"depth": depth,
"tagPath": item['path'],
"iconPath": item['label']['icon']['path'],
"requiredRoles": requiredRoles
}
instances.append(dict)
if len(item['items']) > 0:
length = len(item['items'])
for j in range(len(item['items'])):
index = j
buildInstance(item['items'][j], options, instances, depth + 1, length, index)
instances = []
items = value
rolesDict = {}
roles = system.user.getRoles("default") # get all possible roles from user source
options = [] # rolesDict will be appended to this to fill in the dropdown options
depth = 0
#create dropdown items object
for r in range(len(roles)):
rolesDict = {"value": r, "label": roles[r]}
options.append(rolesDict.copy())
length = len(items)
for i in range(len(items)):
index = i
buildInstance(items[i], options, instances, depth, length, index)
return instances
return instances
Please give me your feedback about this.
Thank you