How to format wsme types

47 views
Skip to first unread message

Guangyu Suo

unread,
Apr 17, 2014, 2:47:50 AM4/17/14
to pytho...@googlegroups.com
Hi,

For example, I have a custom type, and it has a datetime field:

class Person(wsme.types.Base):
    birthday = datetime.datetime

This will return the result like:
{
    "birthday": "2014-04-17T07:22:09"
}

I want the result more iso8601, so I want to add 'Z' at the end of the datetime, like:

{
    "birthday": "2014-04-17T07:22:09Z"
}

But, I don't know how to format this field in wsme, hope anyone can help me out, thanks..

Christophe de Vienne

unread,
Apr 17, 2014, 9:46:10 AM4/17/14
to pytho...@googlegroups.com
Hello,

The datetimes are formatted by calling their "isoformat()" method.
If you have a timezone set on the datetime object, it will be added to the string, though not in the format you want.

For example, datetime.datetime(2004, 1, 2, 12, 0, 0, tzinfo=pytz.utc) will be formated as '2004-01-02T12:00:00+00:00'

The simpler way would be to use this format as there is no builtin way to change the date formats in wsme.

That said, the actual formatting happens in wsme/rest/json.py, around line 112.
The problem is that there is no way, with simplegeneric, to override the function.
So you would have to patch wsme, OR provide your own 'tojson' function for all the types and replace wsme.rest.json.tojson with it.
Both ways are a bit overkill.

Last option, propose a patch to wsme that make the date format configurable, but more importantly add proper timezone handling to wsme.
If it does not break the current behavior, it has all its chances to be merged.

Regards,

Christophe
--
You received this message because you are subscribed to the Google Groups "python-wsme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-wsme...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages