[facet] make BitFieldFacet allow special 0 value. Closes #2522697 stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 22 Nov 2012 16:13:43 +0100
branchstable
changeset 8598 95b3792a8947
parent 8597 293a595f4365
child 8599 4fabc81cd924
[facet] make BitFieldFacet allow special 0 value. Closes #2522697
web/facet.py
web/test/unittest_facet.py
--- a/web/facet.py	Fri Nov 16 12:28:30 2012 +0100
+++ b/web/facet.py	Thu Nov 22 16:13:43 2012 +0100
@@ -1388,11 +1388,16 @@
             return
         if isinstance(value, list):
             value = reduce(lambda x, y: int(x) | int(y), value)
+        else:
+            value = int(value)
         attr_var = self.select.make_variable()
         self.select.add_relation(self.filtered_variable, self.rtype, attr_var)
         comp = nodes.Comparison('=', nodes.Constant(value, 'Int'))
-        comp.append(nodes.MathExpression('&', nodes.variable_ref(attr_var),
-                                         nodes.Constant(value, 'Int')))
+        if value == 0:
+            comp.append(nodes.variable_ref(attr_var))
+        else:
+            comp.append(nodes.MathExpression('&', nodes.variable_ref(attr_var),
+                                             nodes.Constant(value, 'Int')))
         having = self.select.having
         if having:
             self.select.replace(having[0], nodes.And(having[0], comp))
@@ -1402,7 +1407,7 @@
     def rset_vocabulary(self, rset):
         mask = reduce(lambda x, y: x | (y[0] or 0), rset, 0)
         return sorted([(self._cw._(label), val) for label, val in self.choices
-                       if val & mask])
+                       if not val or val & mask])
 
     def possible_values(self):
         return [unicode(val) for label, val in self.vocabulary()]
--- a/web/test/unittest_facet.py	Fri Nov 16 12:28:30 2012 +0100
+++ b/web/test/unittest_facet.py	Thu Nov 22 16:13:43 2012 +0100
@@ -221,6 +221,25 @@
         self.assertEqual(f.select.as_string(),
                           "DISTINCT Any  WHERE X ordernum XO, X is CWAttribute, X ordernum C HAVING 3 = (C & 3)")
 
+    def test_bitfield_0_value(self):
+        req, rset, rqlst, filtered_variable = self.prepare_rqlst(
+            'CWAttribute X WHERE X ordernum XO',
+            expected_baserql='Any X WHERE X ordernum XO, X is CWAttribute',
+            expected_preparedrql='DISTINCT Any  WHERE X ordernum XO, X is CWAttribute')
+        f = facet.BitFieldFacet(req, rset=rset,
+                                select=rqlst.children[0],
+                                filtered_variable=filtered_variable)
+        f.choices = [('zero', 0,), ('un', 1,), ('deux', 2,)]
+        f.rtype = 'ordernum'
+        self.assertEqual(f.vocabulary(),
+                          [(u'deux', 2), (u'un', 1), (u'zero', 0)])
+        self.assertEqual(f.possible_values(),
+                          ['2', '1', '0'])
+        req.form[f.__regid__] = '0'
+        f.add_rql_restrictions()
+        self.assertEqual(f.select.as_string(),
+                          "DISTINCT Any  WHERE X ordernum XO, X is CWAttribute, X ordernum C HAVING 0 = C")
+
     def test_rql_path_eid(self):
         req, rset, rqlst, filtered_variable = self.prepare_rqlst()
         class RPF(facet.RQLPathFacet):