Skip to content

Commit

Permalink
webapi
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesturk committed Sep 30, 2009
1 parent c385f90 commit 3112563
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
Empty file added matchbox/webapi/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions matchbox/webapi/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)

if __name__ == "__main__":
execute_manager(settings)
8 changes: 8 additions & 0 deletions matchbox/webapi/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DEBUG = True
TEMPLATE_DEBUG = DEBUG
TEMPLATE_LOADERS = ()
MIDDLEWARE_CLASSES = ()
ROOT_URLCONF = 'urls'
INSTALLED_APPS = (
'matchbox.webapi'
)
8 changes: 8 additions & 0 deletions matchbox/webapi/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('matchbox.webapi.views',
(r'^entity/$', 'entity'),
(r'^entity/(?P<uid>\w{32})/$', 'entity'),
(r'^make_merge/$', 'make_merge'),
(r'^commit_merge/$', 'commit_merge'),
)
68 changes: 68 additions & 0 deletions matchbox/webapi/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import datetime
try:
import json
except ImportError:
import simplejson as json
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
from matchbox.api import LocalClient

client = LocalClient('datacommons')

def _querydict_to_dict(qd):
d = {}
for k,l in qd.iterlists():
if len(l) == 1:
l = l[0]
d[str(k)] = l
return d

def _jsonize(data):
if isinstance(data, list):
return [_jsonize(i) for i in data]

for k,v in data.iteritems():
if isinstance(v, datetime.datetime):
data[k] = str(v)
return data

def entity(request, uid=None):
data = None
if request.method == 'POST':
if uid:
key = client.update(uid, **_querydict_to_dict(request.POST))
else:
try:
key = client.insert(_querydict_to_dict(request.POST))
except TypeError, e:
return HttpResponseBadRequest(str(e))
data = {'id':key}
elif request.method == 'GET':
if uid:
data = client.get(uid)
else:
data = list(client.search(**_querydict_to_dict(request.GET)))

return HttpResponse(json.dumps(_jsonize(data)))

@require_POST
def make_merge(request):
name = request.POST.get('name')
ids = request.POST.getlist('id')
source = request.POST.get('source')
_type = request.POST.get('type')

if not name:
return HttpResponseBadRequest('missing required parameter "name"')
if not ids or len(ids) < 2:
return HttpResponseBadRequest('must specify two or more id parameters to merge')

data = client.make_merge(name, ids, source, _type)

return HttpResponse(json.dumps(_jsonize(data)))

@require_POST
def commit_merge(request):
merge_record = _querydict_to_dict(request.POST)
new_id = client.commit_merge(merge_record)
return HttpResponse(json.dumps({'id':new_id}))

0 comments on commit 3112563

Please sign in to comment.