@@ -15,7 +15,6 @@ class Todo:
15
15
db .users = db .create (User , transform = True , pk = 'name' )
16
16
db .todos = db .create (Todo , transform = True )
17
17
18
-
19
18
def user_auth_before (req , sess ):
20
19
auth = req .scope ['auth' ] = sess .get ('auth' , None )
21
20
if not auth : return login_redir
@@ -25,26 +24,11 @@ def user_auth_before(req, sess):
25
24
user_auth_before ,
26
25
skip = [r'/favicon\.ico' , r'/static/.*' , r'.*\.css' , r'.*\.js' , '/login' ]
27
26
)
28
-
29
-
30
27
app , rt = fast_app (hdrs = Theme .blue .headers (),before = beforeware )
31
28
29
+ # Authentication
32
30
login_redir = Redirect ('/login' )
33
31
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
-
48
32
@rt ('/login' )
49
33
def get ():
50
34
frm = Form (
@@ -71,4 +55,25 @@ def logout(sess):
71
55
del sess ['auth' ]
72
56
return login_redir
73
57
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
+
74
79
serve ()
0 commit comments