Skip to content

Commit f8444b8

Browse files
New handler for adding todo
Co-authored-by: Audrey Roy Greenfeld <[email protected]>
1 parent ef181b4 commit f8444b8

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

examples/adv_app2.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Todo:
1515
db.users = db.create(User, transform=True, pk='name')
1616
db.todos = db.create(Todo, transform=True)
1717

18-
1918
def user_auth_before(req, sess):
2019
auth = req.scope['auth'] = sess.get('auth', None)
2120
if not auth: return login_redir
@@ -25,26 +24,11 @@ def user_auth_before(req, sess):
2524
user_auth_before,
2625
skip=[r'/favicon\.ico', r'/static/.*', r'.*\.css', r'.*\.js', '/login']
2726
)
28-
29-
3027
app, rt = fast_app(hdrs=Theme.blue.headers(),before=beforeware)
3128

29+
# Authentication
3230
login_redir = Redirect('/login')
3331

34-
35-
@rt
36-
def index(auth):
37-
top = Grid(Div(A('logout', href=logout), style='text-align: right'))
38-
new_inp = Input(id="new-title", name="title", placeholder="New Todo")
39-
add = Form(Group(new_inp, Button("Add")),
40-
hx_post="/", target_id='todo-list', hx_swap="afterbegin")
41-
frm = Form(*db.todos(order_by='priority'),
42-
id='todo-list', cls='sortable', hx_post="/reorder", hx_trigger="end")
43-
44-
card = Card(Ul(frm), header=add, footer=Div(id='current-todo'))
45-
return Titled(f"{auth}'s Todo list", Container(top, card))
46-
47-
4832
@rt('/login')
4933
def get():
5034
frm = Form(
@@ -71,4 +55,25 @@ def logout(sess):
7155
del sess['auth']
7256
return login_redir
7357

58+
# Dashboard and control handlers
59+
@rt
60+
def index(auth):
61+
top = Grid(Div(A('logout', href=logout), style='text-align: right'))
62+
new_inp = Input(id="new-title", name="title", placeholder="New Todo")
63+
add = Form(Group(new_inp, Button("Add")),
64+
hx_post=add_todo, target_id='todo-list', hx_swap="afterbegin")
65+
frm = Form(*db.todos(order_by='priority'),
66+
id='todo-list', cls='sortable', hx_post="/reorder", hx_trigger="end")
67+
68+
card = Card(Ul(frm), header=add, footer=Div(id='current-todo'))
69+
return Titled(f"{auth}'s Todo list", Container(top, card))
70+
71+
@rt
72+
def add_todo(todo:Todo, auth):
73+
new_inp = LabelInput('Title', id="new-title", name="title", placeholder="New Todo", hx_swap_oob='true')
74+
# `insert` returns the inserted todo, which is appended to the start of the list, because we used
75+
# `hx_swap='afterbegin'` when creating the todo list form.
76+
return db.todos.insert(todo), new_inp
77+
78+
7479
serve()

0 commit comments

Comments
 (0)