def __call__(self): """Build up the schema and return the form view. """ # build the schema if it not exist if self.schema is None: if self.schema_factory is None: self.schema_factory = SettingsSchema self.schema = self.schema_factory() # append csrf token if needed if self.use_csrf_token and 'csrf_token' not in self.schema: self.schema.children.append(CSRFSchema()['csrf_token']) # enhance the schema with the given definitions for setting_obj in self.settings.settings_objs: node = colander.SchemaNode( self.colander_type(setting_obj.type)(), name=setting_obj.field_name, title=setting_obj.title, description=setting_obj.description, default=setting_obj.default ) self.schema.children.append(node) # the names of the children should begin with the module name for child in self.schema.children: if child.name == 'csrf_token': continue if not child.name.startswith(self.settings.module): child.name = "%s-%s" % (self.settings.module, child.name) # Build up the buttons dynamically, so we can check what setting form # was saved. save = 'save_' + self.form_id self.buttons = ( deform.Button(save, _(u'Save')), deform.Button('cancel', _(u'Cancel'))) setattr(self, save + '_success', self.save_success) return super(SettingsFormView, self).__call__()
import colander import deform from kotti_settings import _ # hold the setting objects SETTINGS = [] # Here we define some default schemas. slot_names = ( ("left", _(u"left")), ("right", _(u"right")), ("abovecontent", _(u"abovecontent")), ("belowcontent", _(u"belowcontent")), ("beforebodyend", _(u"beforebodyend")), ) class SlotSchemaNode(colander.SchemaNode): name = "slot" title = _(u"Direction") default = u"left" widget = deform.widget.SelectWidget(values=slot_names) show_in_context = ( ("everywhere", _(u"Everywhere")), ("only on root", _(u"Only on root")), ("not on root", _(u"Not on root")), ("nowhere", _(u"Nowhere")), )
def cancel_success(self, appstruct): self.request.session.flash(_(u'No changes made.'), 'info') location = "%s@@settings" % self.request.resource_url(self.context) raise HTTPFound(location=location)