@@ -713,29 +713,48 @@ def field_to_int(field, field_map):
713713 flags = re .IGNORECASE )
714714 field_map [field_name ] = int (field_id )
715715
716- for idx , c in enumerate (params ['criteria' ]):
717- if 'field' in c and c ['field' ] is not None :
718- params ["criteria[%d][field]" % idx ] = field_to_int (
719- c ['field' ], field_map )
720- else :
721- raise GlpiInvalidArgument (
722- 'Missing "field" parameter for ' + str (idx + 1 ) +
723- 'the criteria: ' + str (c ))
724-
725- # build value argument
726- params ["criteria[%d][value]" % idx ] = c .get ('value' , '' )
727-
728- # build searchtype argument
729- # -> optional! defaults to "contains" on the server if empty
730- params ["criteria[%d][searchtype]" % idx ] = c .get ('searchtype' , '' )
731-
732- # link is optional for 1st criterion according to docs...
733- # -> error if not present but more than one criterion
734- if 'link' not in c and idx > 0 :
735- raise GlpiInvalidArgument (
736- 'Missing link type for ' + str (idx + 1 )+ '. criterion ' + str (c ))
737- elif 'link' in c :
738- params ["criteria[%d][link]" % idx ] = c .get ('link' , '' )
716+ def build_params_from_criteria (criteria , prefix ):
717+ for idx , c in enumerate (criteria ):
718+ if 'criteria' in c :
719+ yield from build_params_from_criteria (
720+ c ['criteria' ],
721+ '%s[%d][criteria]' % (prefix , idx ),
722+ )
723+ elif 'field' in c and c ['field' ] is not None :
724+ yield (
725+ "%s[%d][field]" % (prefix , idx ),
726+ field_to_int (c ['field' ], field_map ),
727+ )
728+ else :
729+ raise GlpiInvalidArgument (
730+ 'Missing "field" parameter for ' + str (idx + 1 ) +
731+ 'the criteria: ' + str (c ))
732+
733+ # build value argument
734+ yield (
735+ "%s[%d][value]" % (prefix , idx ),
736+ c .get ('value' , '' ),
737+ )
738+
739+ # build searchtype argument
740+ # -> optional! defaults to "contains" on the server if empty
741+ yield (
742+ "%s[%d][searchtype]" % (prefix , idx ),
743+ c .get ('searchtype' , '' ),
744+ )
745+
746+ if 'link' in c :
747+ yield (
748+ "%s[%d][link]" % (prefix , idx ),
749+ c ['link' ],
750+ )
751+ elif idx > 0 :
752+ # link is optional for 1st criterion according to docs...
753+ # -> error if not present but more than one criterion
754+ raise GlpiInvalidArgument (
755+ 'Missing link type for ' + str (idx + 1 )+ '. criterion ' + str (c ))
756+
757+ params .update (build_params_from_criteria (params ['criteria' ], 'criteria' ))
739758
740759 del params ['criteria' ]
741760
0 commit comments