web/formfields.py
changeset 4370 75c610a85949
parent 4369 6d3dae46ee95
child 4374 ff3efacfea42
equal deleted inserted replaced
4369:6d3dae46ee95 4370:75c610a85949
   344             value = form.formvalues[self] = self._process_form_value(form)
   344             value = form.formvalues[self] = self._process_form_value(form)
   345             return value
   345             return value
   346 
   346 
   347     def _process_form_value(self, form):
   347     def _process_form_value(self, form):
   348         widget = self.get_widget(form)
   348         widget = self.get_widget(form)
   349         return widget.process_field_data(form, self)
   349         value = widget.process_field_data(form, self)
       
   350         return self._ensure_correctly_typed(form, value)
       
   351 
       
   352     def _ensure_correctly_typed(self, form, value):
       
   353         """widget might to return date as a correctly formatted string or as
       
   354         correctly typed objects, but process_for_value must return a typed value.
       
   355         Override this method to type the value if necessary
       
   356         """
       
   357         return value
   350 
   358 
   351     def process_posted(self, form):
   359     def process_posted(self, form):
   352         for field in self.actual_fields(form):
   360         for field in self.actual_fields(form):
   353             if field is self:
   361             if field is self:
   354                 try:
   362                 try:
   608         self.max = max
   616         self.max = max
   609         if isinstance(self.widget, fw.TextInput):
   617         if isinstance(self.widget, fw.TextInput):
   610             self.widget.attrs.setdefault('size', 5)
   618             self.widget.attrs.setdefault('size', 5)
   611             self.widget.attrs.setdefault('maxlength', 15)
   619             self.widget.attrs.setdefault('maxlength', 15)
   612 
   620 
   613     def _process_form_value(self, form):
   621     def _ensure_correctly_typed(self, form, value):
   614         value = Field._process_form_value(self, form)
   622         if isinstance(value, basestring):
   615         if value:
       
   616             try:
   623             try:
   617                 return int(value)
   624                 return int(value)
   618             except ValueError:
   625             except ValueError:
   619                 raise ProcessFormError(form._cw._('an integer is expected'))
   626                 raise ProcessFormError(form._cw._('an integer is expected'))
   620         return None
   627         return value
   621 
   628 
   622 
   629 
   623 class BooleanField(Field):
   630 class BooleanField(Field):
   624     widget = fw.Radio
   631     widget = fw.Radio
   625 
   632 
   626     def vocabulary(self, form):
   633     def vocabulary(self, form):
   627         if self.choices:
   634         if self.choices:
   628             return super(BooleanField, self).vocabulary(form)
   635             return super(BooleanField, self).vocabulary(form)
   629         return [(form._cw._('yes'), '1'), (form._cw._('no'), '')]
   636         return [(form._cw._('yes'), '1'), (form._cw._('no'), '')]
   630 
   637 
   631     def _process_form_value(self, form):
   638     def _ensure_correctly_typed(self, form, value):
   632         return bool(Field._process_form_value(self, form))
   639         if value is not None:
       
   640             return bool(value)
       
   641         return value
   633 
   642 
   634 
   643 
   635 class FloatField(IntField):
   644 class FloatField(IntField):
   636     def format_single_value(self, req, value):
   645     def format_single_value(self, req, value):
   637         formatstr = req.property_value('ui.float-format')
   646         formatstr = req.property_value('ui.float-format')
   640         return formatstr % float(value)
   649         return formatstr % float(value)
   641 
   650 
   642     def render_example(self, req):
   651     def render_example(self, req):
   643         return self.format_single_value(req, 1.234)
   652         return self.format_single_value(req, 1.234)
   644 
   653 
   645     def _process_form_value(self, form):
   654     def _ensure_correctly_typed(self, form, value):
   646         value = Field._process_form_value(self, form)
   655         if isinstance(value, basestring):
   647         if value:
       
   648             try:
   656             try:
   649                 return float(value)
   657                 return float(value)
   650             except ValueError:
   658             except ValueError:
   651                 raise ProcessFormError(form._cw._('a float is expected'))
   659                 raise ProcessFormError(form._cw._('a float is expected'))
   652         return None
   660         return None
   660         return value and ustrftime(value, req.property_value(self.format_prop)) or u''
   668         return value and ustrftime(value, req.property_value(self.format_prop)) or u''
   661 
   669 
   662     def render_example(self, req):
   670     def render_example(self, req):
   663         return self.format_single_value(req, datetime.now())
   671         return self.format_single_value(req, datetime.now())
   664 
   672 
   665     def _process_form_value(self, form):
   673     def _ensure_correctly_typed(self, form, value):
   666         # widget is supposed to return a date as a correctly formatted string
   674         if isinstance(value, basestring):
   667         date = Field._process_form_value(self, form)
       
   668         # but for some widgets, it might be simpler to return date objects
       
   669         # directly, so handle that case :
       
   670         if isinstance(date, basestring):
       
   671             try:
   675             try:
   672                 date = form._cw.parse_datetime(date, 'Date')
   676                 value = form._cw.parse_datetime(value, self.etype)
   673             except ValueError, ex:
   677             except ValueError, ex:
   674                 raise ProcessFormError(unicode(ex))
   678                 raise ProcessFormError(unicode(ex))
   675         return date
   679         return value
   676 
   680 
   677 
   681 
   678 class DateTimeField(DateField):
   682 class DateTimeField(DateField):
   679     format_prop = 'ui.datetime-format'
   683     format_prop = 'ui.datetime-format'
   680 
   684     etype = 'Datetime'
   681     def _process_form_value(self, form):
       
   682         # widget is supposed to return a date as a correctly formatted string
       
   683         date = Field._process_form_value(self, form)
       
   684         # but for some widgets, it might be simpler to return date objects
       
   685         # directly, so handle that case :
       
   686         if isinstance(date, basestring):
       
   687             try:
       
   688                 date = form._cw.parse_datetime(date, 'Datetime')
       
   689             except ValueError, ex:
       
   690                 raise ProcessFormError(unicode(ex))
       
   691         return date
       
   692 
   685 
   693 
   686 
   694 class TimeField(DateField):
   687 class TimeField(DateField):
   695     format_prop = 'ui.time-format'
   688     format_prop = 'ui.time-format'
   696     widget = TextInput
   689     etype = 'Time'
   697 
       
   698     def _process_form_value(self, form):
       
   699         # widget is supposed to return a date as a correctly formatted string
       
   700         time = Field._process_form_value(self, form)
       
   701         # but for some widgets, it might be simpler to return time objects
       
   702         # directly, so handle that case :
       
   703         if isinstance(time, basestring):
       
   704             try:
       
   705                 time = form._cw.parse_datetime(time, 'Time')
       
   706             except ValueError, ex:
       
   707                 raise ProcessFormError(unicode(ex))
       
   708         return time
       
   709 
   690 
   710 
   691 
   711 # relation vocabulary helper functions #########################################
   692 # relation vocabulary helper functions #########################################
   712 
   693 
   713 def relvoc_linkedto(entity, rtype, role):
   694 def relvoc_linkedto(entity, rtype, role):