Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 28ec7c2791
Fetching contributors…

Cannot retrieve contributors at this time

242 lines (194 sloc) 5.814 kb
layout title
default
Basic Todo List in 0.2

Basic Todo List in 0.2

DEPRECATED! This code won't work in the latest version of web.py.

Uses web.py microframework: web.py, template.py, form.py.

Requires PostgreSQL, lighttpd, psycopg2 and flup.

Note: This code is beta. This means: 2xplore at your own risk ;)

Files:

tidy/
  __init__.py
  app/
    __init__.py
    controller.py
  conf/
    httpd.conf
  db/
    model.sql
  static/
    favicon.ico
    icon.jpg
    style.css
  views/
    base.html
    login.html
    todos.html

Main:

init.py

#! /usr/bin/env python
'tidy: a tiny todo list web engine.'
__version__ = '0.01beta' 
__author__  = 'domimob'
__license__ = 'public domain'
#
# Modules import.
import web, app.controller
#
# App definition, via URL to class mapping.
tidy = (
    '/', 'app.controller.Index',
    '/session', 'app.controller.Session',
    '/todo/add', 'app.controller.ToDo',
    '/todo/del/(\d+)', 'app.controller.ToDo',
    '/style.css', 'app.controller.Style' ,)
#
# Database connection definition.
web.db_parameters = dict (
    dbn='postgres',
    user='domimob',
    pw='passwd', 
    db='tidydb' )
#
# App execution.
if __name__ == '__main__': web.run(tidy)

Model:

db/model.sql

-- PostgreSQL database model.
--
create table todos (id serial primary key, title text);

Controller:

app/init.py

# Application modules import.
#
import controller

app/controller.py

'todo-list web engine controller.'
__version__ = '0.01beta' 
__author__  = 'domimob' 
#
# Modules import.
import web, template, form
#
# Views directory definition.
view = template.render('views/')
# 
# Forms definition.
loginForm = form.Form(form.Password('password'))
addForm = form.Form(form.Textbox('new', form.notnull))
#
# RESTful class definition.
class Index:
    'Index page object definition.'
    def GET(self):
        'Render the index page.'
        session = web.cookies()
        if not session:
            form = loginForm()
            print view.base(view.login(form))
        else:
            list = web.select('todos')
            form = addForm()
            print view.base(view.todos(list, form), session)
#
class Session:
    'Session object definition.'
    def GET(self):
        'Manage the user logout.'
        web.setcookie('user', '', 'Mon, 01-Jan-2000 00:00:00 GMT') 
        web.redirect('/')
    def POST(self):
        'Manage the user login.'
        login = web.input()
        if login.password == web.db_parameters['pw']:
            web.setcookie('user', web.db_parameters['user'])
        web.redirect('/')
#
class ToDo:
    'ToDo object definition.'
    def GET(self, id):
        'Delete a todo item, identified by parameter id.'
        web.delete('todos', int(id))
        web.redirect('/')
    def POST(self):
        'Add a todo item.'
        form = addForm()
        if form.validates():
            todo = web.input()
            web.insert('todos', title=todo.new)
        web.redirect('/')
#
class Style:
    'Style object definition.'
    def GET(self):
        'Render the stylesheet.'
        web.header("Content-Type","text/css; charset=utf-8")
        print open('static/style.css').read()

Views:

views/base.html

$def with (view, session=None)
$# Base view definition.
$#
<html>
<head>
    <title>tidy</title>
    <link rel="stylesheet" type="text/css" href="http://domimob.infogami.com/wiki/style.css"/>
</head> 
<body> 
    <table class="base">
        <tr><td><img src="static/icon.jpg"/></td></tr>
        $if session:
            <tr><td><h3>$session.user's todo list</h3></tr></td>
        $else:
            <tr><td><h3>a tiny todo list web engine</h3></tr></td>
        <tr><td>$:view</tr></td>
    </table>
</body>
</html>

views/todos.html

$def with (list, form)
$# Todos view definition.
$# 
$for todo in list:
    <a class ="todo" href="todo/del/$todo.id" title="click to delete">$todo.title</a><br/>
<form method="post" action="todo/add">
    <br/>$:form.render()
</form>
<a href="/session">.logout</a>

views/login.html

$def with (form)
$# Login view definition.
$#
<form method="post" action="session">
    <br/>$:form.render()
</form>

Static Files:

static/style.css

body { text-align: left; font-family: arial; color: #C0C0C0 } 
table.base { width: 50%; text-align: left; margin: auto } 
a { text-decoration: none; color: gray } 
a:hover { text-decoration: overline }
a.todo { text-decoration: none; font-weight: bolder; color: black } 
a.todo:hover { text-decoration: line-through }

Web Server:

conf/httpd.conf

# LigHTTPd web server configuration.
#
# Server configuration.
server.document-root = "/Users/domimob/lab/tidy/"
server.modules = ("mod_fastcgi", "mod_rewrite")
server.port = 8080
#
# FastCGI configuration.
fastcgi.server = ( 
    "__init__.py" => ((
        "bin-path" => server.document-root + "/__init__.py",
        "socket" => "/tmp/fastcgi.socket",
        "max-procs" => 1 )))
#
# mod_rewrite configuration.
url.rewrite-once = (
    "^/favicon.ico$" => "/static/favicon.ico", 
    "^/static/(.*)$" => "/static/$1", 
    "^/(.*)$" => "__init__.py/$1" ,)
Jump to Line
Something went wrong with that request. Please try again.