Skip to content

Commit 34cae1b

Browse files
committed
better claude.md
1 parent 6631180 commit 34cae1b

1 file changed

Lines changed: 45 additions & 32 deletions

File tree

CLAUDE.md

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,36 @@ def index():
4444
return dict(message="Hello")
4545

4646
# With URL parameters
47-
@action("item/<id:int>")
47+
@action("api/item/<id:int>")
4848
@action.uses(db, auth.user)
49-
def item(id):
50-
record = db.thing[id] or abort(404)
49+
def item(item_id):
50+
record = db.thing[item_id] or abort(404)
5151
return dict(record=record)
5252

5353
# API endpoint returning JSON (no template)
54-
@action("api/data", method="GET")
54+
@action("api/items", method="GET")
5555
@action.uses(db, auth.user)
56-
def api_data():
57-
rows = db(db.thing).select()
58-
return dict(data=rows.as_list())
56+
def api_items():
57+
items = db(db.thing).select()
58+
return dict(data=itemas.as_list())
5959

6060
# POST action
61-
@action("api/create", method="POST")
61+
@action("api/item", method="POST")
6262
@action.uses(db, auth.user)
63-
def api_create():
64-
db.thing.insert(**request.json)
65-
return dict(ok=True)
63+
def api_new_item():
64+
res = db.thing.validate_and_insert(**request.json)
65+
return dict(ok=record_id=res.get("id"), errors=res.get("errors"), ok=not res.get("errors"))
66+
67+
# PUT action
68+
@action("api/item/<id:int>", method="PUT")
69+
@action.uses(db, auth.user)
70+
def api_new_item(item_id):
71+
res = db.thing.validate_and_update(item_id, **request.json)
72+
return dict(record_id=res.get("id"), errors=res.get("errors"), ok=bool(res.get("updated")))
73+
6674
```
6775

76+
6877
**Rules:**
6978
- `@action("path")` defines the route. The URL is `/{app_name}/{path}`.
7079
- `@action.uses(...)` declares fixtures. Template must be **first** if present.
@@ -144,12 +153,33 @@ rows = db(query).select()
144153
### Forms
145154

146155
```python
147-
form = Form(db.thing, csrf_session=session)
148-
if form.accepted:
149-
redirect(URL("index"))
150-
return dict(form=form)
156+
from py4web import action, URL, redirect, request, abort
157+
from .common import db, session, auth, T, flash, cache
158+
from utils.form import Form
159+
160+
# create form
161+
@action("create_item")
162+
@action.uses(db, auth.user, "create_template.html")
163+
def create_item():
164+
form = Form(db.thing) # does postback
165+
if form.accepted:
166+
# on success
167+
item_id = form.vars.get("id")
168+
redirect(URL("other_action", item_id))
169+
return dict(form=form)
170+
171+
# create form
172+
@action("edit_item")
173+
@action.uses(db, auth.user, "edit_template.html")
174+
def edit_item(item_id):
175+
form = Form(db.thing, item_id) # does postback
176+
if form.accepted:
177+
# on success
178+
redirect(URL("other_action", item_id))
179+
return dict(form=form)
151180
```
152181

182+
- template but contain [[=form]] to embed the form
153183
- `Form(db.table)` for create, `Form(db.table, record_id)` for edit.
154184
- Check `form.accepted`, `form.deleted`, `form.errors`.
155185

@@ -180,23 +210,6 @@ URL("static", "js/index.js") # /{app}/static/js/index.js
180210
URL("action", scheme=True) # https://host/{app}/action
181211
```
182212

183-
### Custom Fixtures
184-
185-
```python
186-
from py4web.core import Fixture
187-
188-
class MyFixture(Fixture):
189-
def on_request(self, context):
190-
# Called before action
191-
pass
192-
def on_success(self, context):
193-
# Called after successful action
194-
pass
195-
def on_error(self, context):
196-
# Called on error
197-
pass
198-
```
199-
200213
### URL Signing (CSRF/tamper protection for callbacks)
201214

202215
```python

0 commit comments

Comments
 (0)