import multiprocessing import os import time import unittest import uuid import mechanize from py4web import action, DAL, Field, Session, Cache from py4web.core import bottle, request, error404 os.environ["PY4WEB_APPS_FOLDER"] = os.path.sep.join( os.path.normpath(__file__).split(os.path.sep)[:-2]) db = DAL("sqlite://storage_%s" % uuid.uuid4(), folder="/tmp/") db.define_table("thing", Field("name")) session = Session(secret="my secret") cache = Cache() action.app_name = "tests" @action("index") @cache.memoize(expiration=1) @action.uses(db, session) @action.requires(lambda: True) def index(): db.thing.insert(name="test") session["number"] = session.get("number", 0) + 1 # test copying Field ThreadSafe attr
import os from py4web import action, request, DAL, Field, Session, Cache, user_in # define session and cache objects session = Session(secret="some secret") cache = Cache(size=1000) # define database and tables db = DAL("sqlite://storage.db", folder=os.path.join(os.path.dirname(__file__), "databases")) db.define_table("todo", Field("info")) db.commit() # example index page using session, template and vue.js @action("index") # the function below is exposed as a GET action @action.uses("index.html") # we use the template index.html to render it @action.uses(session) # action needs a session object (read/write cookies) def index(): session["counter"] = session.get("counter", 0) + 1 session["user"] = {"id": 1} # store a user in session return dict(session=session) # example of GET/POST/DELETE RESTful APIs @action("api") # a GET API function @action.uses(session) # we load the session @action.requires(user_in(session) ) # then check we have a valid user in session
import os from py4web import DAL, Field # define database and tables db = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases')) db.define_table( 'person', Field('name'), Field('job')) db.define_table( 'superhero', Field('name'), Field('real_identity', 'reference person')) db.define_table( 'superpower', Field('description')) db.define_table( 'tag', Field('superhero', 'reference superhero'), Field('superpower', 'reference superpower'), Field('strength', 'integer')) if not db(db.person).count(): db.person.insert(name='Clark Kent', job='Journalist') db.person.insert(name='Peter Park', job='Photographer') db.person.insert(name='Bruce Wayne', job='CEO') db.superhero.insert(name='Superman', real_identity=1)
import multiprocessing import os import time import unittest import uuid import mechanize from py4web import action, DAL, Field, Session, Cache from py4web.core import bottle, request, error404 os.environ['PY4WEB_APPS_FOLDER'] = os.path.sep.join( os.path.normpath(__file__).split(os.path.sep)[:-2]) db = DAL('sqlite://storage_%s' % uuid.uuid4(), folder='/tmp/') db.define_table('thing', Field('name')) session = Session(secret='my secret') cache = Cache() action.app_name = 'tests' @action('index') @cache.memoize(expiration=1) @action.uses(db, session) @action.requires(lambda: True) def index(): db.thing.insert(name='test') session['number'] = session.get('number', 0) + 1 return 'ok %s %s' % (session['number'], db(db.thing).count())
import os from py4web import DAL, Field from pydal.validators import IS_NOT_EMPTY, IS_NOT_IN_DB # define database and tables db = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases')) # simple table example db.define_table('person', Field('name', requires=IS_NOT_IN_DB(db, 'person.name')), Field('job', requires=IS_NOT_EMPTY())) # simple reference example db.define_table('superhero', Field('name'), Field('real_identity', 'reference person')) db.define_table('superpower', Field('description')) # many to many example db.define_table('tag', Field('superhero', 'reference superhero'), Field('superpower', 'reference superpower'), Field('strength', 'integer')) if not db(db.person).count(): db.person.insert(name='Clark Kent', job='Journalist') db.person.insert(name='Peter Park', job='Photographer') db.person.insert(name='Bruce Wayne', job='CEO') db.superhero.insert(name='Superman', real_identity=1) db.superhero.insert(name='Spiderman', real_identity=2) db.superhero.insert(name='Batman', real_identity=3) db.superpower.insert(description='Flight')
import os from py4web import action, request, DAL, Field, Session, Cache, user_in # define session and cache objects session = Session(secret='some secret') cache = Cache(size=1000) # define database and tables db = DAL('sqlite://storage.db', folder=os.path.join(os.path.dirname(__file__), 'databases')) db.define_table('todo', Field('info')) # example index page using session, template and vue.js @action('index') # the function below is exposed as a GET action @action.uses('index.html') # we use the template index.html to render it @action.uses(session) # action needs a session object (read/write cookies) def index(): session['counter'] = session.get('counter', 0) + 1 session['user'] = {'id': 1} # store a user in session return dict(session=session) # example of GET/POST/DELETE RESTful APIs @action('api') # a GET API function @action.uses(session) # we load the session @action.requires(user_in(session) ) # then check we have a valid user in session @action.uses(db) # all before starting a db connection