class FormInputView(BaseFormView): def __init__(self, context, request): super(FormInputView, self).__init__(context, request) self._form = ComposedForm(self.context, request) # non-intuitive, but the way to tell the renderer not to wrap # the form is to tell it that it is already wrapped. alsoProvides(self._form, IWrappedForm) def fieldnames(self): return getFieldNamesInOrder(self.definition.schema) def groups(self): return self._form.groups def render_form(self): for group in self._form.groups: fieldgroup = self._form.components.groups[group.__name__] if fieldgroup.group_usage != 'grid': group.updateWidgets() self._form.updateWidgets() # note: by this point, updateWidgets() may be called twice: # once by form update prior to above, and then by above; this # appears to have no adverse consequence. if self._form.save_attempt: data, errors = self._form.extractData() return self._form.render() def update(self, *args, **kwargs): self._form.update(*args, **kwargs)
def test_composed(self): request, library, definition, group_a, group_b = self._fixtures() # modify schema (via XML of definition, group) entries, trigger # load of dynamic schema via event handlers from uu.dynamicschema: definition.entry_schema = DEFINITION_SCHEMA notify(ObjectModifiedEvent(definition)) assert 'title' in getFieldNamesInOrder(definition.schema) group_a.entry_schema = GROUP_A_GRID_SCHEMA notify(ObjectModifiedEvent(group_a)) assert 'name' in getFieldNamesInOrder(group_a.schema) group_b.entry_schema = GROUP_B_FIELDSET_SCHEMA notify(ObjectModifiedEvent(group_b)) assert 'feedback' in getFieldNamesInOrder(group_b.schema) from uu.formlibrary.forms import ComposedForm # assumed: ComposedForm can get out of date when the schema of the # adapted item changes. composed.schema and # composed.additionalSchemata reflect the schema of the definition # and its contained groups AT THE TIME OF CONSTRUCTION/ADAPTATION # -- if this becomes a problem, adjust the property implementation # to be a true proxy at a later date, and adjust this test # accordingly. composed = ComposedForm(definition, request) assert len(composed.additionalSchemata) == 2 #composed.updateFields() composed.update() # group_a is a grid, which has its schema wrapped by ComposedForm # construction -- the wrapper is referenced, we want to get it: from uu.formlibrary.forms import is_grid_wrapper_schema schemas = [t[1] for t in composed.group_schemas] wrapper = [s for s in schemas if is_grid_wrapper_schema(s)][0] # 'data' is field name for wrapped datagrid as list of DictRow objects assert 'data' in wrapper assert isinstance(wrapper['data'], zope.schema.List) assert isinstance(wrapper['data'].value_type, DictRow) column_schema = wrapper['data'].value_type.schema assert column_schema == group_a.schema # with regard to wrapping, serializations for the wrapper are NOT # stored or available in the uu.dynamicschema.schema.generated module # as they are throw-away and temporary for the scope of one view # transaction. Every time you adapt a definition with ComposedForm, # a new wrapper schema will be created. # However, it should be noted that the wrapped schema providing the # field group's columns is persisted in the schema saver: from uu.dynamicschema.interfaces import ISchemaSaver saver = queryUtility(ISchemaSaver) assert saver is not None group_signature = saver.signature(group_a.schema) # consequence of schema mod above; group_a.schema saved, serialized: assert group_signature in saver.keys() group_schema_identifier = 'I%s' % group_signature from uu.dynamicschema.schema import generated #assert group_schema_identifier in generated.__dict__ dynamic = getattr(generated, group_schema_identifier, None) assert dynamic is not None assert dynamic is group_a.schema wrapper_signature = saver.signature(wrapper) assert wrapper_signature not in saver.keys() # throw-away not stored # default fieldset fields: composed_schema_fields = [f.field for f in composed.fields.values()] assert definition.schema in [ field.interface for field in composed_schema_fields ] for name, field in getFieldsInOrder(definition.schema): assert name in composed.fields # keys assert field in composed_schema_fields # each field group for group in (group_a, group_b): schema = group.schema if group.group_usage == 'grid': schema = wrapper # shortcut, we only have one grid in tests... self.assertEqual(schema['data'].required, False) formgroup = [ g for g in composed.groups if g.__name__ == group.getId() ][0] assert schema in [ field.field.interface for field in formgroup.fields.values() ] for name, field in getFieldsInOrder(schema): fullname = '.'.join((composed.getPrefix(schema), name)) assert fullname in formgroup.fields # prefixed name in keys assert field in [f.field for f in formgroup.fields.values()]
def test_composed(self): request, library, definition, group_a, group_b = self._fixtures() # modify schema (via XML of definition, group) entries, trigger # load of dynamic schema via event handlers from uu.dynamicschema: definition.entry_schema = DEFINITION_SCHEMA notify(ObjectModifiedEvent(definition)) assert 'title' in getFieldNamesInOrder(definition.schema) group_a.entry_schema = GROUP_A_GRID_SCHEMA notify(ObjectModifiedEvent(group_a)) assert 'name' in getFieldNamesInOrder(group_a.schema) group_b.entry_schema = GROUP_B_FIELDSET_SCHEMA notify(ObjectModifiedEvent(group_b)) assert 'feedback' in getFieldNamesInOrder(group_b.schema) from uu.formlibrary.forms import ComposedForm # assumed: ComposedForm can get out of date when the schema of the # adapted item changes. composed.schema and # composed.additionalSchemata reflect the schema of the definition # and its contained groups AT THE TIME OF CONSTRUCTION/ADAPTATION # -- if this becomes a problem, adjust the property implementation # to be a true proxy at a later date, and adjust this test # accordingly. composed = ComposedForm(definition, request) assert len(composed.additionalSchemata) == 2 #composed.updateFields() composed.update() # group_a is a grid, which has its schema wrapped by ComposedForm # construction -- the wrapper is referenced, we want to get it: from uu.formlibrary.forms import is_grid_wrapper_schema schemas = [t[1] for t in composed.group_schemas] wrapper = [s for s in schemas if is_grid_wrapper_schema(s)][0] # 'data' is field name for wrapped datagrid as list of DictRow objects assert 'data' in wrapper assert isinstance(wrapper['data'], zope.schema.List) assert isinstance(wrapper['data'].value_type, DictRow) column_schema = wrapper['data'].value_type.schema assert column_schema == group_a.schema # with regard to wrapping, serializations for the wrapper are NOT # stored or available in the uu.dynamicschema.schema.generated module # as they are throw-away and temporary for the scope of one view # transaction. Every time you adapt a definition with ComposedForm, # a new wrapper schema will be created. # However, it should be noted that the wrapped schema providing the # field group's columns is persisted in the schema saver: from uu.dynamicschema.interfaces import ISchemaSaver saver = queryUtility(ISchemaSaver) assert saver is not None group_signature = saver.signature(group_a.schema) # consequence of schema mod above; group_a.schema saved, serialized: assert group_signature in saver.keys() group_schema_identifier = 'I%s' % group_signature from uu.dynamicschema.schema import generated #assert group_schema_identifier in generated.__dict__ dynamic = getattr(generated, group_schema_identifier, None) assert dynamic is not None assert dynamic is group_a.schema wrapper_signature = saver.signature(wrapper) assert wrapper_signature not in saver.keys() # throw-away not stored # default fieldset fields: composed_schema_fields = [f.field for f in composed.fields.values()] assert definition.schema in [ field.interface for field in composed_schema_fields] for name, field in getFieldsInOrder(definition.schema): assert name in composed.fields # keys assert field in composed_schema_fields # each field group for group in (group_a, group_b): schema = group.schema if group.group_usage == 'grid': schema = wrapper # shortcut, we only have one grid in tests... self.assertEqual( schema['data'].required, False ) formgroup = [ g for g in composed.groups if g.__name__ == group.getId() ][0] assert schema in [ field.field.interface for field in formgroup.fields.values()] for name, field in getFieldsInOrder(schema): fullname = '.'.join((composed.getPrefix(schema), name)) assert fullname in formgroup.fields # prefixed name in keys assert field in [f.field for f in formgroup.fields.values()]