Skip to content

Instantly share code, notes, and snippets.

@wgx731
Created August 19, 2011 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wgx731/1155998 to your computer and use it in GitHub Desktop.
Save wgx731/1155998 to your computer and use it in GitHub Desktop.
A simple enhancement version of python bottle framework "Todolist" tutorial. (Change GET to POST)
import sqlite3
from bottle import route, run, debug, template, request, validate, send_file, error
# only needed when you run Bottle on mod_wsgi
from bottle import default_app
@route('/todo')
def todo_list():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT id, task FROM todo WHERE status LIKE '1';")
result = c.fetchall()
c.close()
output = template('make_table', rows=result)
return output
@route('/new', method='GET')
def view_new():
return template('new_task.tpl')
@route('/new', method='POST')
def new_item():
if request.POST.get('save','').strip():
new = request.POST.get('task', '').strip()
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,1))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The new task was inserted into the database, the ID is %s</p> <a href="/todo">View List</a>' % new_id
else:
redirect('/new')
@route('/edit/:no', method='GET')
@validate(no=int)
def view_edit(no):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT task,status FROM todo WHERE id LIKE ?", (str(no)))
cur_data = c.fetchone()
c.close()
return template('edit_task', old = cur_data, no = no)
@route('/edit/:no', method='POST')
@validate(no=int)
def edit_item(no):
if request.POST.get('save','').strip():
edit = request.POST.get('task','').strip()
status = request.POST.get('status','').strip()
if status == 'open':
status = 1
else:
status = 0
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit,status,no))
conn.commit()
c.close()
return '<p>The item number %s was successfully updated</p> <a href="/todo">View List</a>' %no
else:
redirect('/edit/?',(no))
@route('/item:item#[1-9]+#')
def show_item(item):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT task FROM todo WHERE id LIKE ?", (item))
result = c.fetchall()
c.close()
if not result:
return 'This item number does not exist!'
else:
return 'Task: %s' %result[0]
@route('/help')
def help():
send_file('help.html', root='.')
@route('/json:json#[1-9]+#')
def show_json(json):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT task FROM todo WHERE id LIKE ?", (json))
result = c.fetchall()
c.close()
if not result:
return {'task':'This item number does not exist!'}
else:
return {'Task': result[0]}
@error(403)
def mistake403(code):
return 'There is a mistake in your url!'
@error(404)
def mistake404(code):
return 'Sorry, this page does not exist!'
debug(True)
run(reloader=True)
#remember to remove reloader=True and debug(True) when you move your application from development to a productive environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment