[form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changesete136d392bd71
branchdefault
phasepublic
hiddenno
parent revision#277c8a8eab21 fix merge, forgot to save a buffer :(
child revision#605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
files modified by this revision
web/formfields.py
web/test/unittest_formfields.py
web/uicfg.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1276172207 -7200
# Thu Jun 10 14:16:47 2010 +0200
# Node ID e136d392bd71f7cf3b6ac3b4ed7170d6d13a2112
# Parent 277c8a8eab21663ed8739c114e4242b73925510c
[form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field

diff --git a/web/formfields.py b/web/formfields.py
@@ -1082,22 +1082,17 @@
1          return eids
2 
3 
4  _AFF_KWARGS = uicfg.autoform_field_kwargs
5 
6 -def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs):
7 +def guess_field(eschema, rschema, role='subject', **kwargs):
8      """This function return the most adapted field to edit the given relation
9      (`rschema`) where the given entity type (`eschema`) is the subject or object
10      (`role`).
11 
12      The field is initialized according to information found in the schema,
13      though any value can be explicitly specified using `kwargs`.
14 -
15 -    The `skip_meta_attr` flag is used to specify wether this function should
16 -    return a field for attributes considered as a meta-attributes
17 -    (e.g. describing an other attribute, such as the format or file name of a
18 -    file (`Bytes`) attribute).
19      """
20      fieldclass = None
21      rdef = eschema.rdef(rschema, role)
22      if role == 'subject':
23          targetschema = rdef.object
@@ -1115,12 +1110,10 @@
24          kwargs.setdefault('label', (eschema.type, rschema.type + '_object'))
25      else:
26          kwargs.setdefault('label', (eschema.type, rschema.type))
27      kwargs.setdefault('help', rdef.description)
28      if rschema.final:
29 -        if skip_meta_attr and rschema in eschema.meta_attributes():
30 -            return None
31          fieldclass = FIELDS[targetschema]
32          if fieldclass is StringField:
33              if eschema.has_metadata(rschema, 'format'):
34                  # use RichTextField instead of StringField if the attribute has
35                  # a "format" metadata. But getting information from constraints
@@ -1142,11 +1135,10 @@
36              for metadata in KNOWN_METAATTRIBUTES:
37                  metaschema = eschema.has_metadata(rschema, metadata)
38                  if metaschema is not None:
39                      metakwargs = _AFF_KWARGS.etype_get(eschema, metaschema, 'subject')
40                      kwargs['%s_field' % metadata] = guess_field(eschema, metaschema,
41 -                                                                skip_meta_attr=False,
42                                                                  **metakwargs)
43          return fieldclass(**kwargs)
44      return RelationField.fromcardinality(card, **kwargs)
45 
46 
diff --git a/web/test/unittest_formfields.py b/web/test/unittest_formfields.py
@@ -13,13 +13,11 @@
47  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
48  # details.
49  #
50  # You should have received a copy of the GNU Lesser General Public License along
51  # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
52 -"""unittests for cw.web.formfields
53 -
54 -"""
55 +"""unittests for cw.web.formfields"""
56 
57  from logilab.common.testlib import TestCase, unittest_main, mock_object as mock
58 
59  from yams.constraints import StaticVocabularyConstraint, SizeConstraint
60 
@@ -51,14 +49,14 @@
61          description_field = guess_field(schema['State'], schema['description'])
62          self.assertIsInstance(description_field, RichTextField)
63          self.assertEquals(description_field.required, False)
64          self.assertEquals(description_field.format_field, None)
65 
66 +        # description_format_field = guess_field(schema['State'], schema['description_format'])
67 +        # self.assertEquals(description_format_field, None)
68 +
69          description_format_field = guess_field(schema['State'], schema['description_format'])
70 -        self.assertEquals(description_format_field, None)
71 -
72 -        description_format_field = guess_field(schema['State'], schema['description_format'], skip_meta_attr=False)
73          self.assertEquals(description_format_field.internationalizable, True)
74          self.assertEquals(description_format_field.sort, True)
75 
76  #         wikiid_field = guess_field(schema['State'], schema['wikiid'])
77  #         self.assertIsInstance(wikiid_field, StringField)
@@ -86,16 +84,16 @@
78          self.assertEquals(owned_by_field.required, False)
79          self.assertEquals(owned_by_field.role, 'object')
80 
81 
82      def test_file_fields(self):
83 -        data_format_field = guess_field(schema['File'], schema['data_format'])
84 -        self.assertEquals(data_format_field, None)
85 -        data_encoding_field = guess_field(schema['File'], schema['data_encoding'])
86 -        self.assertEquals(data_encoding_field, None)
87 -        data_name_field = guess_field(schema['File'], schema['data_name'])
88 -        self.assertEquals(data_name_field, None)
89 +        # data_format_field = guess_field(schema['File'], schema['data_format'])
90 +        # self.assertEquals(data_format_field, None)
91 +        # data_encoding_field = guess_field(schema['File'], schema['data_encoding'])
92 +        # self.assertEquals(data_encoding_field, None)
93 +        # data_name_field = guess_field(schema['File'], schema['data_name'])
94 +        # self.assertEquals(data_name_field, None)
95 
96          data_field = guess_field(schema['File'], schema['data'])
97          self.assertIsInstance(data_field, FileField)
98          self.assertEquals(data_field.required, True)
99          self.assertIsInstance(data_field.format_field, StringField)
diff --git a/web/uicfg.py b/web/uicfg.py
@@ -216,10 +216,16 @@
100          sectdict = _formsections_as_dict(formsections)
101          if rschema in META_RTYPES:
102              sectdict.setdefault('main', 'hidden')
103              sectdict.setdefault('muledit', 'hidden')
104              sectdict.setdefault('inlined', 'hidden')
105 +        elif role == 'subject' and rschema in sschema.meta_attributes():
106 +            # meta attribute, usually embeded by the described attribute's field
107 +            # (eg RichTextField, FileField...)
108 +            sectdict.setdefault('main', 'hidden')
109 +            sectdict.setdefault('muledit', 'hidden')
110 +            sectdict.setdefault('inlined', 'hidden')
111          # ensure we have a tag for each form type
112          if not 'main' in sectdict:
113              if not rschema.final and (
114                  sectdict.get('inlined') == 'attributes' or
115                  'inlined_attributes' in self.init_get(sschema, rschema, oschema,