-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontrollers_components.py
More file actions
151 lines (116 loc) · 5 KB
/
Copy pathcontrollers_components.py
File metadata and controls
151 lines (116 loc) · 5 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
This file defines actions, i.e. functions the URLs are mapped into
The @action(path) decorator exposed the function at URL:
http://127.0.0.1:8000/{app_name}/{path}
If app_name == '_default' then simply
http://127.0.0.1:8000/{path}
If path == 'index' it can be omitted:
http://127.0.0.1:8000/
The path follows the bottlepy syntax.
@action.uses('generic.html') indicates that the action uses the generic.html template
@action.uses(session) indicates that the action uses the session
@action.uses(db) indicates that the action uses the db
@action.uses(T) indicates that the action uses the i18n & pluralization
@action.uses(auth.user) indicates that the action requires a logged in user
@action.uses(auth) indicates that the action requires the auth object
session, db, T, auth, and tempates are examples of Fixtures.
Warning: Fixtures MUST be declared with @action.uses({fixtures}) else your app will result in undefined behavior
"""
import datetime
import uuid
from py4web import action, request, abort, redirect, URL, Field
from py4web.utils.form import Form, FormStyleBulma
from py4web.utils.url_signer import URLSigner
from pydal.validators import *
from yatl.helpers import A
from .common import db, session, T, cache, auth
from .components.grid import Grid
from .components.vueform import VueForm, InsertForm, TableForm
from .components.fileupload import FileUpload
from .components.starrater import StarRater
signed_url = URLSigner(session)
# -----------------------------
# Sample grid.
vue_grid = Grid('grid_api', session)
@action('vuegrid', method=['GET'])
@action.uses(vue_grid, 'vuegrid.html')
def vuegrid():
"""This page generates a sample grid."""
# We need to instantiate our grid component.
return dict(grid=vue_grid())
# -----------------------------
# File uploader.
file_uploader = FileUpload('upload_api', session)
@action('file_uploader', method=['GET'])
@action.uses(file_uploader, 'file_uploader.html')
def fileuploader():
return dict(uploader=file_uploader(id=1))
# -----------------------------
# Custom vue form.
def get_time():
return datetime.datetime.utcnow()
vue_form = VueForm('test_form', session,
[Field('name', default="Luca"),
Field('last_name', default="Smith", writable=False),
Field('read', 'boolean', default=True),
Field('animal', requires=IS_IN_SET(['cat', 'dog', 'bird']),
default='dog', writable=False),
Field('choice', requires=IS_IN_SET({'c': 'cat', 'd': 'dog', 'b': 'bird'}),
default='d'),
Field('arrival_time', 'datetime', default=get_time),
Field('date_of_birth', 'date'),
Field('narrative', 'text'),
], readonly=False, redirect_url='index')
@action('vue_form', method=['GET'])
@action.uses(auth.user, vue_form, "vueform.html")
def vueform():
return dict(form=vue_form())
# -----------------------------
# Insertion form.
def not_too_expensive(fields):
"""Validation function that checks that the total price is low enough."""
if (fields['product_quantity']['validated_value'] *
fields['product_cost']['validated_value']) > 1000000:
err = "Please insert only products with total value of less than a million."
fields['product_quantity']['error'] = err
fields['product_cost']['error'] = err
insert_form = InsertForm('insert_product', session, db.product,
validate=not_too_expensive, redirect_url='index')
@action('insert_form', method=['GET'])
@action.uses(insert_form, 'vueform.html')
def insertform():
return dict(form=insert_form())
# -----------------------------
# Update form.
update_form = TableForm('update_product', session, db.product,
validate=not_too_expensive, redirect_url='index')
@action('update_form', method=['GET'])
@action.uses(update_form, 'vueform.html')
def updateform():
# For simplicity, we update the record 1.
return dict(form=update_form(id=1))
# -----------------------------
# Star rater.
star_rater = StarRater('star_rater', session)
@action('star_rater', method=['GET'])
@action.uses(star_rater, 'starrating.html')
def starrater():
# This performs a star rating of item 1.
return dict(stars=star_rater(id=1))
# ------------------------------
# Star rater, instantiated from Vue.
@action('star_rater_vue', method=['GET'])
@action.uses(star_rater, 'star_rater_vue.html')
def star_rater_vue():
return dict(get_posts_url=URL('star_rater_get_posts'))
@action('star_rater_get_posts', method=['GET'])
def star_rater_get_posts():
posts = [
{"id": 1, "content": "Hello there"},
{"id": 2, "content": "I love you"},
{"id": 3, "content": "Do you love me too?"},
]
for p in posts:
# Creates the callback URL for each rater.
p["url"] = star_rater.url(p["id"])
return dict(posts=posts)