def test_session_in_db(self): request.app_name = 'myapp' db = DAL('sqlite:memory') session = Session(secret="a", expiration=10, storage=DBStore(db)) request.cookies.clear() session.on_request() session['key'] = 'value' session.on_success() cookie_name = session.local.session_cookie_name a, b = str( response._cookies)[len('Set-Cookie: '):].split(';')[0].split( '=', 1) request.cookies[a] = b request.cookies = response._cookies session.local.data.clear() session = Session(expiration=10, storage=DBStore(db)) session.on_request() self.assertEqual(session.get('key'), 'value') request.cookies[a] = 'wrong_cookie' session = Session(expiration=10, storage=DBStore(db)) session.on_request() self.assertEqual(session.get('key'), None)
import os from web3py import DAL, Field from . import settings db = DAL(settings.DB_URI, folder=settings.DB_FOLDER, pool_size=settings.DB_POOL_SIZE) ### Define you table below # # db.define_table('thing', Field('name')) #
import os from web3py 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 def todo(): return dict(items=db(db.todo).select(orderby=~db.todo.id).as_list()) @action('api', method='POST') @action.uses(session)
import os from web3py import DAL, Field db = DAL('sqlite://test', folder=os.path.join(os.path.dirname(__file__), 'databases')) db.define_table('thing', Field('name'))
import os from web3py 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 def todo(): return dict(items=db(db.todo).select(orderby=~db.todo.id).as_list()) @action('api', method='POST') @action.uses(session)
import multiprocessing import os import time import unittest import uuid import mechanize from web3py import action, DAL, Field, Session, Cache from web3py.core import bottle os.environ['WEB3PY_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('index') @cache.memoize(expiration=1) @action.uses(db, session) def index(): db.thing.insert(name='test') session['number'] = session.get('number', 0) + 1 return 'ok %s %s' % (session['number'], db(db.thing).count()) class CacheAction(unittest.TestCase): def setUp(self):
from web3py import DAL, Field, expose, tag, cat, safe, HTTP, url, Form, DALForm, current from web3py.session import SessionCookieManager from web3py.dal import Transact from web3py.validators import IS_NOT_EMPTY db = DAL('sqlite://storage.test') db.define_table('person',Field('name',requires=IS_NOT_EMPTY())) expose.common_cleaners = [SessionCookieManager('test'),Transact(db)] @expose() def index(): form = Form(Field('name',requires=IS_NOT_EMPTY()), Field('area','text'), Field('check','boolean',default=True), Field('age','integer',comment='Try type in a number!'), Field('weight','double'), Field('a','time'), Field('b','date'), Field('c','datetime')).process() message = form.vars.name or '' return locals() @expose() def error(): return 1/0 @expose() def counter(): current.session.counter = (current.session.counter or 0)+1 return current.session.counter
import os from web3py 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)
from web3py import DAL, Field, expose, tag, cat, safe, HTTP, url, Form, DALForm, current from web3py.session import SessionCookieManager from web3py.dal import Transact from web3py.validators import IS_NOT_EMPTY db = DAL("sqlite://storage.test") db.define_table("person", Field("name", requires=IS_NOT_EMPTY())) expose.common_cleaners = [SessionCookieManager("test"), Transact(db)] @expose() def index(): form = Form( Field("name", requires=IS_NOT_EMPTY()), Field("area", "text"), Field("check", "boolean", default=True), Field("age", "integer", comment="Try type in a number!"), Field("weight", "double"), Field("a", "time"), Field("b", "date"), Field("c", "datetime"), ).process() message = form.vars.name or "" return locals() @expose() def error(): return 1 / 0