~openerp/openerp-client-rpc-com-lib/rows

« back to all changes in this revision

Viewing changes to openerprows/criteria.py

  • Committer: Vo Minh Thu
  • Date: 2011-04-19 20:23:48 UTC
  • Revision ID: vmt@openerp.com-20110419202348-1a14chzvz0usswlx
criteria to domain conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
class criteria(object):
2
2
  def __init__(self, *ast):
3
3
    # TODO add some checking
4
 
    self.ast = tuple(ast)
 
4
    if len(ast):
 
5
      self.ast = tuple(ast)
 
6
    else:
 
7
      self.ast = True
5
8
 
6
9
  def __getattr__(self, field_name):
7
10
    return criteria_field(self, field_name)
10
13
    return str(self.ast)
11
14
 
12
15
  def __and__(self, other):
 
16
    if self.ast is True:
 
17
      return other
 
18
    elif other.ast is True:
 
19
      return self
13
20
    return criteria('&', self.ast, other.ast)
14
21
 
15
22
  def __or__(self, other):
 
23
    if self.ast is False:
 
24
      return self
 
25
    elif other.ast is False:
 
26
      return other
16
27
    return criteria('|', self.ast, other.ast)
17
28
 
 
29
  # TODO the other way around: transform a domain into a criteria and
 
30
  # use criteria as the underlying implmentation instead of a domain.
 
31
  def domain(self):
 
32
    def to_domain(ast):
 
33
      if ast is True:
 
34
        return []
 
35
      if ast[0] == '<':
 
36
        return [(to_domain1(ast[1]), '<', to_domain1(ast[2]))]
 
37
      elif ast[0] == '>':
 
38
        return [(to_domain1(ast[1]), '>', to_domain1(ast[2]))]
 
39
      elif ast[0] == '&':
 
40
        return ['&'] + to_domain(ast[1]) + to_domain(ast[2])
 
41
      elif ast[0] == 'like':
 
42
        return [(to_domain1(ast[1]), 'like', to_domain1(ast[2]))]
 
43
      return [] # TODO can't happen
 
44
 
 
45
    def to_domain1(operand):
 
46
      if isinstance(operand, tuple) and len(operand) == 2 and operand[0] == 'field':
 
47
        return operand[1]
 
48
      return operand
 
49
 
 
50
    return to_domain(self.ast)
 
51
 
18
52
class criteria_field(object):
19
53
  def __init__(self, crit, field_name):
20
54
    self.crit = crit
34
68
  def __gt__(self, other):
35
69
    return self.build_binary('>', other)
36
70
 
 
71
  def __getattr__(self, name):
 
72
    def f(strg):
 
73
      return criteria('like', ('field', self.field_name), strg)
 
74
    if name == 'like':
 
75
      return f
 
76
 
37
77
if __name__ == '__main__':
38
78
  pass