connect format field to form_field_format for its initial value stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 19 May 2009 10:05:56 +0200
branchstable
changeset 1858 69e41c88e195
parent 1857 46f975203f4f
child 1859 b068abd45a1c
connect format field to form_field_format for its initial value
web/formfields.py
web/test/unittest_formfields.py
--- a/web/formfields.py	Tue May 19 10:05:19 2009 +0200
+++ b/web/formfields.py	Tue May 19 10:05:56 2009 +0200
@@ -213,20 +213,21 @@
         try:
             return req.data[self]
         except KeyError:
+            fkwargs = {}
             if self.use_fckeditor(form):
                 # if fckeditor is used and format field isn't explicitly
                 # deactivated, we want an hidden field for the format
-                widget = HiddenInput()
-                choices = None
+                fkwargs['widget'] = HiddenInput()
+                fkwargs['initial'] = 'text/html'
             else:
                 # else we want a format selector
-                # XXX compute vocabulary
-                widget = Select()
+                fkwargs['widget'] = Select()
+                fkwargs['widget'].attrs['size'] = 1
                 fcstr = FormatConstraint()
-                choices = [(req._(fmt), fmt) for fmt in fcstr.vocabulary(req=req)]
-                widget.attrs['size'] = 1
-            field = StringField(name=self.name + '_format', widget=widget,
-                                choices=choices)
+                fkwargs['choices'] = fcstr.vocabulary(req=req)
+                fkwargs['internationalizable'] = True
+                fkwargs['initial'] = lambda f: f.form_field_format(self)
+            field = StringField(name=self.name + '_format', **fkwargs)
             req.data[self] = field
             return field
 
--- a/web/test/unittest_formfields.py	Tue May 19 10:05:19 2009 +0200
+++ b/web/test/unittest_formfields.py	Tue May 19 10:05:56 2009 +0200
@@ -1,12 +1,15 @@
 """unittests for cw.web.formfields"""
 
+from logilab.common.testlib import TestCase, unittest_main
+
 from yams.constraints import StaticVocabularyConstraint, SizeConstraint
-from logilab.common.testlib import TestCase, unittest_main
+
 from cubicweb.devtools import TestServerConfiguration
+from cubicweb.devtools.testlib import EnvBasedTC
+from cubicweb.web.form import EntityFieldsForm
 from cubicweb.web.formwidgets import PasswordInput, TextArea, Select
 from cubicweb.web.formfields import *
-from cubicweb.entities.wfobjs import State
-from cubicweb.entities.authobjs import CWUser
+
 from cubes.file.entities import File
 
 config = TestServerConfiguration('data')
@@ -43,12 +46,13 @@
         self.assertEquals(description_format_field.sort, True)
         self.assertEquals(description_format_field.initial(None), 'text/rest')
 
+
 #         wikiid_field = guess_field(state_schema, schema['wikiid'])
 #         self.assertIsInstance(wikiid_field, StringField)
 #         self.assertEquals(wikiid_field.required, False)
 
 
-    def test_euser_fields(self):
+    def test_cwuser_fields(self):
         upassword_field = guess_field(cwuser_schema, schema['upassword'])
         self.assertIsInstance(upassword_field, StringField)
         self.assertIsInstance(upassword_field.widget, PasswordInput)
@@ -89,5 +93,21 @@
                           [SizeConstraint, StaticVocabularyConstraint])
         self.assertIsInstance(salesterm_field.widget, Select)
 
+class MoreFieldsTC(EnvBasedTC):
+    def test_rtf_format_field(self):
+        req = self.request()
+        req.use_fckeditor = lambda: False
+        e = self.etype_instance('State')
+        form = EntityFieldsForm(req, entity=e)
+        description_field = guess_field(state_schema, schema['description'])
+        description_format_field = description_field.get_format_field(form)
+        self.assertEquals(description_format_field.internationalizable, True)
+        self.assertEquals(description_format_field.sort, True)
+        # unlike below, initial is bound to form.form_field_format
+        self.assertEquals(description_format_field.initial(form), 'text/html')
+        self.execute('INSERT CWProperty X: X pkey "ui.default-text-format", X value "text/rest", X for_user U WHERE U login "admin"')
+        self.commit()
+        self.assertEquals(description_format_field.initial(form), 'text/rest')
+
 if __name__ == '__main__':
     unittest_main()