def test_processFields_permissionChecks_w_prefix(self): form = Form(None, None) form.groups = () class schema(Interface): title = zope.schema.TextLine() schema.setTaggedValue(WRITE_PERMISSIONS_KEY, {'title': 'foo'}) processFields(form, schema, prefix='prefix', permissionChecks=True) self.assertEqual('foo', self.secman.checks.pop()) self.assertFalse('prefix.title' in form.fields)
def update(self): # FIXME: stupid autoform thinks if the schema is in schema # attribute and not in additionalSchemata it won't need a # prefix # self.updateFieldsFromSchemata() # # use processFields instead processFields(self, self.schema, prefix=self.toolkit) #, permissionChecks=have_user) super(ExperimentParamGroup, self).update()
def update(self): # FIXME: stupid autoform thinks if the schema is in schema # attribute and not in additionalSchemata it won't need a # prefix # self.updateFieldsFromSchemata() # # stupid autoform thinks self.groups should be a list and not a tuple # :( self.groups = list(self.groups) # use processFields instead # , permissionChecks=have_user) processFields(self, self.schema, prefix=self.toolkit) # revert back to tuple self.groups = tuple(self.groups) super(ExperimentParamGroup, self).update()
def test_processFields_fieldsets_as_form_groups(self): form = Form(None, None) form.groups = [] class schema(Interface): title = zope.schema.TextLine() fieldset = Fieldset('custom', label=u'Custom', fields=['title']) schema.setTaggedValue(FIELDSETS_KEY, [fieldset]) class subschema(schema): subtitle = zope.schema.TextLine() fieldset = Fieldset('custom', label=u'Custom', fields=['subtitle']) subschema.setTaggedValue(FIELDSETS_KEY, [fieldset]) processFields(form, subschema, prefix='prefix', permissionChecks=True) self.assertEqual(len(form.groups), 1) self.assertEqual(len(form.groups[0].fields), 2) self.assertEqual([g.__name__ for g in form.groups], ['custom'])
def test_fieldset_configuration(self): """Test, if fieldsets can be orderd via fieldset configuration on a schema without fields. This schema should also not be included in form groups. """ form = Form(None, None) form.groups = [] class schema1(Interface): title = zope.schema.TextLine() fs1 = Fieldset('fs1', label=u'fs1', fields=['title']) schema1.setTaggedValue(FIELDSETS_KEY, [fs1]) class schema2(Interface): subtitle = zope.schema.TextLine() fs2 = Fieldset('fs2', label=u'fs2', fields=['subtitle']) schema2.setTaggedValue(FIELDSETS_KEY, [fs2]) class schema3(Interface): pass fs3 = Fieldset('fs1', order=2) fs4 = Fieldset('fs2', order=1) schema3.setTaggedValue(FIELDSETS_KEY, [fs3, fs4]) processFields(form, schema1, prefix='prefix', permissionChecks=True) processFields(form, schema2, prefix='prefix', permissionChecks=True) processFields(form, schema3, prefix='prefix', permissionChecks=True) self.assertEqual(len(form.groups), 2) self.assertEqual(form.groups[0].__name__, 'fs1') self.assertEqual(form.groups[0].order, 2) self.assertEqual(form.groups[1].__name__, 'fs2') self.assertEqual(form.groups[1].order, 1)
def updateFieldsFromSchemata(self): # If the form is called from the ++widget++ traversal namespace, # we won't have a user yet. In this case, we can't perform permission # checks. have_user = bool(self.request.get('AUTHENTICATED_USER', False)) # Turn fields into an instance variable, since we will be modifying it self.fields = field.Fields(self.fields) # Copy groups to an instance variable and ensure that we have # the more mutable factories, rather than 'Group' subclasses groups = [] for g in self.groups: group_name = getattr(g, '__name__', g.label) fieldset_group = GroupFactory(group_name, field.Fields(g.fields), g.label, getattr(g, 'description', None)) groups.append(fieldset_group) # Copy to instance variable only after we have potentially read from # the class self.groups = groups prefixes = {} # Set up all widgets, modes, omitted fields and fieldsets if self.schema is not None: processFields(self, self.schema, permissionChecks=have_user) for schema in self.additionalSchemata: # Find the prefix to use for this form and cache for next round prefix = self.getPrefix(schema) if prefix and prefix in prefixes: prefix = schema.__identifier__ prefixes[schema] = prefix # By default, there's no default group, i.e. fields go # straight into the default fieldset defaultGroup = None # Create groups from schemata if requested and set default # group if self.autoGroups: # use interface name, or prefix for anonymous schema group_name = schema.__name__ or prefix or None # Look for group - note that previous processFields # may have changed the groups list, so we can't easily # store this in a dict. found = False for g in self.groups: if group_name == getattr(g, '__name__', g.label): found = True break if not found: fieldset_group = GroupFactory(group_name, field.Fields(), group_name, schema.__doc__) self.groups.append(fieldset_group) defaultGroup = group_name processFields(self, schema, prefix=prefix, defaultGroup=defaultGroup, permissionChecks=have_user) # Then process relative field movements. The base schema is processed # last to allow it to override any movements made in additional # schemata. if self.schema is not None: for schema in self.additionalSchemata: processFieldMoves(self, schema, prefix=prefixes[schema]) processFieldMoves(self, self.schema)