Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

webapi

  • Loading branch information...
commit 3112563f4bccfed432a50e74e3490dc7a7cdab13 1 parent c385f90
@jamesturk jamesturk authored
View
0  matchbox/webapi/__init__.py
No changes.
View
11 matchbox/webapi/manage.py
@@ -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)
View
8 matchbox/webapi/settings.py
@@ -0,0 +1,8 @@
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+TEMPLATE_LOADERS = ()
+MIDDLEWARE_CLASSES = ()
+ROOT_URLCONF = 'urls'
+INSTALLED_APPS = (
+ 'matchbox.webapi'
+)
View
8 matchbox/webapi/urls.py
@@ -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'),
+)
View
68 matchbox/webapi/views.py
@@ -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}))
Please sign in to comment.
Something went wrong with that request. Please try again.