On Fri, Jul 30, 2010 at 07:13:47AM +0200, Tom Coetser wrote:
> I would like to use a validator with a PickleCol like this:
> 
> ---------------- cut ---------------------------------------------------------
> from formencode import validators
> 
> class PTValidator(validators.FancyValidator):
> 
>     def validate_python(self, value, state):
>         # Do validation on value here before storing it in the db
>         print "value:", value
> 
> 
> class PT(SQLObject):
>     pickled = PickleCol(validator=PTValidator)
> 
> ---------------- cut ---------------------------------------------------------
> 
> The problem is that (it seems) value is already pickled by the time it is 
> passed to the PTValidator.validate_pyton() method.
> 
> Can anyone give me some example/advise on how to use validators on a pickle 
> column and being able to **validate** the value being stored only, but still 
> leaving SQLObject to deal with the pickling/unpickling?

   This is the way:

from formencode import compound

class PT(SQLObject):
    pickled = PickleCol()

socol = PT.sqlmeta.columns['pickled']
validator = socol.validator
socol.validator = compound.All.join(PTValidator(), validator)

   PTValidator.from_python will be called first, before String/BLOB/Pickle
conversion; and PTValidator.to_python will be called last, after
unPickle/BLOB/String conversion.

   And you raised an interesting question. Some user-supplied validators
need to be run in "normal" (current) order - from_python called last, after
all conversions, and to_python first. For example, I use a special
validator in a CurrencyCol, and I certainly want its from_python get data
after all decimal conversions.
   But some columns require a different order. Perhaps I need to add an
additional validator ("before_validator"? "validator2"?) so user-supplied
validator will be inserted into the other end of the list of validators.

Oleg.
-- 
     Oleg Broytman            http://phd.pp.ru/            p...@phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://p.sf.net/sfu/dev2dev-palm
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to