Skip to content

Instantly share code, notes, and snippets.

@syshack
Created May 21, 2012 07:26
Show Gist options
  • Select an option

  • Save syshack/2760939 to your computer and use it in GitHub Desktop.

Select an option

Save syshack/2760939 to your computer and use it in GitHub Desktop.
a todo demo in sae
p>编辑 ID 为 {{no}} 的任务</p>
<form action="/edit/{{no}}" method="GET">
<input type="text" name="task" value="{{old[0]}}" size="100" maxlength="100" />
<select name="status">
<option value="1">开启</option>
<option value="0">关闭</option>
</select>
<br />
<input type="submit" name="save" value="save" />
</form>
<p>开启的项目如下:</p>
<table border="1">
%for row in list(rows):
<tr>
%for r in row:
<td>{{r}}</td>
%end
</tr>
%end
</table>
<p>添加一个新的任务到 ToDo 列表中:</p>
<form action="/add" method="GET">
<input type="text" size="100" maxlength="100" name="todo" />
<input type="submit" name="save" value="save" />
</form>
from bottle import *
import sae
app=Bottle()
def db_conn():
import MySQLdb as mydb
import sae.const
host=sae.const.MYSQL_HOST
user=sae.const.MYSQL_USER
passwd=sae.const.MYSQL_PASS
port=sae.const.MYSQL_PORT
db=sae.const.MYSQL_DB
conn=mydb.connect(host=host,port=int(port),user=user,passwd=passwd,db=db,charset="utf8")
return conn
@app.route('/todo')
#----------------------------------------------------------------------
def show_todo_list():
"""A simple todo list"""
conn=db_conn()
c=conn.cursor()
c.execute("SELECT id,task FROM todo WHERE status LIKE '1'")
todos=c.fetchall()
c.close()
conn.close
output=template('make_table',rows=todos)
return output
@app.get("/add")
#----------------------------------------------------------------------
def add_todo():
"""Add todo"""
if request.GET.get('save','').strip():
new=request.GET.get('todo','').strip()
conn=db_conn()
c=conn.cursor()
c.execute("INSERT INTO todo(task, status) VALUES('%s', 1)", %(new))
new_id = c.lastrowid
c.close()
conn.close()
return '<p>新的项目被插入到数据库,其 ID 为 %s</p>' % new_id
else:
return template('new_task.tpl')
@app.get("/edit/:no")
#----------------------------------------------------------------------
def edit_item(no):
"""Edit todos"""
conn=db_conn()
c=conn.cursor()
if request.GET.get('save','').strip():
edit=request.GET.get('todo','').strip()
st=request.GET.get('status','').strip()
c.execute('UPDATE todo SET status=%s WHERE id like %s' %(st, no))
c.close()
conn.close()
return '<p>项目编号 %s 已经被成功更新</p>' %no
else:
c.execute('SELECT task FROM todo WHERE id like %s' %(str(no)))
cur_data=c.fetchone()
c.close()
conn.close()
return template('edit_task', old=cur_data, no=no)
application = sae.create_wsgi_app(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment