""" The Form class is a helper class that takes parameters and method calls and can return html for a form with appropriate hooks for css styling. It should allow you to display a form but have the formatting and styling added by the class. You can worry less about how it looks while still getting consistent, decent-looking forms. Take a look at the FirstBoot class for an example of forms in action. Copyright 2011-2013 James Vasile This software is released to you (yes, you) under the terms of the GNU Affero General Public License, version 3 or later, available at . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. """ class Form(): def __init__(self, action=None, cls='form', title=None, onsubmit=None, name=None, message='', method="get"): """Note that there appears to be a bug in cherrypy whereby forms submitted via post don't have their fields included in kwargs for the default index method. So we use get by default, though it's not as neat. TODO: file bug on this w/ CherryPy project """ action = self.get_form_attrib_text('action', action) onsubmit = self.get_form_attrib_text('onsubmit', onsubmit) name = self.get_form_attrib_text('name', name) self.pretext = '
\n' % (cls, method, action, onsubmit, name) if title: self.pretext += '

%s

\n' % title if message: self.message = "

%s

" % message else: self.message = '' self.text = '' self.end_text = "
\n" def get_form_attrib_text(self, field, val): if val: return ' %s="%s"' % (field, val) else: return '' def html(self, html): self.text += html def dropdown(self, label='', name=None, id=None, vals=None, select=None, onchange=''): """vals is a list of values. select is the index in vals of the selected item. None means no item is selected yet.""" name, id = self.name_or_id(name, id) self.text += (""" \n""" def dotted_quad(self, label='', name=None, id=None, quad=None): name, id = self.name_or_id(name, id) if not quad: quad = [0,0,0,0] self.text += """ """ % {'label':label, 'name':name, 'id':id, 'q0':quad[0], 'q1':quad[1], 'q2':quad[2], 'q3':quad[3]} def text_input(self, label='', name=None, id=None, type='text', value='', size=20): name, id = self.name_or_id(name, id) if type=="hidden": self.text += '' % (type, name, id, value) else: self.text += """ """ % (label, type, name, id, value, size) def hidden(self, name=None, id=None, value=''): self.text_input(type="hidden", name=name, id=id, value=value) def text_box(self, label='', name=None, id=None, value=""): name, id = self.name_or_id(name, id) self.text += """ """ % (label, name, id, value) def submit(self, label='', name=None, id=None): name, id = self.name_or_id(name, id) self.text += """
\n""" % (label, name, id) def submit_row(self, buttons): """buttons is a list of tuples, each containing label, name, id. Name and id are optional.""" self.text += '
' % button_text def name_or_id(self, name, id): if not name: name = id if not id: id = name if not name: name = '' if not id: id = '' return name, id def checkbox(self, label='', name='', id='', checked=''): name, id = self.name_or_id(name, id) if checked: checked = 'checked="on"' self.text += """
\n""" % (label, name, id, checked) def get_checkbox(self, name='', id=''): return '\n' % self.name_or_id(name, id) def render(self): return self.pretext+self.message+self.text+self.end_text