Skip to content
Newer
Older
100644 147 lines (98 sloc) 2.65 KB
9ec0531 @irrelative Add todo 0.3
irrelative authored Jan 26, 2010
1 ---
2 layout: default
3 title: Basic Todo List in 0.3
4 ---
5
6 # Basic Todo List in 0.3
7
8 # Todo list
9
10 Very basic todo list in 0.3 webpy. Probably the most trivial database-backed app possible.
11
12 ## Files
13
14 /schema.sql
15 /templates:
16 /templates/base.html
17 /templates/index.html
18 /model.py
19 /todo.py
20
21
22 ###/schema.sql
23
24 CREATE TABLE todo (
25 id INT AUTO_INCREMENT,
26 title TEXT,
27 primary key (id)
28 );
29
30
31 ###/templates/base.html
32
33 $def with (page)
34
35 <html>
36 <head>
ca66e16 edit
anonymous authored Jan 30, 2010
37 <title>Todo list</title>
9ec0531 @irrelative Add todo 0.3
irrelative authored Jan 26, 2010
38 </head>
39 <body>
40
41 $:page
42
43 </body>
44 </html>
45
46
47 ###/templates/index.html
48
49
50 $def with (todos, form)
51
52 <table>
53 <tr>
54 <th>What to do ?</th>
55 <th></th>
56 </tr>
57 $for todo in todos:
58 <tr>
59 <td>$todo.title</td>
60 <td>
61 <form action="/del/$todo.id" method="post">
62 <input type="submit" value="Delete"/>
63 </form>
64 </td>
ca66e16 edit
anonymous authored Jan 30, 2010
65 </tr>
66 </table>
9ec0531 @irrelative Add todo 0.3
irrelative authored Jan 26, 2010
67
68 <form action="" method="post">
69 $:form.render()
70 </form>
71
72
73 ###/model.py
74
75
76 import web
77
78 db = web.database(dbn='mysql', db='todo', user='justin')
79
80 def get_todos():
81 return db.select('todo', order='id')
82
83 def new_todo(text):
84 db.insert('todo', title=text)
85
86 def del_todo(id):
87 db.delete('todo', where="id=$id", vars=locals())
88
89
90
91 ###/todo.py
92
93
94 """ Basic todo list using webpy 0.3 """
95 import web
96 import model
97
98 ### Url mappings
99
100 urls = (
101 '/', 'Index',
102 '/del/(\d+)', 'Delete'
103 )
104
105
106 ### Templates
107 render = web.template.render('templates', base='base')
108
109
110 class Index:
111
112 form = web.form.Form(
113 web.form.Textbox('title', web.form.notnull,
114 description="I need to:"),
115 web.form.Button('Add todo'),
116 )
117
118 def GET(self):
119 """ Show page """
120 todos = model.get_todos()
121 form = self.form()
122 return render.index(todos, form)
123
124 def POST(self):
125 """ Add new entry """
126 form = self.form()
127 if not form.validates():
128 todos = model.get_todos()
129 return render.index(todos, form)
130 model.new_todo(form.d.title)
131 raise web.seeother('/')
132
133
134
135 class Delete:
136
137 def POST(self, id):
138 """ Delete based on ID """
139 id = int(id)
140 model.del_todo(id)
141 raise web.seeother('/')
5591b16 edit
6yf authored Jan 30, 2010
142
143
9ec0531 @irrelative Add todo 0.3
irrelative authored Jan 26, 2010
144 app = web.application(urls, globals())
145
146 if __name__ == '__main__':
147 app.run()
Something went wrong with that request. Please try again.