#!/usr/bin/env python # coding: utf8 from gluon.html import * from gluon.http import * from gluon.validators import * from gluon.sqlhtml import * from ajaxform import * # request, response, session, cache, T, db(s) # must be passed and cannot be imported! # { 'name' : { 'type' : 'text', 'value' : 'blah' } } class AutoForm(AjaxForm): def __init__(self, schema, translator, **attributes): AjaxForm.__init__( self, translator, **attributes) for key, field in schema.items(): input = None if field.get('type', 'text') == 'select': input = SELECT( _name = key ) if 'options' in field: for option in field['options']: if isinstance(option, dict): input.append( OPTION( option['label'], _value=option['value'] ) ) else: input.append( OPTION( option ) ) elif field.get('type', 'text') == 'textarea': input = TEXTAREA( _name = key ) else: input = INPUT( _name = key, _type = field.get('type', 'text') ) if field.get('type', 'text') == 'checkbox' or field.get('type', 'text') == 'radio': if 'checked' in field: input.attributes['_checked'] = field['checked'] if 'value' in field: input.attributes['_value'] = field['value'] if 'requires' in field: input.attributes['requires'] = field['requires'] if field.get('type', 'text') == 'hidden': self.form.append( input ) else: self.appendLine( 1, LABEL( field.get('label', key ), _class = 'ajax-form-auto-label'), input ) self.appendLine( 2, DIV( INPUT( _value=self.T('Add'), _type='button', _id='volume_ok_button', _class='submit_button' ), INPUT( _value=self.T('Cancel'), _type='button', _id='volume_cancel_button', _class='cancel_button' ), INPUT( _value=self.T('Reset'), _type='button', _id='volume_reset_button', _class='reset_button' ) ) )