Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
112 lines (94 sloc) 2.94 KB
from bottle import route, run, template, redirect, debug, static_file, request, abort
from couchdbkit import Server, Document, StringProperty
from couchdbkit.designer import push
import simplejson as json
except ImportError:
import json
import mimerender
mimerender = mimerender.BottleMimeRender()
couch = Server()
db = couch.get_or_create_db('simple-todo')
push('db/todolist', db)
push('db/todo', db)
class TodoList(Document):
class Todo(Document):
title = StringProperty()
priority = StringProperty()
todolist = StringProperty()
def server_static(filepath):
return static_file(filepath, root='./static')
def index():
todolist = TodoList()
redirect('/%s/' % todolist['_id'])
render_json = lambda **x: json.dumps({"lists": [l['_id'] for l in x["lists"]]})
render_html = lambda **x: template('list', lists=x["lists"])
render_txt = lambda **x: "\n".join([l['_id'] for l in x["lists"]])
default = 'html',
html = render_html,
json = render_json,
txt = render_txt
def todolists():
lists = TodoList.view('todolist/all')
return {"lists": lists}
render_json = lambda **x: json.dumps(x)
render_html = lambda **x: template('index', todos=x["todos"])
render_txt = lambda **x: "\n".join([y["title"] for y in x["todos"]])
default = 'html',
html = render_html,
json = render_json,
txt = render_txt
def todolist(list_id):
todolist = TodoList.get_or_create(list_id)
all_todos = Todo.view('todo/all')
todos = []
for todo in all_todos:
if todo.todolist == todolist['_id']:
todos.append(dict(todo, id=todo['_id']))
return {"todos": todos}
@route('/:list_id/add', method='POST')
def add(list_id):
todolist = TodoList.get(list_id)
title = request.forms.get('title')
priority = request.forms.get('priority')
if title and not title.strip() == "":
todo = Todo(title=title)
if priority:
todo.priority = priority
todo.todolist = list_id
redirect('/%s/' % list_id)
@route('/t/delete', method='POST')
def delete():
todo_id = request.forms.get('todo_id')
if todo_id.startswith('todo-'):
t = Todo.get(todo_id[5:])
return "success"
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", default='wsgiref', choices=['wsgiref', 'flup'])
parser.add_argument("-d", "--debug", action="store_true")
parser.add_argument("-r", "--reloader", action="store_true")
args = parser.parse_args()
if args.server == 'wsgiref':
if args.server == 'flup':
run(server='flup', bindAddress='/tmp/simple-todo.sock', reloader=args.reloader)
Something went wrong with that request. Please try again.