parse_as_rest date and time range?

103 views
Skip to first unread message

Bernard

unread,
May 6, 2013, 4:42:40 PM5/6/13
to web...@googlegroups.com

Hi Web2py users,
  I am using the parse_as_rest feature for filtering by date range:
The pattern is as follows:
         "/unit/{unit.unit_id}/timeseries[timeseries.unit]/{timeseries.utc_time.ge}/{timeseries.utc_time.le}",
I couldn't find documentation that 'ge' and 'le' are valid for datetime fields, it seems to work well:

However, I need to filter based on date and time.

Ideally, I would like to send a request with the time as part of the date specification:
However, this is an invalid request due to the ':' in the URI.
I looked into using seconds since epoch for the datetime spec, but that's too slow on sqlite.

I'm wondering if anybody knows an easy way to work around this?

The not so pretty solution I'm considering right now is to add a time as part of the URI after the date:
Then, in the controller, I would modify the args before passing to the parse_as_rest() function by concating args 4 and 5 with the date in arg 3, to form the correct format ('YYYY-MM-YY hh:ss') that parse_as_rest() can use.

Any help much appreciated.

Regards,
Bernard

Christian Foster Howes

unread,
May 6, 2013, 6:28:03 PM5/6/13
to web...@googlegroups.com
can you url escape the : and have it work?

if not, maybe use a different separator and change the string to timestamp parsing to expect your format.

cfh

Bernard

unread,
May 6, 2013, 6:35:07 PM5/6/13
to web...@googlegroups.com
Thanks Christian,

I tried to escape it to %3A but that gave me an error invalid request. I couldn't figure out where it's being blocked on the server (routes.py?).

Where can I get more info to change separator and parsing? Is that all in dal.py?

Christian Foster Howes

unread,
May 6, 2013, 6:39:13 PM5/6/13
to web...@googlegroups.com
there are a couple of characters that web2py is not nice about in the
URL parsing.....

for the format string, on the datetime fields use the
IS_DATETIME(format='...') validator with a custom format string. see
http://web2py.com/books/default/chapter/29/07#Validators

On 5/6/13 15:35 , Bernard wrote:
> Thanks Christian,
>
> I tried to escape it to %3A but that gave me an error invalid request. I
> couldn't figure out where it's being blocked on the server (routes.py?).
>
> Where can I get more info to change separator and parsing? Is that all in
> dal.py?
>
> On Monday, May 6, 2013 3:28:03 PM UTC-7, Christian Foster Howes wrote:
>>
>> can you url escape the : and have it work?
>>
>> if not, maybe use a different separator and change the string to timestamp
>> parsing to expect your format.
>>
>> cfh
>>
>> On Monday, May 6, 2013 1:42:40 PM UTC-7, Bernard wrote:
>>>
>>>
>>> Hi Web2py users,
>>> I am using the parse_as_rest feature for filtering by date range:
>>> The pattern is as follows:
>>> "/unit/{unit.unit_id}/timeseries[timeseries.unit]/{
>>> timeseries.utc_time.ge}/{timeseries.utc_time.le}",
>>> I couldn't find documentation that 'ge' and 'le' are valid for datetime
>>> fields, it seems to work well:
>>>
>>> http://localhost/manager/default/api/unit/123/timeseries/2013-05-01/2013-05-02
>>>
>>> However, I need to filter based on date and time.
>>>
>>> Ideally, I would like to send a request with the time as part of the date
>>> specification:
>>> http://localhost/manager/default/api/unit/123/timeseries/2013-05-0102:00/2013-05-02 00:00

Bernard

unread,
May 6, 2013, 7:51:09 PM5/6/13
to web...@googlegroups.com
I don't think the form validators are used when using parse_as_rest().

When the URI is parsed, the date is simply extracted (as a string?) into a DAL query.

Ex from my previous query:
>>> http://localhost/manager/default/api/unit/123/timeseries/2013-05-01/2013-05-02 
query=(unit.unit_id = '123')
query=(timeseries.utc_time >= '2013-05-01')
query=(timeseries.utc_time <= '2013-05-02')

Printed from dal.py->parse_as_rest()
The three above queries are and-ed together in the line : "dbset=dbset(query)"

Christian Foster Howes

unread,
May 6, 2013, 7:56:23 PM5/6/13
to web...@googlegroups.com
oh, we are over my parse_as_rest knowledge... i've only accessed the
args directly and did my own logic with it. :(
>> http://localhost/manager/default/api/unit/123/timeseries/2013-05-0102:00/2013-05-0200:00
Reply all
Reply to author
Forward
0 new messages