def build(self, params: List[SearchParam], obj_manager: CmdbObjectManager = None, active_flag: bool = False) -> Pipeline: """Build a pipeline query out of frontend params""" # clear pipeline self.clear() # fetch only active objects if active_flag: self.add_pipe(self.match_({'active': {"$eq": True}})) # text builds text_params = [ _ for _ in params if _.search_form == 'text' or _.search_form == 'regex' ] for param in text_params: regex = self.regex_('fields.value', param.search_text, 'ims') self.add_pipe(self.match_(regex)) # type builds disjunction_query = [] type_params = [_ for _ in params if _.search_form == 'type'] for param in type_params: if param.settings and len(param.settings.get('types', [])) > 0: type_id_in = self.in_('type_id', param.settings['types']) if param.disjunction: disjunction_query.append(type_id_in) else: self.add_pipe(self.match_(type_id_in)) if len(disjunction_query) > 0: self.add_pipe(self.match_(self.or_(disjunction_query))) # category builds category_params = [_ for _ in params if _.search_form == 'category'] for param in category_params: if param.settings and len(param.settings.get('categories', [])) > 0: categories = obj_manager.get_categories_by( **self.regex_('label', param.search_text)) for curr_category in categories: type_id_in = self.in_('type_id', curr_category.types) self.add_pipe(self.match_(type_id_in)) # public builds id_params = [_ for _ in params if _.search_form == 'publicID'] for param in id_params: self.add_pipe(self.match_({'public_id': int(param.search_text)})) return self.pipeline
def build(self, params: List[SearchParam], obj_manager: CmdbObjectManager = None, user: UserModel = None, permission: AccessControlPermission = None, active_flag: bool = False, *args, **kwargs) -> Pipeline: """Build a pipeline query out of frontend params""" # clear pipeline self.clear() # load reference fields in runtime. self.pipeline = SearchReferencesPipelineBuilder().build() # fetch only active objects if active_flag: self.add_pipe(self.match_({'active': {"$eq": True}})) # text builds text_params = [ _ for _ in params if _.search_form == 'text' or _.search_form == 'regex' ] for param in text_params: regex = self.regex_('fields.value', param.search_text, 'ims') self.add_pipe(self.match_(regex)) # type builds disjunction_query = [] type_params = [_ for _ in params if _.search_form == 'type'] for param in type_params: if param.settings and len(param.settings.get('types', [])) > 0: type_id_in = self.in_('type_id', param.settings['types']) if param.disjunction: disjunction_query.append(type_id_in) else: self.add_pipe(self.match_(type_id_in)) if len(disjunction_query) > 0: self.add_pipe(self.match_(self.or_(disjunction_query))) # category builds category_params = [_ for _ in params if _.search_form == 'category'] for param in category_params: if param.settings and len(param.settings.get('categories', [])) > 0: categories = obj_manager.get_categories_by( **self.regex_('label', param.search_text)) for curr_category in categories: type_id_in = self.in_('type_id', curr_category.types) self.add_pipe(self.match_(type_id_in)) # public builds id_params = [_ for _ in params if _.search_form == 'publicID'] for param in id_params: self.add_pipe(self.match_({'public_id': int(param.search_text)})) # permission builds if user and permission: self.pipeline = [ *self.pipeline, *(AccessControlQueryBuilder().build( group_id=PublicID(user.group_id), permission=permission)) ] return self.pipeline