import osfrom py4web import DAL, Fieldfrom pydal.validators import *# define database and tablesdb = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases'))
# simple table exampledb.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()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))[[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>....
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, "")
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.'),
)