web/formfields.py
changeset 10696 4ba4be5553cf
parent 10612 84468b90e9c1
child 10716 9a9d57edb1c1
equal deleted inserted replaced
10695:321b99973b69 10696:4ba4be5553cf
    64 __docformat__ = "restructuredtext en"
    64 __docformat__ = "restructuredtext en"
    65 
    65 
    66 from warnings import warn
    66 from warnings import warn
    67 from datetime import datetime, timedelta
    67 from datetime import datetime, timedelta
    68 
    68 
    69 from six import string_types
    69 from six import text_type, string_types
    70 
    70 
    71 from logilab.mtconverter import xml_escape
    71 from logilab.mtconverter import xml_escape
    72 from logilab.common import nullobject
    72 from logilab.common import nullobject
    73 from logilab.common.date import ustrftime
    73 from logilab.common.date import ustrftime
    74 from logilab.common.configuration import format_time
    74 from logilab.common.configuration import format_time
   279         """return value suitable for display"""
   279         """return value suitable for display"""
   280         if value is None or value is False:
   280         if value is None or value is False:
   281             return u''
   281             return u''
   282         if value is True:
   282         if value is True:
   283             return u'1'
   283             return u'1'
   284         return unicode(value)
   284         return text_type(value)
   285 
   285 
   286     def get_widget(self, form):
   286     def get_widget(self, form):
   287         """return the widget instance associated to this field"""
   287         """return the widget instance associated to this field"""
   288         return self.widget
   288         return self.widget
   289 
   289 
   794         if self.format(form) in self.editable_formats:
   794         if self.format(form) in self.editable_formats:
   795             data = self.typed_value(form, load_bytes=True)
   795             data = self.typed_value(form, load_bytes=True)
   796             if data:
   796             if data:
   797                 encoding = self.encoding(form)
   797                 encoding = self.encoding(form)
   798                 try:
   798                 try:
   799                     form.formvalues[(self, form)] = unicode(data.getvalue(), encoding)
   799                     form.formvalues[(self, form)] = data.getvalue().decode(encoding)
   800                 except UnicodeError:
   800                 except UnicodeError:
   801                     pass
   801                     pass
   802                 else:
   802                 else:
   803                     if not self.required:
   803                     if not self.required:
   804                         msg = form._cw._(
   804                         msg = form._cw._(
   815                     # XXX restore form context?
   815                     # XXX restore form context?
   816         return '\n'.join(wdgs)
   816         return '\n'.join(wdgs)
   817 
   817 
   818     def _process_form_value(self, form):
   818     def _process_form_value(self, form):
   819         value = form._cw.form.get(self.input_name(form))
   819         value = form._cw.form.get(self.input_name(form))
   820         if isinstance(value, unicode):
   820         if isinstance(value, text_type):
   821             # file modified using a text widget
   821             # file modified using a text widget
   822             return Binary(value.encode(self.encoding(form)))
   822             return Binary(value.encode(self.encoding(form)))
   823         return super(EditableFileField, self)._process_form_value(form)
   823         return super(EditableFileField, self)._process_form_value(form)
   824 
   824 
   825 
   825 
   946     widget = fw.TextInput
   946     widget = fw.TextInput
   947 
   947 
   948     def format_single_value(self, req, value):
   948     def format_single_value(self, req, value):
   949         if value:
   949         if value:
   950             value = format_time(value.days * 24 * 3600 + value.seconds)
   950             value = format_time(value.days * 24 * 3600 + value.seconds)
   951             return unicode(value)
   951             return text_type(value)
   952         return u''
   952         return u''
   953 
   953 
   954     def example_format(self, req):
   954     def example_format(self, req):
   955         """return a sample string describing what can be given as input for this
   955         """return a sample string describing what can be given as input for this
   956         field
   956         field
   993             if not value:
   993             if not value:
   994                 return None
   994                 return None
   995             try:
   995             try:
   996                 value = form._cw.parse_datetime(value, self.etype)
   996                 value = form._cw.parse_datetime(value, self.etype)
   997             except ValueError as ex:
   997             except ValueError as ex:
   998                 raise ProcessFormError(unicode(ex))
   998                 raise ProcessFormError(text_type(ex))
   999         return value
   999         return value
  1000 
  1000 
  1001 
  1001 
  1002 class DateTimeField(DateField):
  1002 class DateTimeField(DateField):
  1003     """Use this field to edit datetime (`Datetime` yams type).
  1003     """Use this field to edit datetime (`Datetime` yams type).
  1083 
  1083 
  1084     def relvoc_linkedto(self, form):
  1084     def relvoc_linkedto(self, form):
  1085         linkedto = form.linked_to.get((self.name, self.role))
  1085         linkedto = form.linked_to.get((self.name, self.role))
  1086         if linkedto:
  1086         if linkedto:
  1087             buildent = form._cw.entity_from_eid
  1087             buildent = form._cw.entity_from_eid
  1088             return [(buildent(eid).view('combobox'), unicode(eid))
  1088             return [(buildent(eid).view('combobox'), text_type(eid))
  1089                     for eid in linkedto]
  1089                     for eid in linkedto]
  1090         return []
  1090         return []
  1091 
  1091 
  1092     def relvoc_init(self, form):
  1092     def relvoc_init(self, form):
  1093         entity, rtype, role = form.edited_entity, self.name, self.role
  1093         entity, rtype, role = form.edited_entity, self.name, self.role
  1095         if not self.required:
  1095         if not self.required:
  1096             vocab.append(('', INTERNAL_FIELD_VALUE))
  1096             vocab.append(('', INTERNAL_FIELD_VALUE))
  1097         # vocabulary doesn't include current values, add them
  1097         # vocabulary doesn't include current values, add them
  1098         if form.edited_entity.has_eid():
  1098         if form.edited_entity.has_eid():
  1099             rset = form.edited_entity.related(self.name, self.role)
  1099             rset = form.edited_entity.related(self.name, self.role)
  1100             vocab += [(e.view('combobox'), unicode(e.eid))
  1100             vocab += [(e.view('combobox'), text_type(e.eid))
  1101                       for e in rset.entities()]
  1101                       for e in rset.entities()]
  1102         return vocab
  1102         return vocab
  1103 
  1103 
  1104     def relvoc_unrelated(self, form, limit=None):
  1104     def relvoc_unrelated(self, form, limit=None):
  1105         entity = form.edited_entity
  1105         entity = form.edited_entity
  1129         for entity in entity.unrelated(self.name, targettype, self.role, limit,
  1129         for entity in entity.unrelated(self.name, targettype, self.role, limit,
  1130                                        lt_infos=form.linked_to).entities():
  1130                                        lt_infos=form.linked_to).entities():
  1131             if entity.eid in done:
  1131             if entity.eid in done:
  1132                 continue
  1132                 continue
  1133             done.add(entity.eid)
  1133             done.add(entity.eid)
  1134             res.append((entity.view('combobox'), unicode(entity.eid)))
  1134             res.append((entity.view('combobox'), text_type(entity.eid)))
  1135         return res
  1135         return res
  1136 
  1136 
  1137     def format_single_value(self, req, value):
  1137     def format_single_value(self, req, value):
  1138         return unicode(value)
  1138         return text_type(value)
  1139 
  1139 
  1140     def process_form_value(self, form):
  1140     def process_form_value(self, form):
  1141         """process posted form and return correctly typed value"""
  1141         """process posted form and return correctly typed value"""
  1142         try:
  1142         try:
  1143             return form.formvalues[(self, form)]
  1143             return form.formvalues[(self, form)]