Hi all;

It seems like this worked in the past, but I could just be imagining
things :)

I have a column in my database defined as a DateTimeCol with no default
and where NULL is allowed.  I'm parsing a CSV file and one of the
columns in the CSV file corresponds with this DateTimeCol.

The CSV value is a string of time.struct_time (ie parseable by
strptime()).

When I update a row or create a new entry passing this string as the
value for the column, the validator returns an error because it's not a
datetime object.

I scratch my head here because I'm *sure* I have used my script in the
past with similar date strings and sqlobject has transparently inserted
them into my database without problem...

In any case, in the here and now I'm trying to figure out the best way
to handle this.

I could write a simple function to check whether or not I'm on a column
in my CSV file that has a date/time value, generate a datetime object
from it and pass it along as the value for updating.  The downside here
is that I then need to know ahead of time which columns will have date
values in them and I'd like to not have to manually track and code for
such scenarios if possible.

The other option (and what I ended up doing for now) is to modify the
DateTimeValidator to automatically convert struct_time strings into
datetime objects with the following bit of code:

            if isinstance(value, basestring):
                try:
                    if len(value) == 0:
                        return None
                    else:
                        try:
                            return 
datetime.datetime.fromtimestamp(time.mktime(time.strptime(value)))
                        except OverflowError:
                            return datetime.datetime.fromtimestamp(0)
                except:
                    raise validators.Invalid("expected a date/time string of 
type struct_time in the DateTimeCol '%s', got %s %r instead" % \
                        (self.name, type(value), value), value, state)

This gets the job done for me and even generates a NULL if the string
was zero length, and also handles situations where the Unix timestamp
can't be properly generated due to an overflow error.

Anyone have any thoughts?  Is this something that would be appropriate
for the main codebase or should I be shunting this into my own custom
validator and keeping it external?

Thanks in advance,
Ray

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to