Update form fields on submit

54 views
Skip to first unread message

backseat

unread,
Oct 15, 2017, 11:53:04 AM10/15/17
to web2py-users
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)



Bernhard Radermacher

unread,
Oct 15, 2017, 12:12:03 PM10/15/17
to web2py-users
Might not be the most efficient, but if you put the from date and to date as arguments and then redirect in the form.accepts:

if form.accepts(request,session):
   
#calculate the new dates...
    session
.flash='Updated date range'
    redirect
(URL('report',args=[from_date,to_date]))


backseat

unread,
Oct 16, 2017, 2:49:10 AM10/16/17
to web2py-users
Thanks, Bernhard.

The solution I ended up with is quite messy, but it does work:

    # Set defaults
   
if request.get_vars:
        filter_bar
.vars.from_date = datetime.datetime.strptime(
            request
.get_vars.from_date, "%Y-%m-%d").date()
        filter_bar
.vars.to_date = datetime.datetime.strptime(
            request
.get_vars.to_date, "%Y-%m-%d").date()
   
else:
        filter_bar.vars.from_date = last_day_last_month.replace(day=1)
        filter_bar.vars.to_date = last_day_last_month


    # Submit form
    if filter_bar.validate(keepvalues=True):
        if filter_bar.vars.daterange:
            daterange = int(filter_bar.vars.daterange)
            if daterange > 0:
                from_date, to_date = get_daterange_dates(daterange)
                redirect(URL(vars=dict(from_date=from_date, to_date=to_date)))



Reply all
Reply to author
Forward
0 new messages