Why my form is not working

45 views
Skip to first unread message

Maurice Waka

unread,
May 12, 2019, 3:17:20 AM5/12/19
to web2py-users
I i'm trying to use one chart to alternate between two forms. A user logs in data in the morning hrs, meaning the form should be active and the evening form inactive. When the time comes ....after 12 pm the morning form is inactive and the evening form is active.
While trying out this morning, both are inactive all through.
Here is the code:

def mg_sugar_chart():
    response
.files.append(URL('e_dextrose','static/js/pygal-tooltips.min.js'))
    response
.headers['Content-Type']='image/svg+xml'


    custom_style
= Style(
        background
='transparent',
        plot_background
='transparent',
        foreground
='#53E89B',
        foreground_strong
='#53A0E8',
        foreground_subtle
='#630C0D',
        opacity
='.6',
        opacity_hover
='.9',
        transition
='400ms ease-in',
        colors
=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53')
       
)
    chart
= pygal.Line(fill=True, interpolate='cubic', style=BlueStyle, x_label_rotation=45, print_values=True, show_y_guides=False, print_values_position='top')
    data
= db(db.amg_sugar).select()
    data1
= db(db.pmg_sugar).select()
   
    chart
.x_labels = al_date
    chart
.x_labels = pl_date
    chart
.add('AM', [i.am_dext for i in data])
    chart
.add('PM', [i.pm_dext for i in data1])

   
return chart.render()


#***sugar***
@auth.requires_login()
def mgSugar():
    db
.amg_sugar.modified_on.readable = db.amg_sugar.modified_on.writable =False
    db
.pmg_sugar.modified_on.readable = db.pmg_sugar.modified_on.writable =False
    chart
= URL('e_dextrose', 'mg_sugar_chart')
    row
= db(db.amg_sugar.author == auth.user_id).select().first()


   
#***open and close for sugar logs***
    early_str
= '05::00::00'
    late_str
= '22::00::00'
    am_str
= '11::59::00'
    pm_str
= '12::00::00'
    am_line
= dt.strptime(am_str, '%H::%M::%S').time()#deadline to post sugar
    pm_line
= dt.strptime(pm_str, '%H::%M::%S').time()#deadline to post sugar
    early_line
= dt.strptime(early_str, '%H::%M::%S').time()#deadline to post sugar
    late_line
= dt.strptime(late_str, '%H::%M::%S').time()#deadline to post sugar


   
#***check if row is empty***
    am_row
= [r.am_dext for r in db(db.amg_sugar.author == auth.user.id).select(db.amg_sugar.ALL)[-1:]]
    pm_row
= [r.pm_dext for r in db(db.pmg_sugar.author == auth.user.id).select(db.pmg_sugar.ALL)[-1:]]
   
#***AM logs***
   
if request.now.date():# == amd.date():
        form
= SQLFORM(db.amg_sugar)
        form
.add_button('Edit', URL('edit_amgSugar'))
        am_row1
= [r.am_dext for r in db(db.amg_sugar.author == auth.user.id).select(db.amg_sugar.ALL)[-1:]]
       
if request.now.time() >= early_line and am_row1[0] == None:        
            form
= SQLFORM(db.amg_sugar, submit_button=T('Submit')).process()
            form
.add_button('Edit', URL('edit_amgSugar'))
   
else:
        session
.flash = ''.join(str(item) for item in ["Your next sugar log is in the next morning. "])
        form
= session.flash


   
if request.now.date():# == amd.date():
        form1
= SQLFORM(db.pmg_sugar)
        form1
.add_button('Edit', URL('edit_pmgSugar'))
        pm_row1
= [r.pm_dext for r in db(db.pmg_sugar.author == auth.user.id).select(db.pmg_sugar.ALL)[-1:]]
       
if request.now.time() >= pm_line and pm_row1[0] ==None:        
            form1
= SQLFORM(db.pmg_sugar, submit_button=T('Submit')).process()
            form1
.add_button('Edit', URL('edit_pmgSugar'))
   
else:
        session
.flash = ''.join(str(item) for item in ["Your next sugar log is in the next afternoon. "])
        form1
= session.flash

   
return dict(chart = chart,form=form, form1=form1)


Could someone give me a better idea/solution.
Much appreciated

João Matos

unread,
May 12, 2019, 6:01:36 PM5/12/19
to web2py-users
Why not create a simple condition using

# The form should have the same name in both conditions.
if request.now.time().hour < 12:
    # Show morning form including the edit and submit buttons.
else:
    # Show afternoon form including the edit and submit buttons.

# If you want to validate anything use the following line.
if form.process(onvalidation=on_validation).accepted:
# Or use the next line if you don't.
# if form.process().accepted:
   # Do whatever you want when success
elif form.errors:
  # Do whatever you want when there are form errors.
Reply all
Reply to author
Forward
0 new messages