import json
py-coolform is a Form generator for CoolForms. Instantiate a CoolForm object and add pages, lines, fields, validators... Once you are done dump the form to JSON
Note that you have to call the methods in the right order:
1 page -> 2 line -> 3 field -> 4validator
For example to create a form and print it's json output do:
import coolforms
myform = coolforms.CoolForm("myform", "/truc")
myform.page("First Page", "this is my first page")
myform.line()
myform.field("first_name", "text", "First Name", 2)
myform.field("last_name", "text", "Last Name", 2)
myform.line()
myform.field("email", "text", "Email", 2)
myform.validator("email", "Invalid email address")
print myform.dump(2)
Also note that a call always return the newly created object. For example you could do that if you wish:
...
fld = myform.field("email", "text", "Email", 2)
fld.label = "Email Address"
fld.validator("email", "Invalid email address")
...
myform.page().line().field("name", "text").validator("email")
...
print myform.dump(2)
For a depeer explenation on the form content please refere to the Form Definition page on the coolform wiki.
import json
CoolForm(name, action, method, submit, reset)
Constructor
class CoolForm():
def __init__(self,
name,
action,
method="POST",
submit=None,
reset=None):
self.action = action
self.name = name
self.method = method
self.submit = submit
self.reset = reset
self.pages = []
self.dependencies = []
self.hiddens = {}
self.headers = {}
def page(self, title=None, description=None):
p = CoolForm.Page(title, description)
self.pages.append(p)
return p
def line(self, *args, **kwargs):
return self.pages[-1].line(*args, **kwargs)
def field(self, *args, **kwargs):
return self.pages[-1].lines[-1].field(*args, **kwargs)
def validator(self, *args, **kwargs):
return self.pages[-1].lines[-1].fields[-1].validator(*args, **kwargs)
Pages, Lines and Fields can be shown or hidden when a previously declared field match a value.
class Displayable:
def __display__(self, display, field, *values):
if hasattr(self, display) is False:
self.__dict__[display] = {}
if field not in self.__dict__[display]:
self.__dict__[display][field] = []
self.__dict__[display][field] += list(*values)
return self
showOn(field, *values)
Add a display constraint on a field. Elements are shown when some other field(s) match value(s).
def showOn(self, field, *values):
return self.__display__('show_on', field, *values)
hideOn(field, *values)
Add a display constraint on a field. Elements are hiddens when they match some other field(s) value(s).
def hideOn(self, field, *values):
return self.__display__('hide_on', field, *values)
page(title, description)
Add a page to the form.
class Page(Displayable):
def __init__(self, title=None, description=None):
self.title = title
self.description = description
self.lines = []
def line(self, *args, **kwargs):
l = CoolForm.Line(*args, **kwargs)
self.lines.append(l)
return l
class Line(Displayable):
def __init__(self):
self.fields = []
def field(self, *args, **kwargs):
f = CoolForm.Field(*args, **kwargs)
self.fields.append(f)
return f
field(name, type, label, size, help, default)
Add a field to the current line.
class Field(Displayable):
def __init__(self,
name,
type,
label=None,
size=1,
help=None,
mandatory=False,
default=None,
options=None):
self.name = name
self.type = type
self.label = label
self.size = size
self.help = help
self.mandatory = mandatory
self.default = default
self.options = options
self.validators = []
def validator(self, *args, **kwargs):
v = CoolForm.Validator(*args, **kwargs)
self.validators.append(v)
return v
validator(name, message, options)
Add a validator to a field
class Validator:
def __init__(self, name, message=None, options={}):
self.name = name
self.message = message
self.options = options
customValidator(name, module, factory)
Enable the use of a custom validator. Must be called on a CoolForm object.
def customValidator(self, name, module, factory):
self.dependencies.append({
'type':'validator',
'name': name,
'module': module,
'factory': factory})
return self
customDirective(name, tag)
Enalble the use of a custom directive for a field. Must be called on a CoolForm object.
def customDirective(self, name, tag):
self.dependencies.append({
'type':'directive',
'name': name,
'tag': tag})
return self
header(key, value)
Add a header to the ajax request
def header(self, key, value):
self.headers[key] = value
return self
hidden(name, value)
Add an hidden field to be sent
def hidden(self, name, value):
self.hiddens[name] = value
return self
dump(indent=None)
Dumps the current state of the form to JSON. Must be called on a CoolForm object.
def dump(self, indent=None):
return json.dumps({'form': self}, cls=CoolForm.__Encoder__, indent=indent)
class __Encoder__(json.JSONEncoder):
def default(self, obj):
return {k: v for k, v in obj.__dict__.items() if v != None and v != [] and v != {}}