py4web sample errors

174 views
Skip to first unread message

JSalvat

unread,
Dec 24, 2019, 8:21:28 AM12/24/19
to web2py-users
Very simple Form and Grid sample to show some errors:

In Grid: decimal field  does not show /  allow input.

In Form: date and time widgets not working

Mainly copied from examples, Showing what was modified:

models.py

import os
from py4web import DAL, Field
from pydal.validators import *
# define database and tables
db = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases'))

# simple table example
db.define_table(
    'sam_table',
    Field('samString', 'string', length=10,  unique=True, required=True,
          requires = [IS_LENGTH(10, 4), IS_SLUG(maxlen=10,
          check=True, error_message='Only alphanumeric characters and non-repeated dashes.')],
          comment='Unique identifier.'),
    Field('samText', 'text', comment='Enter a description text.'),
    Field('samBool', 'boolean', comment='Are you interested in py4web ?'),
    Field('samDate', 'date', required=True,
          requires = [IS_NOT_EMPTY(), IS_DATE()],
          comment='Enter a valid sample date.'),
    Field('samTime', 'time',
          requires = IS_TIME(), comment='Enter a valid sample time.'),
    Field('samInteger', 'integer', default=0,
          requires = IS_INT_IN_RANGE(0, 9999, error_message='Must be integer between 0 and 9999.'),
          comment='Enter a valid sample integer.'),
    Field('samDecimal', 'decimal(4,2)', default=0.0,
          requires = IS_DECIMAL_IN_RANGE(0, 35.0, dot='.'),
          comment='Enter a decimal between 0 and 35.'),
    )

if not db(db.sam_table).count():
    db.sam_table.insert(
        samString='2011',
        samText='This record was inserted when first time create-table.',
        samBool=True,
        samDate='2011-12-24',
        samTime='11:45:00',
        samInteger=1234,
        samDecimal=21.50)
    db.commit()

__init__.py

import os
from py4web import *
from py4web.utils.form import Form, FormStyleBulma
from py4web.utils.publisher import Publisher, ALLOW_ALL_POLICY
from pydal.validators import *
from . models import db

session = Session(secret='mysecret')

T
= Translator(os.path.join(os.path.dirname(__file__), 'translations'))

publisher = Publisher(db, policy=ALLOW_ALL_POLICY)

@action('index')
@action.uses('index.html')
def index():
   
return {}

# exposed as /sample/create_form or /sample/update_form/<id>
@action('create_form', method=['GET','POST'])
@action('update_form/<id>', method=['GET','POST'])
@action.uses('form.html', db, session)
def sample_form(id=None):
    form
= Form(db.sam_table, id, deletable=False, formstyle=FormStyleBulma)
    rows
= db(db.sam_table).select()
   
return dict(form=form, rows=rows)

# exposed as /sample/grid
@action('grid')
@action.uses('grid.html')
def sample_grid():
   
return dict(grid=publisher.grid(db.sam_table))

form.html

[[extend 'layout.html']]

<h2 class="title">Sample Form</h2>

[[=form]]

<h2 class="title">Rows</
h2>

<ul>
 
[[for row in rows:]]
 
<li>[[=row.id]]: [[=row.samString]] ( [[=row.samBool]] )</li>
 
[[pass]]
</ul>


Have not tested translations yet.

Massimo Di Pierro

unread,
Dec 25, 2019, 1:49:22 PM12/25/19
to web2py-users
Can you please check the latest version? This should be fixed.

translations in forms are not yet supported in an automatic manner. working on it.

JSalvat

unread,
Dec 28, 2019, 4:14:10 AM12/28/19
to web2py-users
Things that may need to be fixed:

* _dashboard, when reloading Apps, examples error: Table already defined: person

* Form usage: Date, Time, Integer and Decimal widgets don't show. Date does not use browser's local date format.

* Grid create: Boolean widget does not show selected state.

* Local access  127.0.0.1:8000/whateverapp  from 2 diferent browsers hungs

Massimo Di Pierro

unread,
Dec 28, 2019, 10:18:05 AM12/28/19
to web2py-users
Thanks for reporting this:

Fixed these:
* _dashboard, when reloading Apps, examples error: Table already defined: person

* Local access  127.0.0.1:8000/whateverapp  from 2 diferent browsers hungs

Problem was that models.py MUST db.commit()

Not sure about these.

* Form usage: Date, Time, Integer and Decimal widgets don't show. Date does not use browser's local date format.

* Grid create: Boolean widget does not show selected state.

I cannot reproduce. Maybe I misunderstand. Can you post code and screenshots?

Massimo

Massimo Di Pierro

unread,
Dec 28, 2019, 10:23:57 AM12/28/19
to web2py-users
was able to reproduce the date format issue. Not a bug, it simply is not currently handled. Let me think about it some more.

JSalvat

unread,
Dec 30, 2019, 1:30:29 PM12/30/19
to web...@googlegroups.com
Some testing done, I don't know the side efects, but with this changes to  py4web/utils/form.py  things work munch better (as per date/time/password widgets):

....
       
elif field.type == "text":
           
# control = TEXTAREA(value or "", _id=input_id, _name=field.name)    <--- MODIFIED
           
control = TEXTAREA(value or "", _id=input_id, _name=field.name, _class="textarea")
....
....
       
else:
           
# field_type = "password" if field.type == "password" else "text"    <--- MODIFIED
            field_type
= "text" if (field.type == "string" or field.type == "") else field.type
            control
= INPUT(
                _type
=field_type,
                _id
=input_id,
                _name
=field.name,
                _value
=value,
               
# _class=field_class,    <--- MODIFIED
                _class
="input",
           
)
       
       
# key = control.name.rstrip("/")    <--- BLOCK 4 LINES UNUSED
       
# if key == "input":
       
#     key += "[type=%s]" % (control["_type"] or "text")
       
# control["_class"] = classes.get(key, "")


Also, in Grid create: Boolean widget does not show selected state.
        in Grid modify: If a field is modified and click on "Close" instead of "Save", changes are saved !

I'm using this table on Form / Grid tests:

db.define_table(
   
'sam_table',
   
Field('samString', type='string', length=10,  unique=True, required=True,

          requires
= [IS_LENGTH(10, 4), IS_SLUG(maxlen=10,
          check
=True, error_message='Only alphanumeric characters and non-repeated dashes.')],
          comment
='Unique identifier.'),

   
Field('samText', type='text', comment='Enter a description text.'),
   
Field('samBool', type='boolean', default=False, comment='Are you interested in py4web ?'),
   
Field('samDate', type='date', required=True,

          requires
= [IS_NOT_EMPTY(), IS_DATE()],
          comment
='Enter a valid sample date.'),

   
Field('samTime', type='time',

          requires
= IS_TIME(), comment='Enter a valid sample time.'),

   
Field('samInteger', type='integer', default=0,

          requires
= IS_INT_IN_RANGE(0, 9999, error_message='Must be integer between 0 and 9999.'),
          comment
='Enter a valid sample integer.'),

   
Field('samDecimal', type='decimal(4,2)', default=0.0,
          requires
= IS_DECIMAL_IN_RANGE(0, 35.0, dot=','),
          comment
='Enter a sample decimal between 0 and 35.'),
   
)


Massimo Di Pierro

unread,
Jan 1, 2020, 9:26:00 AM1/1/20
to web2py-users
About the class style, you should specify it:

>>> from py4web.utils.form import Form
>>> from pydal import Field

DEFAULT STYLE

>>> f = Form([Field('a','text'), Field('b','string')])
>>> print(f.xml())
<form method="POST" action="/" enctype="multipart/form-data"><div class=""><label for="none_a" class="">A</label><div class=""><textarea id="none_a" name="a" class=""></textarea></div><p class=""></p></div><div class=""><label for="none_b" class="">B</label><div class=""><input type="text" id="none_b" name="b" class=""/></div><p class=""></p></div><div class=""><div class=""><input type="submit" value="Submit"/></div></div><input type="hidden" name="_formkey" value="none"/></form>

BULMA STYLE

>>> from py4web.utils.form import FormStyleBulma
>>> f = Form([Field('a','text'), Field('b','string')], formstyle=FormStyleBulma)
>>> print(f.xml())
<form method="POST" action="/" enctype="multipart/form-data"><div class="field"><label for="none_a" class="label">A</label><div class="control"><textarea id="none_a" name="a" class="textarea"></textarea></div><p class="help"></p></div><div class="field"><label for="none_b" class="label">B</label><div class="control"><input type="text" id="none_b" name="b" class="input"/></div><p class="help"></p></div><div class="field"><div class="control"><input type="submit" value="Submit" class="button"/></div></div><input type="hidden" name="_formkey" value="none"/></form>

JSalvat

unread,
Jan 1, 2020, 1:33:35 PM1/1/20
to web2py-users
Hi Massimo, thanks for your help, but the date and time widgets don't apear since the input type is text:

>>> from py4web.utils.form import Form, FormStyleBulma
>>> from pydal import Field

>>> f=Form([Field('sdate','date')], formstyle=FormStyleBulma)
>>> print(f.xml())
<form method="POST" action="/" enctype="multipart/form-data"><div class="field"><label for="none_sdate" class="label">Sdate</label><div class="control"><input type="text" id="none_sdate" name="sdate" class="input"/></div><p class="help"></p></div><div class="field"><div class="control"><input type="submit" value="Submit" class="button"/></div></div><input type="hidden" name="_formkey" value="none"/></form>

Should be:   ... <input type="date" ...

>>> f=Form([Field('stime','time')], formstyle=FormStyleBulma)
>>> print(f.xml())
<form method="POST" action="/" enctype="multipart/form-data"><div class="field"><label for="none_stime" class="label">Stime</label><div class="control"><input type="text" id="none_stime" name="stime" class="input"/></div><p class="help"></p></div><div class="field"><div class="control"><input type="submit" value="Submit" class="button"/></div></div><input type="hidden" name="_formkey" value="none"/></form>

Should be:   ... <input type="time" ...

May need to fix   py4web/utils/form.py  ?
Reply all
Reply to author
Forward
0 new messages