Draft new function has been created to search multiple criteria for a single model. If the search strategy is set to loose, the function will search in a loop for each criteria.
Search criteria is pushed into the function, in a nested dictionary. The first item in the array should be the primary search. Example below:
def object_match(obj, search_array):
"""Used to search models for multiple criteria."""
try:
result = obj.objects.get(**search_array[0])
return result
except obj.DoesNotExist as error:
if PLUGIN_SETTINGS['object_match_strategy'] == "loose":
for search_array_element in search_array[1:]:
try:
result = obj.objects.get(**search_array_element)
return result
except obj.DoesNotExist:
pass
except obj.Multiple:
raise OnboardException(reason="fail-general", message=f"ERROR multiple objects found")
raise
search_array = [
{"slug": nb_device_type_slug},
{"slug__iexact": nb_device_type_slug},
{"model__iexact": netdev_model},
{"part_number__iexact": netdev_model}
]
nb_device_type = object_match(DeviceType, search_array)
REPLACES MR #98
Draft new function has been created to search multiple criteria for a single model. If the search strategy is set to loose, the function will search in a loop for each criteria.
Search criteria is pushed into the function, in a nested dictionary. The first item in the array should be the primary search. Example below: