def test_reduce_domain(self): 'Test reduce_domain' clause = ('x', '=', 'x') tests = ( ([clause], ['AND', clause]), ([clause, [clause]], ['AND', clause, clause]), (['AND', clause, [clause]], ['AND', clause, clause]), ([clause, ['AND', clause]], ['AND', clause, clause]), ([clause, ['AND', clause, clause]], ['AND', clause, clause, clause]), (['AND', clause, ['AND', clause]], ['AND', clause, clause]), ([[[clause]]], ['AND', clause]), (['OR', clause], ['OR', clause]), (['OR', clause, [clause]], ['OR', clause, ['AND', clause]]), (['OR', clause, [clause, clause]], ['OR', clause, ['AND', clause, clause]]), (['OR', clause, ['OR', clause]], ['OR', clause, clause]), (['OR', clause, [clause, ['OR', clause, clause]]], ['OR', clause, ['AND', clause, ['OR', clause, clause]]]), (['OR', [clause]], ['OR', ['AND', clause]]), ([], []), (['OR', clause, []], ['OR', clause, []]), (['AND', clause, []], ['AND', clause, []]), ) for i, j in tests: self.assertEqual(reduce_domain(i), j, '%s -> %s != %s' % (i, reduce_domain(i), j))
def _search_domain_active(self, domain, active_test=True): # reduce_domain return a new instance so we can safety modify domain domain = reduce_domain(domain) # if the object has a field named 'active', filter out all inactive # records unless they were explicitely asked for if not (('active' in self._columns or 'active' in self._inherit_fields.keys()) and (active_test and Transaction().context.get('active_test', True))): return domain def process(domain): i = 0 active_found = False while i < len(domain): arg = domain[i] #add test for xmlrpc that doesn't handle tuple if isinstance(arg, tuple) or \ (isinstance(arg, list) and len(arg) > 2 and \ arg[1] in OPERATORS): if arg[0] == 'active': active_found = True elif isinstance(arg, list): domain[i] = process(domain[i]) i += 1 if not active_found: if domain and ((isinstance(domain[0], basestring) \ and domain[0] == 'AND') \ or (not isinstance(domain[0], basestring))): domain.append(('active', '=', True)) else: domain = ['AND', domain, ('active', '=', True)] return domain return process(domain)