Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 250 lines (159 sloc) 4.872 kb
e226631 @irrelative create blog page
irrelative authored
1 ---
2 layout: default
3 title: Basic Blog in 0.3
4 ---
5
6 # Basic Blog in 0.3
7
6bb7d9c @irrelative edit
irrelative authored
8 A really basic blog. Note there isn't authentication on this, so anyone could create a post, but it shows the basic functionality.
9
e226631 @irrelative create blog page
irrelative authored
10 ## Files
11
12 /blog.py
13 /schema.sql
14 /templates:
15 /templates/view.html
16 /templates/new.html
17 /templates/base.html
18 /templates/index.html
19 /templates/edit.html
20 /model.py
21
22
23 ###/blog.py
24
25
ac67254 @irrelative edit
irrelative authored
26 """ Basic blog using webpy 0.3 """
e226631 @irrelative create blog page
irrelative authored
27 import web
28 import model
29
30 ### Url mappings
31
32 urls = (
33 '/', 'Index',
c9734db fixed regex in url to work with multiple digits
anonymous authored
34 '/view/(\d+)', 'View',
e226631 @irrelative create blog page
irrelative authored
35 '/new', 'New',
c9734db fixed regex in url to work with multiple digits
anonymous authored
36 '/delete/(\d+)', 'Delete',
37 '/edit/(\d+)', 'Edit',
e226631 @irrelative create blog page
irrelative authored
38 )
39
40
41 ### Templates
42 t_globals = {
43 'datestr': web.datestr
44 }
45 render = web.template.render('templates', base='base', globals=t_globals)
46
47
48 class Index:
49
50 def GET(self):
51 """ Show page """
52 posts = model.get_posts()
53 return render.index(posts)
54
55
56 class View:
57
58 def GET(self, id):
59 """ View single post """
60 post = model.get_post(int(id))
61 return render.view(post)
62
63
64 class New:
65
66 form = web.form.Form(
67 web.form.Textbox('title', web.form.notnull,
68 size=30,
69 description="Post title:"),
70 web.form.Textarea('content', web.form.notnull,
71 rows=30, cols=80,
72 description="Post content:"),
73 web.form.Button('Post entry'),
74 )
75
76 def GET(self):
77 form = self.form()
78 return render.new(form)
79
80 def POST(self):
81 form = self.form()
82 if not form.validates():
83 return render.new(form)
84 model.new_post(form.d.title, form.d.content)
85 raise web.seeother('/')
86
87
88 class Delete:
89
90 def POST(self, id):
91 model.del_post(int(id))
92 raise web.seeother('/')
93
94
95 class Edit:
96
97 def GET(self, id):
98 post = model.get_post(int(id))
99 form = New.form()
100 form.fill(post)
101 return render.edit(post, form)
102
103
104 def POST(self, id):
105 form = New.form()
106 post = model.get_post(int(id))
107 if not form.validates():
108 return render.edit(post, form)
109 model.update_post(int(id), form.d.title, form.d.content)
110 raise web.seeother('/')
111
112
113 app = web.application(urls, globals())
114
115 if __name__ == '__main__':
116 app.run()
117
118
119
120 ###/schema.sql
121
122
123
124
125 CREATE TABLE entries (
126 id INT AUTO_INCREMENT,
127 title TEXT,
128 content TEXT,
129 posted_on DATETIME,
130 primary key (id)
131 );
132
133
134
135 ###/templates/view.html
136
137
138 $def with (post)
139
140 <h1>$post.title</h1>
141 $datestr(post.posted_on)<br/>
142
143 $post.content
144
145
146
147 ###/templates/new.html
148
149
150 $def with (form)
151
152
153 <h1>New Blog Post</h1>
154 <form action="" method="post">
155 $:form.render()
156 </form>
157
158
159
160 ###/templates/base.html
161
162
163 $def with (page)
164
165 <html>
166 <head>
167 <title>My Blog</title>
168 <style>
169 #menu {
170 width: 200px;
171 float: right;
172 }
173 </style>
174 </head>
175 <body>
176
177 <ul id="menu">
178 <li><a href="/">Home</a></li>
179 <li><a href="/new">New Post</a></li>
180 </ul>
181
182 $:page
183
184 </body>
185 </html>
186
187
188
189 ###/templates/index.html
190
191
192 $def with (posts)
193
194 <h1>Blog posts</h1>
195
196 <ul>
197 $for post in posts:
198 <li>
199 <a href="/view/$post.id">$post.title</a>
200 from $datestr(post.posted_on)
201 <a href="/edit/$post.id">Edit</a>
202 </li>
203 </ul>
204
205
206
207 ###/templates/edit.html
208
209
210 $def with (post, form)
211
212 <h1>Edit $form.d.title</h1>
213
214 <form action="" method="post">
215 $:form.render()
216 </form>
217
218
219 <h2>Delete post</h2>
220 <form action="/delete/$post.id" method="post">
221 <input type="submit" value="Delete post"/>
222 </form>
223
224
225
226 ###/model.py
227
228
229 import web, datetime
230
231 db = web.database(dbn='mysql', db='blog', user='justin')
232
233 def get_posts():
234 return db.select('entries', order='id DESC')
235
236 def get_post(id):
237 try:
238 return db.select('entries', where='id=$id', vars=locals())[0]
239 except IndexError:
240 return None
241
242 def new_post(title, text):
243 db.insert('entries', title=title, content=text, posted_on=datetime.datetime.utcnow())
244
245 def del_post(id):
246 db.delete('entries', where="id=$id", vars=locals())
247
248 def update_post(id, title, text):
249 db.update('entries', where="id=$id", vars=locals(),
250 title=title, content=text)
Something went wrong with that request. Please try again.