Skip to content

Commit

Permalink
tried to add content negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
dAnjou committed Nov 21, 2012
1 parent eb33f89 commit 643d544
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
34 changes: 31 additions & 3 deletions todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
from couchdbkit import Server, Document, StringProperty
from couchdbkit.designer import push

try:
import simplejson as json
except ImportError:
import json
import mimerender

mimerender = mimerender.BottleMimeRender()

couch = Server()
db = couch.get_or_create_db('simple-todo')

Expand Down Expand Up @@ -30,20 +38,40 @@ def index():
todolist.save()
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"]])

@route('/l/')
@mimerender(
default = 'html',
html = render_html,
json = render_json,
txt = render_txt
)
def todolists():
lists = TodoList.view('todolist/all')
return template('list', lists=lists)
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"]])

@route('/:list_id/')
@mimerender(
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(todo)
return template('index', todos=todos)
todos.append(dict(todo, id=todo['_id']))
return {"todos": todos}

@route('/:list_id/add', method='POST')
def add(list_id):
Expand Down
10 changes: 5 additions & 5 deletions views/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@
%for t in todos:
<li>
<div class="alert-box">
%if t.priority == 'lazy':
%if t["priority"] == 'lazy':
<span class="blue radius label">lazy</span>
%end
%if t.priority == 'soon':
%if t["priority"] == 'soon':
<span class="red radius label">soon</span>
%end
%if t.priority == 'now':
%if t["priority"] == 'now':
<span class="black radius label">now</span>
%end
{{ t.title }}
<a href="#" class="delete" id="todo-{{ t['_id'] }}">&times;</a>
{{ t["title"] }}
<a href="#" class="delete" id="todo-{{ t['id'] }}">&times;</a>
</div>
</li>
%end
Expand Down

0 comments on commit 643d544

Please sign in to comment.