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 = []
# Get the required roles
requiredRoles = item.get('requiredRoles', [])
if isinstance(requiredRoles, bool):
requiredRoles = [] # If it's a boolean, set it to an empty list
# Check if any of the options match the required roles
for roles in range(len(requiredRoles)):
for opt in range(len(options)):
if requiredRoles[roles] == options[opt]['label']:
selectedValues.append(options[opt]['value'])
# If the item has child items, it's not a leaf (isChild = False)
if len(item['items']) > 0:
isChild = False
# Create a dictionary matching the view parameters for 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 the item has sub-items, recursively process them
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)
# Main function logic
instances = []
items = value # Assuming 'value' is already defined somewhere in your code
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 each role
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
Please give me your feedback about this.
Thank you