|
Eric Clack
eric at bright-interactive.com
Fri Aug 6 23:05:44 EDT 2004
Hi All,
Been playing around with mod_python for a few weeks now and am very
impressed :-)
Recently I've found in my apps that I seem to be doing a lot of type
checking and catching of empty strings -- because all parameters are
passed as string and this causes problems with database criteria for
example.
So I thought I'd hack publisher.py to make things a little easier. I
include my changes below for discussion/criticism. Since I'm new to
Python and mod_python any comments gratefully received :-)
-----------8<-----------------------------------------------------
First, add these lines to any actions scripts and customise as you wish
(just like you would add __auth__ variables)...
# Identify which parameters to actions are longs and should
# be converted as such...
# Use zero if not long...
__long_or_zero__ = [ 'projectid', 'staffid' ]
# Use None if not long...
__long_or_None__ = [ 'jobid' ]
-----------8<-----------------------------------------------------
Secondly, add the following lines to publisher.py...
In the function handler, line 175, insert these lines:
# Perform any type conversions
args = process_types(req, module, args)
Just before the line:
result = apply(object, (), args)
Then add these functions to the end of the file:
def process_types(req, object, args):
"""Perform any type conversions"""
if hasattr(object, "__long_or_zero__"):
for p in object.__long_or_zero__:
if args.has_key(p):
args[p] = longOrZero(args[p])
if hasattr(object, "__long_or_None__"):
for p in object.__long_or_None__:
if args.has_key(p):
args[p] = longOrNone(args[p])
return args
def longOrNone(p):
"""Convert string parameter to long or if blank None"""
try: return long(p)
except ValueError: return None
except TypeError: return None
def longOrZero(p):
"""Convert string parameter to long or if blank zero"""
try: return long(p)
except ValueError: return 0
except TypeError: return 0
Eric Clack
eric at bright-interactive.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2138 bytes
Desc: not available
Url : http://modpython.org/pipermail/mod_python/attachments/20040806/de5eeddf/attachment.bin
|