def _get_filter_expression(self, cr, uid, args, context=None): """Return a expression for additional filtering""" orm_model=self.pool.get(self._model) applicable_args=[] def get_applicable_args(args, index): if expression.is_leaf(args[index]): #TODO: also check for inherited fields etc if (( args[index][0] in orm_model._columns or orm_model._log_access and args[index][0] in ['create_date','create_uid', 'write_date', 'write_uid'] ) and args[index][0] not in ['text','model']): return [args[index]], 1 else: return [], 1 else: op1=get_applicable_args(args, index+1) op2=get_applicable_args(args, index+op1[1]+1) return (([args[index]] if len(op1[0]) > 0 and len(op2[0]) > 0 else []) + op1[0] + op2[0], op1[1] + op2[1] + 1 ) if openerp.release.version_info[0] <= 6: args=get_applicable_args(expression.normalize(args), 0)[0] else: args=get_applicable_args(expression.normalize_domain(args), 0)[0] return expression.expression(cr, uid, args, orm_model, context)
def assign_scores_to_leads(self, ids=[]): domain = [('running', '=', True)] if ids: domain.append(('id', 'in', ids)) scores = self.search_read(domain=domain, fields=['domain']) for score in scores: domain = safe_eval(score['domain'], evaluation_context) # Don't replace the domain with a 'not in' like below... that doesn't make the same thing !!! # domain.extend(['|', ('stage_id.on_change', '=', False), ('stage_id.probability', 'not in', [0,100])]) domain.extend(['|', ('stage_id.on_change', '=', False), '&', ('stage_id.probability', '!=', 0), ('stage_id.probability', '!=', 100)]) e = expression(self._cr, self._uid, domain, self.pool['crm.lead'], self._context) where_clause, where_params = e.to_sql() where_clause += """ AND (id NOT IN (SELECT lead_id FROM crm_lead_score_rel WHERE score_id = %s)) """ where_params.append(score['id']) self._cr.execute("""INSERT INTO crm_lead_score_rel SELECT crm_lead.id as lead_id, %s as score_id FROM crm_lead WHERE %s RETURNING lead_id""" % (score['id'], where_clause), where_params) # Force recompute of fields that depends on score_ids lead_ids = [resp[0] for resp in self._cr.fetchall()] leads = self.env["crm.lead"].browse(lead_ids) leads.modified(['score_ids']) leads.recompute()
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100): if not args: args = [] domain_expression = expression.expression(cr, uid, args, self, context) args_query_clause, args_query_params = domain_expression.to_sql() args_query_string = args_query_clause % tuple(args_query_params) if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'): # 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:] query_args = {'name': search_name} limit_str = '' if limit: limit_str = ' limit %(limit)s' query_args['limit'] = limit cr.execute('''SELECT res_partner.id FROM res_partner LEFT JOIN res_partner company ON res_partner.parent_id = company.id WHERE (res_partner.email ''' + operator +''' %(name)s OR res_partner.ref ''' + operator +''' %(name)s OR res_partner.name || ' (' || COALESCE(company.name,'') || ')' ''' + operator + ' %(name)s ) and ' + args_query_string + ' order by char_length(res_partner.ref) ' + limit_str, query_args) ids = map(lambda x: x[0], cr.fetchall()) if ids: return self.name_get(cr, uid, ids, context) return super(res_partner,self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)
def assign_scores_to_leads(self, ids=[]): domain = [('running', '=', True)] if ids: domain.append(('id', 'in', ids)) scores = self.search_read(domain=domain, fields=['domain']) for score in scores: domain = safe_eval(score['domain'], evaluation_context) # Don't replace the domain with a 'not in' like below... that doesn't make the same thing !!! # domain.extend(['|', ('stage_id.on_change', '=', False), ('stage_id.probability', 'not in', [0,100])]) domain.extend([ '|', ('stage_id.on_change', '=', False), '&', ('stage_id.probability', '!=', 0), ('stage_id.probability', '!=', 100) ]) e = expression(self._cr, self._uid, domain, self.pool['crm.lead'], self._context) where_clause, where_params = e.to_sql() where_clause += """ AND (id NOT IN (SELECT lead_id FROM crm_lead_score_rel WHERE score_id = %s)) """ where_params.append(score['id']) self._cr.execute( """INSERT INTO crm_lead_score_rel SELECT crm_lead.id as lead_id, %s as score_id FROM crm_lead WHERE %s RETURNING lead_id""" % (score['id'], where_clause), where_params) # Force recompute of fields that depends on score_ids lead_ids = [resp[0] for resp in self._cr.fetchall()] leads = self.env["crm.lead"].browse(lead_ids) leads.modified(['score_ids']) leads.recompute()
def assign_scores_to_leads(self, ids=[]): domain = [('running', '=', True)] if ids: domain.append(('id', 'in', ids)) scores = self.search_read(domain=domain, fields=['domain']) for score in scores: domain = safe_eval(score['domain'], evaluation_context) domain.extend([('active', '=', True)]) e = expression(self._cr, self._uid, domain, self.pool['crm.lead'], self._context) where_clause, where_params = e.to_sql() where_clause += """ AND (id NOT IN (SELECT lead_id FROM crm_lead_score_rel WHERE score_id = %s)) """ where_params.append(score['id']) if not self.event_based: # Only check leads that are newer than the last matching lead. # Could be based on a "last run date" for a more precise optimization where_clause += """ AND (id > (SELECT COALESCE(max(lead_id), 0) FROM crm_lead_score_rel WHERE score_id = %s)) """ where_params.append(score['id']) self._cr.execute("""INSERT INTO crm_lead_score_rel SELECT crm_lead.id as lead_id, %s as score_id FROM crm_lead WHERE %s RETURNING lead_id""" % (score['id'], where_clause), where_params) # Force recompute of fields that depends on score_ids lead_ids = [resp[0] for resp in self._cr.fetchall()] leads = self.env["crm.lead"].browse(lead_ids) leads.modified(['score_ids']) leads.recompute()