def edit_rackobject(context, request): rackobform = form.Form(context, request) rackobform.fields = models.RackObject.__type__.fieldset rackobform.label = 'Edit object' def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: return form.context.title = data['title'] form.context.label = data['label'] form.context.objtype = data['objtype'] rackobform.buttons.add_action('Update', action=updateAction) rackobform.buttons.add_action('Back', action=cancelAction) rackobform.content = { 'title': context.title, 'label': context.label, 'objtype': context.objtype } return rackobform()
def form_view(context, request): myform = form.Form(context, request) # define fields for form myform.fields = form.Fieldset( form.TextField('title', title=u'Title'), # field title form.TextAreaField( 'description', title=u'Description', missing=u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified form.TextField( 'email', title=u'E-Mail', description=u'Please provide email address.', validator=form.Email(), # email validator ), ) # form actions def cancelAction(form): raise HTTPFound(location='.') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return pprint(data) form.context.title = data['title'] form.context.description = data['description'] form.message('Content has been updated.', 'info') raise HTTPFound(location='.') myform.buttons.add_action('Update', action=updateAction) myform.buttons.add_action('Cancel', action=cancelAction) # form default values myform.content = { 'title': context.title, 'description': context.description } # prepare form myform.update() # render form res = myform.render() # optional, render form in layout layout = view.LayoutRenderer('') return layout(context, request, res)
def contact_us(context, request): contactform = form.Form(context, request) contactform.label = 'Contact us' contactform.fields = form.Fieldset( form.TextField('fullname', title=u'First & Last Name'), form.TextField('phone', title=u'Telephone number', description=u'Please provide telephone number', validator=Telephone()), form.TextField('email', title=u'Your email', description=u'Please provide email address.', validator=form.Email()), form.TextAreaField( 'subject', title=u'How can we help?', missing=u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified ) # form actions def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return # form.context is ... form.context.fullname = data['fullname'] form.context.phone = data['phone'] form.context.email = data['email'] form.context.subject = data['subject'] # You would add any logic/database updates/insert here. # You would probably also redirect. log.info('The form was updated successfully') form.message('The form was updated successfully') contactform.buttons.add_action('Contact us', action=updateAction, actype=form.AC_PRIMARY) contactform.buttons.add_action('Cancel', action=cancelAction) # form default values contactform.content = {} return contactform()
def form_view(context, request): myform = form.Form(context, request) # define fields for form myform.fields = form.Fieldset( form.TextField( 'title', title = u'Title'), # field title form.TextAreaField( 'description', title = u'Description', missing = u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified form.TextField( 'email', title = u'E-Mail', description = u'Please provide email address.', validator = form.Email(), # email validator ), ) # form actions def cancel_action(form): form.message('Cancel button', 'info') def update_action(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return pprint(data) form.message('Content has been updated.', 'info') return HTTPFound(location='.') myform.buttons.add_action('Update', action=update_action, actype=ptah.form.AC_PRIMARY) myform.buttons.add_action('Cancel', action=cancel_action) # form default values myform.content = {'title': 'Test title', 'description': 'Context description'} # render form myform.update() return {'view': myform}
def login_form(request): login_form = form.Form(None, request) login_form.title = 'Login' login_form.fields = form.Fieldset( form.TextField( 'login', title = u'Login Name', description = 'Login names are case sensitive, '\ 'make sure the caps lock key is not enabled.', default = u''), form.PasswordField( 'password', title = u'Password', description = 'Case sensitive, make sure caps '\ 'lock is not enabled.', default = u''), ) def loginAction(form): request = form.request data, errors = form.extract() if errors: form.message(errors, 'form-error') return info = ptah.auth_service.authenticate(data) if info.status: headers = security.remember(request, info.principal.uri) return HTTPFound(headers = headers, location = request.application_url) if info.message: form.message(info.message, 'warning') return form.message('You enter wrong login or password.', 'error') login_form.buttons.add_action('Log in', action=loginAction) res = login_form.update() if isinstance(res, HTTPFound): return res return {'rendered_login_form': login_form.render(), 'user': ptah.resolve(authenticated_userid(request))}
def add_object(context, request): rackobform = form.Form(context, request) rackobform.fields = models.RackObject.__type__.fieldset def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return obj = models.RackObject(title=data['title'], label=data['label'], objtype=data['objtype']) ptah.get_session().add(obj) form.message('RackObject has been created.') return HTTPFound(location='/') rackobform.label = u'Add object' rackobform.buttons.add_action('Add', action=updateAction) rackobform.buttons.add_action('Cancel', action=cancelAction) result = rackobform.update() # prepare form for rendering if isinstance(result, HTTPFound): return result rendered_form = rackobform.render() ptah.include(request, 'bootstrap') rendered_includes = ptah.render_includes(request) return { 'objects': ptah.get_session().query(models.RackObject), 'rendered_form': rendered_form, 'rendered_includes': rendered_includes, 'rendered_messages': ptah.render_messages(request) }
def add_link(context, request): linkform = form.Form(context,request) linkform.fields = models.Link.__type__.fieldset def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return link = models.Link(title = data['title'], href = data['href'], color = data['color']) ptah.get_session().add(link) form.message('Link has been created.') return HTTPFound(location='/') linkform.label = u'Add link' linkform.buttons.add_action('Add', action=updateAction) linkform.buttons.add_action('Cancel', action=cancelAction) result = linkform.update() # prepare form for rendering if isinstance(result, HTTPFound): return result rendered_form = linkform.render() ptah.include(request, 'bootstrap') rendered_includes = ptah.render_includes(request) return {'links': ptah.get_session().query(models.Link), 'rendered_form': rendered_form, 'rendered_includes': rendered_includes, 'rendered_messages': ptah.render_messages(request)}
def edit_link(context, request): linkform = form.Form(context,request) linkform.fields = models.Link.__type__.fieldset def backAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return form.context.title = data['title'] form.context.href = data['href'] form.context.color = data['color'] form.message('Link has been updated.') linkform.label = u'Edit link' linkform.buttons.add_action('Update', action=updateAction) linkform.buttons.add_action('Back', action=backAction) linkform.content = {'title':context.title, 'href':context.href, 'color':context.color} result = linkform.update() # prepare form for rendering if isinstance(result, HTTPFound): return result rendered_form = linkform.render() ptah.include(request, 'bootstrap') rendered_includes = ptah.render_includes(request) return {'links': ptah.get_session().query(models.Link), 'rendered_form': rendered_form, 'rendered_includes': rendered_includes, 'rendered_messages': ptah.render_messages(request)}
def contact_us(context, request): contactform = form.Form(context, request) contactform.fields = form.Fieldset( form.TextField( 'fullname', title = u'First & Last Name'), form.TextField( 'phone', title = u'Telephone number', description=u'Please provide telephone number', validator = Telephone()), form.TextField( 'email', title = u'Your email', description = u'Please provide email address.', validator = form.Email()), form.TextAreaField( 'subject', title = u'How can we help?', missing = u''), # field use this value is request doesnt contain # field value, effectively field is required # if `missing` is not specified ) # form actions def cancelAction(form): return HTTPFound(location='/') def updateAction(form): data, errors = form.extract() if errors: form.message(errors, 'form-error') return # form.context is ... form.context.fullname = data['fullname'] form.context.phone = data['phone'] form.context.email = data['email'] form.context.subject = data['subject'] # You would add any logic/database updates/insert here. # You would probably also redirect. log.info('The form was updated successfully') form.message('The form was updated successfully') contactform.label = u'Contact us' contactform.buttons.add_action('Update', action=updateAction) contactform.buttons.add_action('Cancel', action=cancelAction) # form default values contactform.content = {} # prepare form result = contactform.update() if isinstance(result, HTTPFound): return result # render form into HTML rendered_form = contactform.render() # query for links to populate links box links = ptah.get_session().query(models.Link) #include library dependencies ptah.include(request, 'bootstrap') # render all the included libraries into html rendered_includes = ptah.render_includes(request) # render messages rendered_messages = ptah.render_messages(request) return {'links': links, 'rendered_form': rendered_form, 'rendered_messages': rendered_messages, 'rendered_includes': rendered_includes}