Web2py YAML

created on 2011-08-06 11:21:22

project

bringing YAML support for web2py models or even to full web2py applications


Description

Using YAML to define models may turn your code more elegant and concise, always better than using a chain of define_table() methods as currently we do to create tables in web2py models. This small work tries to make it possible and could be adapted to allow you to model full web2py applications.

Example

contents of input_file.yml:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
db:
name: test
dbms: postgres
dbfile: storage.sqlite # if it was sqlite...
user: john
pass: Doe
hostname: localhost
parameters: pool_size=10
options: all-nodesc # this is for everything without comments but could be gae, session, description, auto, crud, auth-crud, services, mail, all or combinations of those..
# you also could replace these parameters by dbline: postgres://john:Doe@localhost/test

animal:
name: string
type: string
sex: string
extra: reference name

person:
name: string
age: int, requires=IS_INT(), default='12'
color:
type: int
requires: IS_INT()
default: 103

car:
name: string
age: int, requires=IS_INT(), default='12'

this would generate this output as web2py model:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
# -*- coding: utf-8 -*- 

if request.env.web2py_runtime_gae: # if running on Google App Engine
test = DAL('gae') # connect to Google BigTable
session.connect(request, response, db=test) # and store sessions and tickets there
### or use the following lines to store sessions in Memcache
# from gluon.contrib.memdb import MEMDB
# from google.appengine.api.memcache import Client
# session.connect(request, response, db=MEMDB(Client())
else:
test = DAL('postgres://john:Doe@localhost/test', pool_size=10) # if not, use SQLite or other DB


## if no need for session
# session.forget()
from gluon.tools import *
auth=Auth(globals(),test) # authentication/authorization
auth.settings.hmac_key='<your secret key>'
auth.define_tables() # creates all needed tables
crud=Crud(globals(),test) # for CRUD helpers using auth
service=Service(globals()) # for json, xml, jsonrpc, xmlrpc, amfrpc
# crud.settings.auth=auth # enforces authorization on crud

# mail=Mail() # mailer
# mail.settings.server='smtp.gmail.com:587' # your SMTP server
# mail.settings.sender='you@gmail.com' # your email
# mail.settings.login='username:password' # your credentials or None

# auth.settings.mailer=mail # for user email verification
# auth.settings.registration_requires_verification = True
# auth.settings.registration_requires_approval = True
# auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['verify_email'])+'/%(key)s to verify your email'
# auth.settings.reset_password_requires_verification = True
# auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL(r=request,c='default',f='user',args=['reset_password'])+'/%(key)s to reset your password'
## more options discussed in gluon/tools.py
#########################################################################

test.define_table('animal',
SQLField('sex', 'string'),
SQLField('type', 'string'),
SQLField('name', 'string'),
SQLField('extra', 'reference name')
)
test.define_table('person',
SQLField('color', 'int'),
SQLField('age', 'int', requires=IS_INT(), default='12'),
SQLField('name', 'string')
)
test.person.color.default=103
test.person.color.requires=IS_INT()

test.define_table('car',
SQLField('color', 'string'),
SQLField('age', 'int', requires=IS_INT(), default='12'),
SQLField('name', 'string')
)

Usage

Requirements

Download

Check out the source code at github page:

git clone git://github.com/blackthorne/web2py-yaml.git

Discussion

Read more on this web2py-users group thread.

Roadmap

last modified on 2016-07-06 02:44:31
View comments