Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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
[Field("name", requires=IS_NOT_EMPTY()), Field("insane", "boolean")],
form_name="4",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field("color", requires=IS_IN_SET(["red", "blue", "green"])),
],
form_name="5",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field(
"favorite_hero", requires=IS_IN_DB(db, "person.id", "person.name")
),
],
form_name="6",
formstyle=FormStyleBulma,
),
]
messages = []
for form in forms:
if form.accepted:
messages.append("form %s accepted with: %s " % (form.form_name, form.vars))
elif form.errors:
messages.append("form %s has errors: %s " % (form.form_name, form.errors))
return dict(forms=forms, messages=messages)
form_name="1",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY())],
form_name="2",
keep_values=True,
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("age", "integer")],
form_name="3",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("insane", "boolean")],
form_name="4",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field("color", requires=IS_IN_SET(["red", "blue", "green"])),
],
form_name="5",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field(
"favorite_hero", requires=IS_IN_DB(db, "person.id", "person.name")
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("age", "integer")],
form_name="3",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("insane", "boolean")],
form_name="4",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field("color", requires=IS_IN_SET(["red", "blue", "green"])),
],
form_name="5",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field(
"favorite_hero", requires=IS_IN_DB(db, "person.id", "person.name")
),
],
form_name="6",
formstyle=FormStyleBulma,
),
]
messages = []
import os
from py4web import DAL, Field
db = DAL('sqlite://test', folder=os.path.join(os.path.dirname(__file__), 'databases'))
db.define_table('thing', Field('name'))
def example_multiple_forms():
name = Field("name", requires=IS_NOT_EMPTY())
forms = [
Form(
[Field("name", requires=IS_NOT_EMPTY())],
form_name="1",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY())],
form_name="2",
keep_values=True,
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("age", "integer")],
form_name="3",
formstyle=FormStyleBulma,
def example_multiple_forms():
name = Field("name", requires=IS_NOT_EMPTY())
forms = [
Form(
[Field("name", requires=IS_NOT_EMPTY())],
form_name="1",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY())],
form_name="2",
keep_values=True,
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("age", "integer")],
form_name="3",
formstyle=FormStyleBulma,
),
Form(
[Field("name", requires=IS_NOT_EMPTY()), Field("insane", "boolean")],
form_name="4",
formstyle=FormStyleBulma,
),
Form(
[
Field("name", requires=IS_NOT_EMPTY()),
Field("color", requires=IS_IN_SET(["red", "blue", "green"])),
],
form_name="5",
formstyle=FormStyleBulma,
),