25 from datetime import datetime, date, time, timedelta |
25 from datetime import datetime, date, time, timedelta |
26 from decimal import Decimal |
26 from decimal import Decimal |
27 |
27 |
28 from logilab.common import attrdict |
28 from logilab.common import attrdict |
29 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
29 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
30 IntervalBoundConstraint, BoundConstraint, |
30 IntervalBoundConstraint, BoundaryConstraint, |
31 Attribute, actual_value) |
31 Attribute, actual_value) |
32 from rql.utils import decompose_b26 as base_decompose_b26 |
32 from rql.utils import decompose_b26 as base_decompose_b26 |
33 |
33 |
34 from cubicweb import Binary |
34 from cubicweb import Binary |
35 from cubicweb.schema import RQLConstraint |
35 from cubicweb.schema import RQLConstraint |
183 maxvalue = base + index * step |
183 maxvalue = base + index * step |
184 if minvalue is None: |
184 if minvalue is None: |
185 minvalue = maxvalue - (index * step) # i.e. randint(-index, 0) |
185 minvalue = maxvalue - (index * step) # i.e. randint(-index, 0) |
186 return choice(list(custom_range(minvalue, maxvalue, step))) |
186 return choice(list(custom_range(minvalue, maxvalue, step))) |
187 |
187 |
188 def _actual_boundary(self, entity, boundary): |
188 def _actual_boundary(self, entity, attrname, boundary): |
189 if isinstance(boundary, Attribute): |
189 if isinstance(boundary, Attribute): |
190 # ensure we've a value for this attribute |
190 # ensure we've a value for this attribute |
191 self.generate_attribute_value(entity, boundary.attr) |
191 entity[attrname] = None # infinite loop safety belt |
|
192 if not boundary.attr in entity: |
|
193 self.generate_attribute_value(entity, boundary.attr) |
192 boundary = actual_value(boundary, entity) |
194 boundary = actual_value(boundary, entity) |
193 return boundary |
195 return boundary |
194 |
196 |
195 def get_bounds(self, entity, attrname): |
197 def get_bounds(self, entity, attrname): |
196 minvalue = maxvalue = None |
198 minvalue = maxvalue = None |
197 for cst in self.eschema.rdef(attrname).constraints: |
199 for cst in self.eschema.rdef(attrname).constraints: |
198 if isinstance(cst, IntervalBoundConstraint): |
200 if isinstance(cst, IntervalBoundConstraint): |
199 minvalue = self._actual_boundary(entity, cst.minvalue) |
201 minvalue = self._actual_boundary(entity, attrname, cst.minvalue) |
200 maxvalue = self._actual_boundary(entity, cst.maxvalue) |
202 maxvalue = self._actual_boundary(entity, attrname, cst.maxvalue) |
201 elif isinstance(cst, BoundConstraint): |
203 elif isinstance(cst, BoundaryConstraint): |
202 if cst.operator[0] == '<': |
204 if cst.operator[0] == '<': |
203 maxvalue = self._actual_boundary(entity, cst.boundary) |
205 maxvalue = self._actual_boundary(entity, attrname, cst.boundary) |
204 else: |
206 else: |
205 minvalue = self._actual_boundary(entity, cst.boundary) |
207 minvalue = self._actual_boundary(entity, attrname, cst.boundary) |
206 return minvalue, maxvalue |
208 return minvalue, maxvalue |
207 |
209 |
208 def get_choice(self, entity, attrname): |
210 def get_choice(self, entity, attrname): |
209 """generates a consistent value for 'attrname' if it has some static |
211 """generates a consistent value for 'attrname' if it has some static |
210 vocabulary set, else return None. |
212 vocabulary set, else return None. |