Skip to content

Commit

Permalink
Added /works/OL123W/editions.json API
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Chitipothu committed Sep 20, 2011
1 parent 23f4d96 commit 377e097
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
51 changes: 51 additions & 0 deletions openlibrary/plugins/openlibrary/api.py
@@ -0,0 +1,51 @@
"""API stuff."""
import web
import simplejson

from infogami.utils import delegate
from openlibrary.core import helpers as h

def setup():
# placeholder
pass

class work_editions(delegate.page):
path = "(/works/OL\d+W)/editions"
encoding = "json"

def GET(self, key):
doc = web.ctx.site.get(key)
if not doc or doc.type.key != "/type/work":
raise web.notfound('')
else:
i = web.input(limit=50, offset=0)
limit = h.safeint(i.limit) or 50
offset = h.safeint(i.offset) or 0

data = self.get_editions_data(doc, limit=limit, offset=offset)
return delegate.RawText(simplejson.dumps(data), content_type="applicaiton/json")

def get_editions_data(self, work, limit, offset):
if limit > 1000:
limit = 1000

keys = web.ctx.site.things({"type": "/type/edition", "works": work.key, "limit": limit, "offset": offset})
editions = web.ctx.site.get_many(keys, raw=True)

size = work.edition_count
links = {
"self": web.ctx.fullpath,
"work": work.key,
}

if offset > 0:
links['prev'] = web.changequery(offset=min(0, offset-limit))

if offset + len(editions) < size:
links['next'] = web.changequery(offset=offset+limit)

return {
"links": links
"size": size,
"entries": editions
}
3 changes: 3 additions & 0 deletions openlibrary/plugins/openlibrary/code.py
Expand Up @@ -779,6 +779,9 @@ def setup():
events.setup()
status.setup()

import api
api.setup()

from stats import stats_hook
delegate.app.add_processor(web.unloadhook(stats_hook))

Expand Down

0 comments on commit 377e097

Please sign in to comment.