replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 21 Dec 2009 20:04:33 +0100
changeset 4162 d2663bcf5306
parent 4161 4273f5094651
child 4163 b2747ed057e6
replace form_field_[encoding|vocabulary] methods on form by encoding|vocabylary(form) methods on fields
web/formfields.py
web/views/forms.py
--- a/web/formfields.py	Mon Dec 21 20:00:18 2009 +0100
+++ b/web/formfields.py	Mon Dec 21 20:04:33 2009 +0100
@@ -298,12 +298,55 @@
             vocab = vocab_sort(vocab)
         return vocab
 
+    def format(self, form):
+        """return MIME type used for the given (text or bytes) field"""
+        if self.eidparam and self.role == 'subject':
+            entity = form.edited_entity
+            if entity.e_schema.has_metadata(self.name, 'format') and (
+                entity.has_eid() or '%s_format' % self.name in entity):
+                return form.edited_entity.attr_metadata(self.name, 'format')
+        return form._cw.property_value('ui.default-text-format')
+
+    def encoding(self, form):
+        """return encoding used for the given (text) field"""
+        if self.eidparam:
+            entity = form.edited_entity
+            if entity.e_schema.has_metadata(self.name, 'encoding') and (
+                entity.has_eid() or '%s_encoding' % self.name in entity):
+                return form.edited_entity.attr_metadata(self.name, 'encoding')
+        return form._cw.encoding
+
     def form_init(self, form):
         """method called before by build_context to trigger potential field
         initialization requiring the form instance
         """
         pass
 
+    def has_been_modified(self, form):
+        if self.is_visible():
+            # fields not corresponding to an entity attribute / relations
+            # are considered modified
+            if not self.eidparam or not self.role or not form.edited_entity.has_eid():
+                return True # XXX
+            try:
+                if self.role == 'subject':
+                    previous_value = getattr(form.edited_entity, self.name)
+                else:
+                    previous_value = getattr(form.edited_entity,
+                                             'reverse_%s' % self.name)
+            except AttributeError:
+                # fields with eidparam=True but not corresponding to an actual
+                # attribute or relation
+                return True
+            # if it's a non final relation, we need the eids
+            if isinstance(previous_value, list):
+                # widget should return untyped eids
+                previous_value = set(unicode(e.eid) for e in previous_value)
+            if form.edited_entity.has_eid() and (previous_value == self.process_form_value(form)):
+                return False # not modified
+            return True
+        return False
+
     def process_form_value(self, form):
         """process posted form and return correctly typed value"""
         widget = self.get_widget(form)
@@ -419,7 +462,7 @@
         `attr`, according to user preferences
         """
         if form._cw.use_fckeditor():
-            return form.form_field_format(self) == 'text/html'
+            return self.format(form) == 'text/html'
         return False
 
     def render(self, form, renderer):
@@ -526,7 +569,7 @@
         if self.format(form) in self.editable_formats:
             data = self.typed_value(form, load_bytes=True)
             if data:
-                encoding = form.form_field_encoding(self)
+                encoding = self.encoding(form)
                 try:
                     form.formvalues[self] = unicode(data.getvalue(), encoding)
                 except UnicodeError:
@@ -551,8 +594,7 @@
         value = form._cw.form.get(self.input_name(form))
         if isinstance(value, unicode):
             # file modified using a text widget
-            encoding = form.form_field_encoding(self)
-            return Binary(value.encode(encoding))
+            return Binary(value.encode(self.encoding(form)))
         return super(EditableFileField, self).process_form_value(form)
 
 
--- a/web/views/forms.py	Mon Dec 21 20:00:18 2009 +0100
+++ b/web/views/forms.py	Mon Dec 21 20:04:33 2009 +0100
@@ -166,17 +166,6 @@
             return u'<span class="error">%s</span>' % self.form_valerror.errors[field.name]
         return u''
 
-    def form_field_format(self, field):
-        """return MIME type used for the given (text or bytes) field"""
-        return self._cw.property_value('ui.default-text-format')
-
-    def form_field_encoding(self, field):
-        """return encoding used for the given (text) field"""
-        return self._cw.encoding
-
-    def form_field_modified(self, field):
-        return field.is_visible()
-
     def _field_has_error(self, field):
         """return true if the field has some error in given validation exception
         """
@@ -256,23 +245,6 @@
             col=self.cw_col, entity=self.edited_entity)
 
 
-
-    def form_field_format(self, field):
-        """return MIME type used for the given (text or bytes) field"""
-        entity = self.edited_entity
-        if field.eidparam and entity.e_schema.has_metadata(field.name, 'format') and (
-            entity.has_eid() or '%s_format' % field.name in entity):
-            return self.edited_entity.attr_metadata(field.name, 'format')
-        return self._cw.property_value('ui.default-text-format')
-
-    def form_field_encoding(self, field):
-        """return encoding used for the given (text) field"""
-        entity = self.edited_entity
-        if field.eidparam and entity.e_schema.has_metadata(field.name, 'encoding') and (
-            entity.has_eid() or '%s_encoding' % field.name in entity):
-            return self.edited_entity.attr_metadata(field.name, 'encoding')
-        return super(EntityFieldsForm, self).form_field_encoding(field)
-    # XXX all this vocabulary handling should be on the field, no?
     def actual_eid(self, eid):
         # should be either an int (existant entity) or a variable (to be
         # created entity)
@@ -281,32 +253,6 @@
             return typed_eid(eid)
         except ValueError:
             try:
-
-    def form_field_modified(self, field):
-        if field.is_visible():
-            # fields not corresponding to an entity attribute / relations
-            # are considered modified
-            if not field.eidparam or not self.edited_entity.has_eid():
-                return True # XXX
-            try:
-                if field.role == 'subject':
-                    previous_value = getattr(self.edited_entity, field.name)
-                else:
-                    previous_value = getattr(self.edited_entity,
-                                             'reverse_%s' % field.name)
-            except AttributeError:
-                # fields with eidparam=True but not corresponding to an actual
-                # attribute or relation
-                return True
-            # if it's a non final relation, we need the eids
-            if isinstance(previous_value, list):
-                # widget should return untyped eids
-                previous_value = set(unicode(e.eid) for e in previous_value)
-            new_value = field.process_form_value(self)
-            if self.edited_entity.has_eid() and (previous_value == new_value):
-                return False # not modified
-            return True
-        return False
                 return self._cw.data['eidmap'][eid]
             except KeyError:
                 self._cw.data['eidmap'][eid] = None