Yann’s Blog

January 22, 2009

yyafl 0.2.1 is now available

Filed under: Python,Software,yyafl — Yann @ 9:59 pm

yyafl, a Python web form library operating on domain objects, now has version 0.2.1 available for download. This release includes numerous fixes for decorators, including the ability to attach multiple decorators to a form field or the wildcard ‘*’ field.

An example is given below, in the context of a CherryPy handler. This example is available in the download as

examples/layout02.py
import cherrypy
import yyafl
import yyafl.layout
from yyafl import fields
from yyafl.widgets import HiddenInput
 
# This example covers the user of custom decorators for required form fields
 
class Form1(yyafl.Form):
    name = fields.CharField(label = "User name", required = True)
    email = fields.CharField(label = "Your e-mail address", required = True)
    optional = fields.CharField(label = "Optional field", required = False)
    hidden = fields.CharField(widget = HiddenInput, default = "123")
    _layout = yyafl.layout.TableLayout()
 
class RequiredDecorator(yyafl.layout.NullDecorator):
    """ Draw a * next to all required fields """
    def extra_markup_widget(self, field, rendered_text):
        if field.required:
            return rendered_text + " * "
        return rendered_text
 
class FormTest(object):
 
    @cherrypy.expose
    def index(self, **kwargs):
        f = Form1(data = kwargs)
        # Add a global decorator
        f.layout().add_decorator('*', RequiredDecorator())
        content = ""
        content += "<html><body>"
 
        if f.is_valid() == False:
            content += "Errors: "
            for error in f.errors:
                content += error + " : " + f.errors[error] + "<br />"
        elif f.is_bound() and f.is_valid():
            content += "Thanks for the data!"
 
 
        content += "<p><form method=\"GET\" action=\"/\">"
 
        # Render using the layout specified in _layout above.
        content += f.render()
        # Or invoke the layout explicitly
        # l = yyafl.layout.TableLayout()
        # l.render(f)
 
        content += "<br /> <input type='submit' value='Submit' /></form>"
        return content
 
 
if __name__ == '__main__':
    cherrypy.quickstart(root = FormTest())

September 29, 2008

yyafl now has layout support, with decorators

Filed under: Python,Software,yyafl — Yann @ 11:35 pm

Just finished adding in a flexible layout system to yyafl, my reimplementation of Django newforms for other Python web frameworks or WSGI adapters. Now its simple to add default layouts or render a different layout on demand.

For example, to add a simple TableLayout() to a form:

class Form1(yyafl.Form):
    name = fields.CharField(label = "User name", required = True)
    email = fields.CharField(label = "Your e-mail address", required = True)
    hidden = fields.CharField(widget = HiddenInput, default = "123")
    _layout = yyafl.layout.TableLayout()

and to render the form to HTML:

# Render using the layout specified in _layout above.
content += f.render()
# Or invoke the layout explicitly
l = yyafl.layout.TableLayout()
content += l.render(f)

yyafl Layout()s also understand decorators:

decorators = { '*' : MarkRequiredDecorator(),
    'name_field' : HighlightDecorator() }
layout = yyafl.layout.TableLayout(decorators = decorators)
layout.render(f)

Decorators can change attributes and wrap fields in markup blocks. In the above example, the ‘*’ signifies the decorator should apply to all fields, and the HighlightDecorator should only apply to the field called ‘name_field’.

There are still some not yet implemented ideas:

  • It doesn’t generated the <form> blocks yet, you need to roll your own.
  • Field reordering and grouping isn’t supported by the default layout engines yet, though you could write your own.
  • No AJAX form support (though its coming!)
  • Automatic CSRF prevention forms.
  • Error message printing in the form.

You can find these latest changes in the Git repository linked from the main yyafl web page.

Powered by WordPress