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 textPreview(request): field = form.TextField('TextField', title='Text field', description='Text field preview description', default='Test text in text field.') widget = field.bind('preview.', form.null, {}) widget.update(request) return widget.snippet('form-widget', widget)
class MyForm(form.Form): view.pview('test-form.html', context=cms.Content) # define fields for form 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 default values def form_content(self): return { 'title': self.context.title, 'description': self.context.description } @form.button('Update') def update_handler(self): data, errors = self.extract() if errors: self.message(errors, 'form-error') return pprint(data) self.context.title = data['title'] self.context.description = data['description'] self.message('Content has been updated.', 'info') raise HTTPFound(location='.') @form.button('Cancel') def cancel_handler(self): raise HTTPFound(location='.')
def test_tinfo_fieldset(self): import ptah, ptah.cms MySchema = ptah.cms.ContentSchema + \ form.Fieldset(form.TextField('test')) class MyContent(ptah.cms.Content): __type__ = ptah.cms.Type('mycontent2', 'MyContent', fieldset=MySchema) tinfo = MyContent.__type__ self.assertIs(tinfo.fieldset, MySchema)
def test_sqlschema_custom(self): import ptah field = form.TextField('name', title='Custom') class Test3(SqlaBase): __tablename__ = 'test3' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field': field}) fieldset = ptah.generate_fieldset(Test3) m_field = fieldset['name'] self.assertEqual(m_field.name, 'name') self.assertEqual(m_field.title, 'Custom') self.assertIs(m_field, field)
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))}
import ptah from ptah import form import permissions from themefile import ThemeFile, ThemeFileSchema class Email(form.Email): def __call__(self, field, value): if value != '': super(Email, self).__call__(field, value) ThemeSchema = form.Fieldset( ptah.cms.ContentSchema, form.TextField('author', title='Author name'), form.TextField('email', missing='', title='Author email', validator=Email()), form.TextField('url', missing='', title='Web site'), form.FileField('preview', title='Preview image', missing=''), ) class Theme(ptah.cms.Container): __tablename__ = 'ploud_themes' __type__ = ptah.cms.Type( 'diazo-theme',
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}
from ptah import form from ptah.testing import PtahTestCase field = form.TextField('test', title='Test node') field1 = form.TextField('test1', title='Test node') class TestFieldset(PtahTestCase): def test_fieldset_name_title(self): fieldset = form.Fieldset(field) self.assertEqual(fieldset.name, '') self.assertEqual(fieldset.title, '') self.assertEqual(fieldset.prefix, '') fieldset = form.Fieldset(field1, name='othername', title='Other name') self.assertEqual(fieldset.name, 'othername') self.assertEqual(fieldset.title, 'Other name') def test_fieldset_nested(self): fieldset = form.Fieldset(field, form.Fieldset(name='fs', *(field, ))) self.assertEqual(fieldset['fs'].name, 'fs') self.assertEqual(fieldset['fs'].prefix, 'fs.') def test_fieldset_fields(self): fieldset = form.Fieldset(field, field1) self.assertEqual(list(fieldset.fields()), [field, field1])
def _makeOne(self, name, **kw): return form.TextField(name, **kw)