home > Basic Blog in 0.3

Basic Blog in 0.3

A really basic blog. Note there isn’t authentication on this, so anyone could create a post, but it shows the basic functionality.




""" Basic blog using webpy 0.3 """
import web
import model

### Url mappings

urls = (
    '/', 'Index',
    '/view/(\d+)', 'View',
    '/new', 'New',
    '/delete/(\d+)', 'Delete',
    '/edit/(\d+)', 'Edit',

### Templates
t_globals = {
    'datestr': web.datestr
render = web.template.render('templates', base='base', globals=t_globals)

class Index:

    def GET(self):
        """ Show page """
        posts = model.get_posts()
        return render.index(posts)

class View:
    def GET(self, id):
        """ View single post """
        post = model.get_post(int(id))
        return render.view(post)

class New:

    form = web.form.Form(
        web.form.Textbox('title', web.form.notnull, 
            description="Post title:"),
        web.form.Textarea('content', web.form.notnull, 
            rows=30, cols=80,
            description="Post content:"),
        web.form.Button('Post entry'),

    def GET(self):
        form = self.form()
        return render.new(form)

    def POST(self):
        form = self.form()
        if not form.validates():
            return render.new(form)
        model.new_post(form.d.title, form.d.content)
        raise web.seeother('/')

class Delete:
    def POST(self, id):
        raise web.seeother('/')

class Edit:

    def GET(self, id):
        post = model.get_post(int(id))
        form = New.form()
        return render.edit(post, form)

    def POST(self, id):
        form = New.form()
        post = model.get_post(int(id))
        if not form.validates():
            return render.edit(post, form)
        model.update_post(int(id), form.d.title, form.d.content)
        raise web.seeother('/')

app = web.application(urls, globals())

if __name__ == '__main__':


CREATE TABLE entries (
    title TEXT,
    content TEXT,
    posted_on DATETIME,
    primary key (id)


$def with (post)




$def with (form)

<h1>New Blog Post</h1>
<form action="" method="post">


$def with (page)

    <title>My Blog</title>
        #menu {
            width: 200px;
            float: right;

<ul id="menu">
    <li><a href="/">Home</a></li>
    <li><a href="/new">New Post</a></li>




$def with (posts)

<h1>Blog posts</h1>

$for post in posts:
        <a href="/view/$post.id">$post.title</a> 
        from $datestr(post.posted_on) 
        <a href="/edit/$post.id">Edit</a>


$def with (post, form)

<h1>Edit $form.d.title</h1>

<form action="" method="post">

<h2>Delete post</h2>
<form action="/delete/$post.id" method="post">
    <input type="submit" value="Delete post"/>


import web, datetime

db = web.database(dbn='mysql', db='blog', user='justin')

def get_posts():
    return db.select('entries', order='id DESC')

def get_post(id):
        return db.select('entries', where='id=$id', vars=locals())[0]
    except IndexError:
        return None

def new_post(title, text):
    db.insert('entries', title=title, content=text, posted_on=datetime.datetime.utcnow())

def del_post(id):
    db.delete('entries', where="id=$id", vars=locals())

def update_post(id, title, text):
    db.update('entries', where="id=$id", vars=locals(),
        title=title, content=text)