Python embedded like PHP

Dave Cole djc at object-craft.com.au
Mon Apr 1 23:34:04 EST 2002


>>>>> "Paul" == Paul Boddie <paul at boddie.net> writes:

Paul> The next problem, though, is coordinating the output with the
Paul> input.  One can invent a "custom tag" (or corresponding feature
Paul> in one's framework of choice) which emits an HTML "input" field,
Paul> but where is the code that checks the value returned by the user
Paul> through that field, and how can we be sure that such code is
Paul> validating and manipulating the same kind of data as that
Paul> described by the "input" field? For example, I could invent a
Paul> "custom tag" like this:

Paul>   <myapp:specialInput name="someField"/>

Paul> This could result in the following HTML being generated:

Paul>   <input name="someField" size="30" type="text">

Paul> The intention might be that the contents of "someField" are in a
Paul> particular format. Moreover, such data may be stored in a
Paul> database system, and we might want to keep the "size" or
Paul> "length" of a piece of data synchronized with the data type used
Paul> in the database - it's not uncommon for such things to evolve as
Paul> the requirements of an application change, but it would be very
Paul> inconvenient to need to change HTML form descriptions,
Paul> validation code, template system code, all in a number of
Paul> different places.

Paul> Template systems typically don't address these issues, whereas
Paul> stand-alone validation systems address the nature of the input
Paul> data but not how such data is "described" to the browser (and
Paul> thus to the user). A coordinated approach which unifies input,
Paul> output, and validation seems to be the logical next step in any
Paul> significantly big or complex application.

Albatross has features which come close to what you are requesting.
With a little bit of sweat you could make it do exactly what you are
talking about.

The built in behaviour for forms creates a Python list of all input
fields in a form and then pickles (and MD5 signs) that to a hidden
field in the form.  When the browser request comes back the list is
restored and used for the basis of merging the browser request into
the "execution context".  What this currently gives you is that any
field which is not filled in and submitted by the browser will be
detected and the corresponding variable will be set to None.

The standard behaviour is performed by a combination of the <al-input>
tag and the NameRecorderMixin class.

There are two ways that you could change the current Albatross
behaviour.  You could implement your own special input tag to
construct your special input tags with a special form recorder, or you
could subclass one of the Application classes and override the
validate_request() method.

1) A custom tag is pretty easy to build - check this out:

   http://www.object-craft.com.au/projects/albatross/albatross/cust-ref.html

   By implementing your own RecorderMixin you could store the field
   type with the name for automatic validation on request merging.

   This is the documentation for the default recorder behaviour as
   implemented by NameRecorderMixin:

   http://www.object-craft.com.au/projects/albatross/albatross/mixin-rec-name.html

   To complete the loop you would then subclass one of the application
   classes which inherit from Application and override the
   merge_request() method which takes fields out of the browser
   request in place them into the execution context.

   http://www.object-craft.com.au/projects/albatross/albatross/pack-app.html

2) This is the easy but less general option.  You could subclass an
   application object and provide a validate_request() method which
   field names with embedded type information (hungarian notation) to
   determine the type of the field.  By returning FALSE from
   validate_request() you suppress request processing.

   As part of the field validation in your validate_request() you
   could set error messages which were picked up as part of the form
   display.

At some stage we are going to start building up a cookbook for
Albatross.  This is a good cookbook example.

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list