I have a code that works OK except when there is no data in the database.
Here is the code, using MongoDB.
model code:

db.define_table('sleep',
                Field('awake','double',default=0),
                Field('light_sleep','double',default=0),
                Field('deep_sleep','double',default=0),
                Field('author', 'reference auth_user', 
default=auth.user_id, readable=False, writable=False),
                common_filter = lambda query: db.sleep.author == 
auth.user_id)

def sleep_chart():
    
response.files.append(URL('e_default','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')
        )
    data = db(db.sleep).select()
    gauge = pygal.SolidGauge(inner_radius=0.70)
    percent_formatter = lambda x: '{:.10g}Hrs'.format(x)
    gauge.value_formatter = percent_formatter

    gauge.add('Awake', [{'value':[i.awake for i in data][-1], 'max_value': 
24}])
    gauge.add('Light Sleep', [{'value':[i.light_sleep for i in data][-1], 
'max_value': 24}])
    gauge.add('Deep Sleep', [{'value':[i.deep_sleep for i in data][-1], 
'max_value': 24}])
    return gauge.render()

def mySleep():
    chart= URL('default', 'sleep_chart')
    form = SQLFORM(db.sleep, submit_button=T('Submit')).process()
    return dict(chart = chart,form=form)

Here is the problem:

When a new user registers into the system, by default, there is no data 
recorded in the database, so I get the error(see attached) below.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
 Traceback (most recent call last):
 File "/home/mau/web2py/gluon/restricted.py", line 219, in restricted
 exec(ccode, environment)
 File "/home/mau/web2py/applications/Hesty/controllers/e_default.py" 
<http://127.0.0.1:8000/admin/default/edit/Hestque_Wellness/controllers/e_default.py>
, line 708, in <module>
 File "/home/mau/web2py/gluon/globals.py", line 421, in <lambda>
 self._caller = lambda f: f()
 File "/home/mau/web2py/gluon/tools.py", line 3868, in f
 return action(*a, **b)
 File "/home/mau/web2py/applications/Hesty/controllers/e_default.py" 
<http://127.0.0.1:8000/admin/default/edit/Hestque_Wellness/controllers/e_default.py>
, line 649, in sleep_chart
 gauge.add('Awake', [{'value':[i.awake for i in data][-1], 'max_value': 24
}])
IndexError: list index out of range


Expectation:I want to have a default figure in the database. I have tried 
having "default=0" but the int only appears in the form text area. So the 
error persists.
Is there a way to have a default figure in the database or a way to catch 
this errors and let the new user to input int figure?

-- 
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