def process(self, value, is_value2): if value is None and self.op in (ops.between, ops.not_between): if is_value2: value = '' else: raise formencode.Invalid(gettext('invalid date'), value, self) if value is None or self.op in self.no_value_operators: return None if self.op == self.default_op and not value: return None if self.op == ops.select_month: if is_value2: return feval.Int(not_empty=False, min=1900, max=9999).to_python(value) return feval.Int(not_empty=False).to_python(value) if self.op in self.days_operators: if is_value2: return None filter_value = feval.Int(not_empty=True).to_python(value) if self.op in (ops.days_ago, ops.less_than_days_ago, ops.more_than_days_ago): try: self._get_today() - dt.timedelta(days=filter_value) except OverflowError: raise formencode.Invalid( gettext('date filter given is out of range'), value, self) if self.op in (ops.in_days, ops.in_less_than_days, ops.in_more_than_days): try: self._get_today() + dt.timedelta(days=filter_value) except OverflowError: raise formencode.Invalid( gettext('date filter given is out of range'), value, self) return filter_value try: d = ensure_date(parse(value)) if isinstance(d, (dt.date, dt.datetime)) and d.year < 1900: return feval.Int(min=1900).to_python(d.year) return d except ValueError: # allow open ranges when blanks are submitted as a second value if is_value2 and not value: return dt.date.today() raise formencode.Invalid(gettext('invalid date'), value, self)
def process(self, value, is_value2): if value is None and self.op in (ops.between, ops.not_between): if is_value2: value = '' else: raise formencode.Invalid(gettext('invalid date'), value, self) if value is None or self.op in self.no_value_operators: return None if self.op == self.default_op and not value: return None if self.op == ops.select_month: if is_value2: return feval.Int(not_empty=False, min=1900, max=9999).to_python(value) return feval.Int(not_empty=False).to_python(value) if self.op in self.days_operators: if is_value2: return None filter_value = feval.Int(not_empty=True).to_python(value) if self.op in (ops.days_ago, ops.less_than_days_ago, ops.more_than_days_ago): try: self._get_today() - dt.timedelta(days=filter_value) except OverflowError: raise formencode.Invalid(gettext('date filter given is out of range'), value, self) if self.op in (ops.in_days, ops.in_less_than_days, ops.in_more_than_days): try: self._get_today() + dt.timedelta(days=filter_value) except OverflowError: raise formencode.Invalid(gettext('date filter given is out of range'), value, self) return filter_value try: d = ensure_date(parse(value)) if isinstance(d, (dt.date, dt.datetime)) and d.year < 1900: return feval.Int(min=1900).to_python(d.year) return d except ValueError: # allow open ranges when blanks are submitted as a second value if is_value2 and not value: return dt.date.today() raise formencode.Invalid(gettext('invalid date'), value, self)
def _process_date(self, value, is_value2): try: d = ensure_date(parse(value)) if isinstance(d, (dt.date, dt.datetime)) and d.year < 1900: return feval.Int(min=1900).to_python(d.year) return d except ValueError: # allow open ranges when blanks are submitted as a second value if is_value2 and not value: return self._get_today() raise formencode.Invalid(gettext('invalid date'), value, self)
def _get_today(self): # this filter is date-only, so our "now" is a date without time return ensure_date(self._get_now())
def test_ensure_date(): dtm = dt.datetime.now() d = dt.date.today() assert ensure_date(dtm) == dtm.date() assert ensure_date(d) is d