-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |