Jorge,
I tried the DateTimeConverter() as you suggested and it mostly works. The
problem is pagination's query strings just return __str__() from the
object ignoring format options for something like a datetime. datetime
returns hours and minutes which the validator rejects if your format is
%Y-%m-%d.
What I did is basically copy DateTimeConverter to make my own
DateConverter that doesn't use datetime.datetime objects, but instead
datetime.date objects. The changes between the two are trivial but this
version of DateConverter supports variable formats unlike the formencode
version. Any chance to get something like this put in the turbogears
validators module?
class DateConverter(FancyValidator):
"""
Converts Python date objects into string representation and back.
"""
messages = {
'badFormat': 'Invalid date format',
'empty': 'Empty values not allowed',
}
def __init__(self, format = "%Y/%m/%d", allow_empty = None,
*args, **kwargs):
if allow_empty is not None:
warnings.warn("Use not_empty instead of allow_empty",
DeprecationWarning, 2)
not_empty = not allow_empty
kw["not_empty"] = not_empty
super(FancyValidator, self).__init__(*args, **kwargs)
self.format = format
def _to_python(self, value, state):
""" parse a string and return a date object. """
if value and isinstance(value, date):
return value
else:
try:
tpl = time.strptime(value, self.format)
except ValueError:
raise Invalid(self.message('badFormat', state), value,
state)
# shoudn't use time.mktime() because it can give
OverflowError,
# depending on the date (e.g. pre 1970) and underlying C
library
return date(year=tpl.tm_year, month=tpl.tm_mon,
day=tpl.tm_mday)
def _from_python(self, value, state):
if not value:
return None
elif isinstance(value, date):
return value.strftime(self.format)
else:
return value
Jorge Vargas wrote:
On 1/10/07, Aaron Bostick [1]<[EMAIL PROTECTED]> wrote:
Ken,
That is totally sweet! Is this something TG specific because I do not
remember seeing such a validator in the formencode source/docs?
yes it is here is the link
[2]http://trac.turbogears.org/browser/tags/1.0/turbogears/validators.py#L71
I have created a draft page here
[3]http://docs.turbogears.org/1.0/RoughDocs/Validators hopefully when the
final docs API is in place this could stop being an obscure feature :)
If this validator will use ISO format, then I can leave the paginate.py
code alone.
well it takes a basic python type so the answer is yes.
References
Visible links
1. mailto:[EMAIL PROTECTED]
2. http://trac.turbogears.org/browser/tags/1.0/turbogears/validators.py#L71
3. http://docs.turbogears.org/1.0/RoughDocs/Validators
4. mailto:[email protected]
5. mailto:[EMAIL PROTECTED]
6. http://groups.google.com/group/turbogears?hl=en
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---