Web2py 2.0 Cheat Sheet

ADVERTISEMENT

WEB2PY 2.0 Cheat Sheet
Database Abstraction Layer
Forms
db = DAL('sqlite://storage.sqlite',pool_size=1)
form = SQLFORM(db.thing,record=None)
form = SQLFORM.factory(Field('name')) (no db)
db.define_table('thing', Field('name','string'))
URL Parsing
id = db.thing.insert(name='max')
form = SQLFORM.dictform(d) (for d={...})
query = db.thing.name.contains('m')&(db.thing.id==1)
(admin interface)
form = SQLFORM(db.thing).process()
db(query).update(name='max')
(app static file)
if form.accepted: ...
db(query).delete()
(database interface)
elif form.errors: ...
things = db(query).select(db.thing.ALL,
(.e)/!args?vars
orderby=~db.thing.name, groupby=db.thing.id
host
request.http_host
Grids
dictinct=True, cache=(cache.ram,60))
port
request.http_port
thing = db.thing(id) or redirect(URL('error'))
app
grid = SQLFORM.grid(query)
request.application
thing.update_record(name='max')
c
grid = SQLFORM.smartgrid(table, linked_tables=[])
request.controller
things.export_to_csv_file(open(filename,'wb'))
f
request.function
db.thing.import_from_csv_file(open(filename,'rb'))
SQLFORM.grid(
e
request.extension
query, fields=None, field_id=None, left=None,
args
request.args (list)
Field Types
headers={}, orderby=None, searchable=True,
vars
request.vars (dict)
string, text, boolean, integer, double, decimal(n,m), date,
sortable=True, paginate=20, deletable=True,
’c/f.e’
response.view
time, datetime, password, upload, blob, list:string,
editable=True, details=True, selectable=None,
Global Objects
list:integer, reference table, list:reference table
create=True, csv=True, links=None, ...)
Field Attributes
request.
Auth
Field(fieldname, type='string', length=None,
application, controller, function, now, client, is_local,
@auth.requires_login()
default=None, required=False, requires=None,
is_https, ajax, args, vars, get_vars, post_vars,
@auth.requires_membership('groupname')
ondelete='CASCADE', notnull=False, unique=False,
env.request_method, env.path_info, env.query_string,
@auth.requires_premission('edit','tablename',id)
uploadfield=True, widget=None, label=None,
env.http_*, env.wsgi_*
@auth.requires(condition)
comment=None, writable=True, readable=True,
auth.(has|add|del)_membership(...)
update=None, authorize=None, autodelete=False,
response.
auth.(has|add|del)_permission(...)
represent=None, uploadfolder=None,
status=200, view='filename.html', flash='flash me',
uploadseparate=False, compute=None, ...)
Full Example
js = 'alert("run me")', download(request,db),
Validators
models/db.py
stream(file), render(template,**vars)
CLEANUP, CRYPT, IS_ALPHANUMERIC, IS_DATE, IS_DATETIME,
from gluon.tools import *
session.
IS_DATETIME_IN_RANGE, IS_DATE_IN_RANGE,
db = DAL('sqlite://storage.sqlite')
IS_DECIMAL_IN_RANGE, IS_EMAIL, IS_EMPTY_OR, IS_EQUAL_TO,
auth = Auth(db)
connect(request,response,db,separate=False),
IS_EXPR, IS_FLOAT_IN_RANGE, IS_GENERIC_URL, IS_HTTP_URL,
auth.define_tables()
flash, secure(), forget(), _unlock(response)
IS_IMAGE, IS_INT_IN_RANGE, IS_IN_DB, IS_IN_SET,
db.define_table('thing',
cache
IS_IN_SUBSET, IS_IPV4, IS_LENGTH, IS_LIST_OF, IS_LOWER,
Field('name',requires=IS_NOT_EMPTY()), auth.signature)
IS_MATCH, IS_NOT_EMPTY, IS_NOT_IN_DB, IS_NULL_OR, IS_SLUG,
auth.enable_record_versioning(db) # for full db auditing
@cache('key',3600,cache.ram)
IS_STRONG, IS_TIME, IS_UPLOAD_FILENAME, IS_UPPER, IS_URL
@cache('key',3600,cache.disk)
controllers/default.py
cache.ram.clear(regex='k.*')
Helpers
def index(): return auth.wiki() # embed a wiki
A, B, BEAUTIFY, BODY, BR, CAT, CENTER, CODE, COL, COLGROUP,
T (internationalization)
def download(): return response.download(request,db)
DIV, EM, EMBED, FIELDSET, FORM, H1, H2, H3, H4, H5, H6, HEAD, HR,
def user(): return dict(form=auth) # login/etc.
T('hello %(key)s',dict(key='thing'))
HTML, I, IFRAME, IMG, INPUT, LABEL, LEGEND, LI, LINK, MARKMIN,
T.current_languages = ['en'] (no translate)
MENU, META, OBJECT, ON, OL, OPTGROUP, OPTION, P, PRE, SCRIPT,
@auth requires_login()
T.force('en') (use languages/en.py)
SELECT, SPAN, STYLE, TABLE, TAG, TBODY, TD, TEXTAREA, TFOOT,
def manage_things(): # access you data
TH, THEAD, TITLE, TR, TT, UL, XHTML, XML
grid = SQLFORM.grid(db.thing.created_by==auth.user.id)
URL, redirect, and HTTP
return locals()
DIV(SPAN('hello'),_id='myid',_class='myclass')
URL('function')
A('link',_href=URL(...))
URL('controller','function')
views/default/manage things.html
SPAN(A('link',callback=URL(...),delete='span'))
URL('app','controller','function')
{{extend 'layout.html'}}
TABLE(*[TR(TD(item)) for item in [...]])
URL('function',args=[...],vars={...})
div = DIV(SPAN('hello',_id='x'))
<h1>Your things</h1>
URL('function',scheme=True) (full url)
div.element('span#x').append("world")
{{=grid}}
URL('function',user_signature=True)
div.element('span#x')['_class'] = 'myclass'
{{# any python between double braces}}
(then use @auth.requires_signature())
DIV('1<2').xml()==DIV(XML('1&lt;2',sanitize=True)).xml()
redirect(URL('index'))
div = TAG.DIV(TAG.SPAN('hello',_id='x'))
raise HTTP(500,'message')
Copyleft 2012 Massimo Di Pierro
div = TAG('<div><span id="hello">hello</span></div>')

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2