def form(self): # make configuration data available on form context self.props = ILDAPProps(self.plugin) self.users = ILDAPUsersConfig(self.plugin) self.groups = ILDAPGroupsConfig(self.plugin) # prepare users data on form context self.users_attrmap = odict() for key in self.static_attrs_users: self.users_attrmap[key] = self.users.attrmap.get(key) self.users_propsheet_attrmap = odict() for key, value in self.users.attrmap.items(): if key in self.static_attrs_users: continue self.users_propsheet_attrmap[key] = value # prepare groups data on form context self.groups_attrmap = odict() for key in self.static_attrs_groups: self.groups_attrmap[key] = self.groups.attrmap.get(key) self.groups_propsheet_attrmap = odict() for key, value in self.groups.attrmap.items(): if key in self.static_attrs_groups: continue self.groups_propsheet_attrmap[key] = value # handle form form = parse_from_YAML('pas.plugins.ldap:properties.yaml', self, _) controller = Controller(form, self.request) if not controller.next: return controller.rendered self.request.RESPONSE.redirect(controller.next) return u''
def __call__(self): # check if authenticated user is vendor if not get_vendors_for(): raise Unauthorized self.prepare() controller = Controller(self.form, self.request) if not controller.next: self.rendered_form = controller.rendered return self.browser_template(self) return controller.next
def render_auth_form(self): # Render the authentication form for anonymous users. req = self.request action = req.getURL() ordernumber = self.ordernumber or req.form.get('ordernumber', '') email = self.email or req.form.get('email', '') form = factory('form', name='order_auth_form', props={'action': action}) form['ordernumber'] = factory('div:label:error:text', value=ordernumber, props={ 'label': _('anon_auth_label_ordernumber', default=u'Ordernumber'), 'div.class': 'ordernumber', 'required': True, }) form['email'] = factory('div:label:error:text', value=email, props={ 'label': _('anon_auth_label_email', default=u'Email'), 'div.class': 'email', 'required': True, }) form['submit'] = factory('div:label:submit', props={ 'label': _('anon_auth_label_submit', default=u'Submit'), 'div.class': 'submit', 'handler': self._form_handler, 'action': 'submit', }) controller = Controller(form, req) return controller.rendered
def forms(self): result = list() for part in self.example: widget = part['widget'] form = factory( u'form', name='form-%s' % widget.name, props={ 'action': self.example_name}) form[widget.name] = widget form['submit'] = factory( 'submit', props={ 'label': 'submit', 'action': 'save', 'handler': lambda widget, data: None, 'class': 'submit-widget button-field context'}) controller = Controller(form, self.request) result.append(controller.rendered) return result
def _process_form(self): self.prepare() self.prepare_ajax() if not self.show: return u'' controller = Controller(self.form, self.request) if not controller.next: return controller.rendered if isinstance(controller.next, HTTPFound): self.redirect(controller.next) return if isinstance(controller.next, AjaxAction) \ or isinstance(controller.next, AjaxEvent): self.request.environ['cone.app.continuation'] = [controller.next] return u'' if isinstance(controller.next, list): # we assume a list of AjaxAction and/or AjaxEvent instances self.request.environ['cone.app.continuation'] = controller.next return u'' return controller.next
def form(self): # make configuration data available on form context try: self.props = ILDAPProps(self.plugin) self.users = ILDAPUsersConfig(self.plugin) self.groups = ILDAPGroupsConfig(self.plugin) except Exception: msg = "Problems getting the configuration adapters, re-initialize!" logger.exception(msg) self.plugin.init_settings() self.anonymous = not self.props.user # prepare users data on form context self.users_attrmap = odict() for key in self.static_attrs_users: self.users_attrmap[key] = self.users.attrmap.get(key) self.users_propsheet_attrmap = odict() for key, value in self.users.attrmap.items(): if key in self.static_attrs_users: continue self.users_propsheet_attrmap[key] = value # prepare groups data on form context self.groups_attrmap = odict() for key in self.static_attrs_groups: self.groups_attrmap[key] = self.groups.attrmap.get(key) self.groups_propsheet_attrmap = odict() for key, value in self.groups.attrmap.items(): if key in self.static_attrs_groups: continue self.groups_propsheet_attrmap[key] = value # handle form form = parse_from_YAML("pas.plugins.ldap:properties.yaml", self, _) controller = Controller(form, self.request) if not controller.next: return controller.rendered self.request.RESPONSE.redirect(controller.next) return u""
def test_form_blueprint(self): # Test Form form = factory('form', name='FORM', props={'action': 'http://fubar.com'}) self.assertEqual( form(), ('<form action="http://fubar.com" enctype="multipart/form-data" ' 'id="form-FORM" method="post" novalidate="novalidate"></form>')) # Form action as callable def action(widget, data): return 'http://fubar.com' form = factory('form', name='FORM', props={'action': action}) self.assertEqual( form(), ('<form action="http://fubar.com" enctype="multipart/form-data" ' 'id="form-FORM" method="post" novalidate="novalidate"></form>')) # Form display renderer form = factory('form', name='FORM', props={'action': 'http://fubar.com'}, mode='display') self.assertEqual(form(), '<div></div>') # Create a form with some children form = factory('form', name='myform', props={'action': 'http://www.domain.tld/someform'}) form['someinput'] = factory('label:text', props={'label': 'Your Text'}) self.form_data = None def formaction(widget, data): self.form_data = data def formnext(request): return 'http://www.domain.tld/result' form['submit'] = factory('submit', props={ 'handler': formaction, 'next': formnext, 'action': True }) # Render an empty form self.check_output( """ <form action="http://www.domain.tld/someform" enctype="multipart/form-data" id="form-myform" method="post" novalidate="novalidate"> <label for="input-myform-someinput">Your Text</label> <input class="text" id="input-myform-someinput" name="myform.someinput" type="text" value=""/> <input id="input-myform-submit" name="action.myform.submit" type="submit" value="submit"/> </form> """, fxml(form())) # Get form data out of request (request is expected dict-like) request = { 'myform.someinput': 'Hello World', 'action.myform.submit': 'submit' } Controller(form, request) form_data = self.form_data self.assertEqual(form_data.name, 'myform') self.assertEqual(form_data.value, UNSET) expected = odict() expected['someinput'] = 'Hello World' expected['submit'] = UNSET self.assertEqual(form_data.extracted, expected) self.assertEqual(form_data.errors, []) input_data = form_data['someinput'] self.assertEqual(input_data.name, 'someinput') self.assertEqual(input_data.value, UNSET) self.assertEqual(input_data.extracted, 'Hello World') self.assertEqual(input_data.errors, []) # submit blueprint gets a runtime data as well, but it's never needed # or used so far submit_data = form_data['submit'] self.assertEqual(submit_data.name, 'submit') self.assertEqual(submit_data.value, UNSET) self.assertEqual(submit_data.extracted, UNSET) self.assertEqual(submit_data.errors, []) del self.form_data # Form action property can be callable def action(widget, data): return 'actionfromcall' form = factory('form', name='form', props={ 'action': action, }) self.assertEqual( form(), ('<form action="actionfromcall" enctype="multipart/form-data" ' 'id="form-form" method="post" novalidate="novalidate"></form>')) # Create label for field in other compound form = factory('form', name='form', props={'action': 'action'}) form['label'] = factory('label', props={ 'label': 'Foo', 'for': 'field' }) form['field'] = factory('text') self.check_output( """ <form action="action" enctype="multipart/form-data" id="form-form" method="post" novalidate="novalidate"> <label for="input-form-field">Foo</label> <input class="text" id="input-form-field" name="form.field" type="text" value=""/> </form> """, fxml(form()))
def test_controller(self): # Dummy context class Context(object): value = 'hello world' context = Context() # Dummy getter def getter(widget, data): return data.request.context.value # Create Widget tree form = factory(u'form', name='testform', props={'action': 'http://fubar.com'}) form['field1'] = factory('text', value=getter) form['field2'] = factory('text', value='', props={'required': True}) # Define action ``handler`` def handler(widget, data): self.handler_result = 'handler called "%s"' % '.'.join(widget.path) # Define action ``next`` def next(request): return 'next return value' # Indicate widget to be an ``action`` definition by setting ``action`` # attribute to widget properties. ``expression``, ``handler`` and # ``next`` are action referring properties props = { 'action': 'save', 'expression': True, 'handler': handler, 'next': next, 'label': 'Save', 'skip': False, } # Add save action form['save'] = factory('submit', props=props) # Add cancel action. In this case we want the form processing to be # skipped and just the next action to be performed props = { 'action': 'cancel', 'expression': True, 'handler': None, 'next': next, 'label': 'Cancel', 'skip': True, } form['cancel'] = factory('submit', props=props) # Check widget tree self.assertEqual(form.treerepr().split('\n'), [ "<class 'yafowil.base.Widget'>: testform", " <class 'yafowil.base.Widget'>: field1", " <class 'yafowil.base.Widget'>: field2", " <class 'yafowil.base.Widget'>: save", " <class 'yafowil.base.Widget'>: cancel", "" ]) # Dummy request class Request(dict): context = None Request.context = context request = Request() # Render form with empty request data = form.extract(request) self.check_output( """ <form action="http://fubar.com" enctype="multipart/form-data" id="form-testform" method="post" novalidate="novalidate"> <input class="text" id="input-testform-field1" name="testform.field1" type="text" value="hello world"/> <input class="required text" id="input-testform-field2" name="testform.field2" required="required" type="text" value=""/> <input id="input-testform-save" name="action.testform.save" type="submit" value="Save"/> <input id="input-testform-cancel" name="action.testform.cancel" type="submit" value="Cancel"/> </form> """, fxml(form(data))) # Create controller for form controller = Controller(form, request) # If action is not triggered, or ``action['next']`` is not set, # ``Controller.next`` is ``None`` self.assertEqual(controller.next, None) # An empty request does not trigger validation failures self.assertFalse(controller.error) # Provide empty required field and it fails! request['testform.field2'] = '' controller = Controller(form, request) self.assertTrue(controller.error) # Provide required field and all is fine request['testform.field2'] = '1' controller = Controller(form, request) self.assertFalse(controller.error) # Trigger save action without required field request['testform.field2'] = '' request['action.testform.save'] = '1' controller = Controller(form, request) self.assertTrue(controller.error, True) self.assertTrue(controller.performed, True) # Trigger save action with valid input request['testform.field2'] = '1' controller = Controller(form, request) self.assertEqual(self.handler_result, 'handler called "testform"') self.handler_result = None self.assertEqual(controller.next, 'next return value') self.assertFalse(controller.error) self.assertTrue(controller.performed) # Render the form performed self.check_output( """ <form action="http://fubar.com" enctype="multipart/form-data" id="form-testform" method="post" novalidate="novalidate"> <input class="text" id="input-testform-field1" name="testform.field1" type="text" value="hello world"/> <input class="required text" id="input-testform-field2" name="testform.field2" required="required" type="text" value="1"/> <input id="input-testform-save" name="action.testform.save" type="submit" value="Save"/> <input id="input-testform-cancel" name="action.testform.cancel" type="submit" value="Cancel"/> </form> """, fxml(controller.rendered)) # Trigger cancel action. performing is skipped del request['action.testform.save'] request['action.testform.cancel'] = '1' controller = Controller(form, request) self.assertEqual(controller.next, 'next return value') self.assertFalse(controller.performed) # Render form not performed self.check_output( """ <form action="http://fubar.com" enctype="multipart/form-data" id="form-testform" method="post" novalidate="novalidate"> <input class="text" id="input-testform-field1" name="testform.field1" type="text" value="hello world"/> <input class="required text" id="input-testform-field2" name="testform.field2" required="required" type="text" value=""/> <input id="input-testform-save" name="action.testform.save" type="submit" value="Save"/> <input id="input-testform-cancel" name="action.testform.cancel" type="submit" value="Cancel"/> </form> """, fxml(controller.rendered)) # Try recursive lookup of actions form = factory(u'form', name='testform', props={'action': 'http://fubar.com'}) form['level1'] = factory('submit', props={'action': 'l1action'}) form['fieldset'] = factory('fieldset') form['fieldset']['level2'] = factory('submit', props={'action': 'l2action'}) form['fieldset']['subset'] = factory('fieldset') form['fieldset']['subset']['level3'] = factory( 'submit', props={'action': 'l3action'}) controller = Controller(form, {}) self.assertEqual(len(controller.actions), 3) self.assertEqual(controller.actions[0].name, 'level1') self.assertEqual(controller.actions[1].name, 'level2') self.assertEqual(controller.actions[2].name, 'level3')