def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] domain = [] if name: domain = ['|', ('name', operator, name), ('partner_ref', operator, name)] purchase_order_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(purchase_order_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): self = self.with_user(name_get_uid or self.env.uid) # as the implementation is in SQL, we force the recompute of fields if necessary self.recompute(['display_name']) self.flush() if args is None: args = [] order_by_rank = self.env.context.get('res_partner_search_mode') if (name or order_by_rank) and operator in ('=', 'ilike', '=ilike', 'like', '=like'): self.check_access_rights('read') where_query = self._where_calc(args) self._apply_ir_rules(where_query, 'read') from_clause, where_clause, where_clause_params = where_query.get_sql() from_str = from_clause if from_clause else 'res_partner' where_str = where_clause and (" WHERE %s AND " % where_clause) or ' WHERE ' # search on the name of the contacts and of its company search_name = name if operator in ('ilike', 'like'): search_name = '%%%s%%' % name if operator in ('=ilike', '=like'): operator = operator[1:] unaccent = get_unaccent_wrapper(self.env.cr) fields = self._get_name_search_order_by_fields() query = """SELECT res_partner.id FROM {from_str} {where} ({email} {operator} {percent} OR {display_name} {operator} {percent} OR {reference} {operator} {percent} OR {vat} {operator} {percent}) -- don't panic, trust postgres bitmap ORDER BY {fields} {display_name} {operator} {percent} desc, {display_name} """.format(from_str=from_str, fields=fields, where=where_str, operator=operator, email=unaccent('res_partner.email'), display_name=unaccent('res_partner.display_name'), reference=unaccent('res_partner.ref'), percent=unaccent('%s'), vat=unaccent('res_partner.vat'),) where_clause_params += [search_name]*3 # for email / display_name, reference where_clause_params += [re.sub('[^a-zA-Z0-9]+', '', search_name) or None] # for vat where_clause_params += [search_name] # for order by if limit: query += ' limit %s' where_clause_params.append(limit) self.env.cr.execute(query, where_clause_params) partner_ids = [row[0] for row in self.env.cr.fetchall()] if partner_ids: return models.lazy_name_get(self.browse(partner_ids)) else: return [] return super(Partner, self)._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] if self.env.context.get('country_id'): args = expression.AND([ args, [('country_id', '=', self.env.context.get('country_id'))] ]) if operator == 'ilike' and not (name or '').strip(): first_domain = [] domain = [] else: first_domain = [('code', '=ilike', name)] domain = [('name', operator, name)] first_state_ids = self._search( expression.AND([first_domain, args]), limit=limit, access_rights_uid=name_get_uid) if first_domain else [] state_ids = first_state_ids + [ state_id for state_id in self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) if not state_id in first_state_ids ] return models.lazy_name_get( self.browse(state_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] if name: # Be sure name_search is symetric to name_get name = name.split(' / ')[-1] args = [('name', operator, name)] + args partner_category_ids = self._search(args, limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(partner_category_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): # TDE FIXME: currently overriding the domain; however as it includes a # search on a m2o and one on a m2m, probably this will quickly become # difficult to compute - check if performance optimization is required if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'): args = args or [] domain = ['|', ('attribute_id', operator, name), ('value_ids', operator, name)] attribute_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(attribute_ids).with_user(name_get_uid)) return super(ProductTemplateAttributeLine, self)._name_search(name=name, args=args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if name and operator == '=' and not args: # search on the name of the pricelist and its currency, opposite of name_get(), # Used by the magic context filter in the product search view. query_args = { 'name': name, 'limit': limit, 'lang': get_lang(self.env).code } query = """SELECT p.id FROM (( SELECT pr.id, pr.name FROM product_pricelist pr JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE pr.name || ' (' || cur.name || ')' = %(name)s ) UNION ( SELECT tr.res_id as id, tr.value as name FROM ir_translation tr JOIN product_pricelist pr ON ( pr.id = tr.res_id AND tr.type = 'model' AND tr.name = 'product.pricelist,name' AND tr.lang = %(lang)s ) JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE tr.value || ' (' || cur.name || ')' = %(name)s ) ) p ORDER BY p.name""" if limit: query += " LIMIT %(limit)s" self._cr.execute(query, query_args) ids = [r[0] for r in self._cr.fetchall()] # regular search() to apply ACLs - may limit results below limit in some cases pricelist_ids = self._search([('id', 'in', ids)], limit=limit, access_rights_uid=name_get_uid) if pricelist_ids: return models.lazy_name_get( self.browse(pricelist_ids).with_user(name_get_uid)) return super(Pricelist, self)._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if operator not in ('ilike', 'like', '=', '=like', '=ilike'): return super(AccountAnalyticAccount, self)._name_search(name, args, operator, limit, name_get_uid=name_get_uid) args = args or [] if operator == 'ilike' and not (name or '').strip(): domain = [] else: # `partner_id` is in auto_join and the searches using ORs with auto_join fields doesn't work # we have to cut the search in two searches ... https://github.com/coffice/coffice/issues/25175 partner_ids = self.env['res.partner']._search([('name', operator, name)], limit=limit, access_rights_uid=name_get_uid) domain = ['|', '|', ('code', operator, name), ('name', operator, name), ('partner_id', 'in', partner_ids)] analytic_account_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(analytic_account_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if isinstance(name, str) and name.split(':')[0] == self._name: record_ids = self._search( [('value', operator, int(name.split(':')[1]))], access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(record_ids).with_user(name_get_uid)) else: return []
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] if operator == 'ilike' and not (name or '').strip(): domain = [] else: domain = [ '|', ('name', operator, name), ('driver_id.name', operator, name) ] rec = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(rec).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] equipment_ids = [] if name: equipment_ids = self._search([('name', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) if not equipment_ids: equipment_ids = self._search([('name', operator, name)] + args, limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(equipment_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] domain = [] if name: domain = [ '|', ('bic', '=ilike', name + '%'), ('name', operator, name) ] if operator in expression.NEGATIVE_TERM_OPERATORS: domain = ['&'] + domain bank_ids = self._search(domain + args, limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(bank_ids).with_user(name_get_uid))
def location_name_search(self, name='', args=None, operator='ilike', limit=100): if args is None: args = [] records = self.browse() if len(name) == 2: records = self.search([('code', 'ilike', name)] + args, limit=limit) search_domain = [('name', operator, name)] if records: search_domain.append(('id', 'not in', records.ids)) records += self.search(search_domain + args, limit=limit) # the field 'display_name' calls name_get() to get its value return models.lazy_name_get(records)
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): """ search full name and barcode """ args = args or [] if operator == 'ilike' and not (name or '').strip(): domain = [] else: domain = [ '|', ('barcode', operator, name), ('complete_name', operator, name) ] location_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(location_ids).with_user(name_get_uid))
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if not args: args = [] if name: positive_operators = ['=', 'ilike', '=ilike', 'like', '=like'] product_ids = [] if operator in positive_operators: product_ids = self._search([('default_code', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) if not product_ids: product_ids = self._search([('barcode', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) if not product_ids and operator not in expression.NEGATIVE_TERM_OPERATORS: # Do not merge the 2 next lines into one single search, SQL search performance would be abysmal # on a database with thousands of matching products, due to the huge merge+unique needed for the # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table # Performing a quick memory merge of ids in Python will give much better performance product_ids = self._search(args + [('default_code', operator, name)], limit=limit) if not limit or len(product_ids) < limit: # we may underrun the limit because of dupes in the results, that's fine limit2 = (limit - len(product_ids)) if limit else False product2_ids = self._search( args + [('name', operator, name), ('id', 'not in', product_ids)], limit=limit2, access_rights_uid=name_get_uid) product_ids.extend(product2_ids) elif not product_ids and operator in expression.NEGATIVE_TERM_OPERATORS: domain = expression.OR([ [ '&', ('default_code', operator, name), ('name', operator, name) ], [ '&', ('default_code', '=', False), ('name', operator, name) ], ]) domain = expression.AND([args, domain]) product_ids = self._search(domain, limit=limit, access_rights_uid=name_get_uid) if not product_ids and operator in positive_operators: ptrn = re.compile('(\[(.*?)\])') res = ptrn.search(name) if res: product_ids = self._search( [('default_code', '=', res.group(2))] + args, limit=limit, access_rights_uid=name_get_uid) # still no results, partner in context: search on supplier info as last hope to find something if not product_ids and self._context.get('partner_id'): suppliers_ids = self.env['product.supplierinfo']._search( [('name', '=', self._context.get('partner_id')), '|', ('product_code', operator, name), ('product_name', operator, name)], access_rights_uid=name_get_uid) if suppliers_ids: product_ids = self._search( [('product_tmpl_id.seller_ids', 'in', suppliers_ids)], limit=limit, access_rights_uid=name_get_uid) else: product_ids = self._search(args, limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(product_ids).with_user(name_get_uid))