Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tried to add content negotiation

  • Loading branch information...
commit 643d5442d482a8aaa425c6ed428fba0d360db037 1 parent eb33f89
@dAnjou authored
Showing with 36 additions and 8 deletions.
  1. +31 −3 todo.py
  2. +5 −5 views/index.tpl
View
34 todo.py
@@ -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')
@@ -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):
View
10 views/index.tpl
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.