handler = logging.FileHandler(filename) handler.setFormatter(formatter) logger.setLevel(getattr(logging, level.upper(), "DEBUG")) logger.addHandler(handler) # connect to db db = DAL( settings.DB_URI, folder=settings.DB_FOLDER, pool_size=settings.DB_POOL_SIZE, migrate=settings.DB_MIGRATE, fake_migrate=settings.DB_FAKE_MIGRATE, ) # define global objects that may or may not be used by th actions cache = Cache(size=1000) T = Translator(settings.T_FOLDER) flash = Flash() # pick the session type that suits you best if settings.SESSION_TYPE == "cookies": session = Session(secret=settings.SESSION_SECRET_KEY) elif settings.SESSION_TYPE == "redis": import redis host, port = settings.REDIS_SERVER.split(":") # for more options: https://github.com/andymccurdy/redis-py/blob/master/redis/client.py conn = redis.Redis(host=host, port=int(port)) conn.set = (lambda k, v, e, cs=conn.set, ct=conn.ttl: cs(k, v, ct(k)) if ct(k) >= 0 else cs(k, v, e)) session = Session(secret=settings.SESSION_SECRET_KEY, storage=conn)
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 db.thing.name.default = "test_clone" field_clone = copy.copy(db.thing.name)