I have a page that is a report, and at the top of that page is a form with 
a 'from' and 'to' date field so that the user can select the date range to 
report on. By default, the dates select and the first to the last of last 
month. That works fine.

I want to add a combo that will allow the selection of a predefined period, 
such as "last week". That works fine, too, but when selecting 'last week', 
I want to update the 'to' and 'from' date fields accordingly, and I can't 
make that work.

Here's an example with hardcoded dates for for simplicity:

def summary_report():

    # Calculate default dates
    last_day_last_month = (datetime.datetime.now().replace(day=1) -
                           datetime.timedelta(days=1)).date()
    from_date = last_day_last_month.replace(day=1)
    to_date = last_day_last_month
    date_range_list = [
        (0, "Select Date Range"),
        (1, "Last Week"),
    ]

    # Non-DB field definitions
    from_date_field = Field('from_date', type='date', label=T('From date'))
    to_date_field =  Field('to_date', type='date', label=T('To date'))
    date_range_field = Field('daterange', type='string', label=T('dr'))
    date_range_field.requires = IS_IN_SET(date_range_list, zero=None)

    # Build filter bar
    filter_bar = SQLFORM.factory(
        from_date_field, to_date_field, date_range_field,
        formstyle='divs',
        submit_button='Filter',
    )

    # Set defaults
    filter_bar.vars.from_date = from_date
    filter_bar.vars.to_date = to_date

    # Submit form
    filter_bar.validate(keepvalues=True,onvalidation=do_daterange)
    from_date = filter_bar.vars.from_date
    to_date = filter_bar.vars.to_date

    # Build query
    [...]


def do_daterange(filter_bar):

    daterange = int(filter_bar.vars.daterange)
      if daterange > 0:
        filter_bar.vars.from_date = datetime.date(2017,1,1)
        filter_bar.vars.to_date = datetime.date(2017,6,30)



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to