This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
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
Szymon Guz
committed
Mar 19, 2013
1 parent
589d019
commit 692f034
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
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
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,60 @@ | ||
import hashlib | ||
from amara.thirdparty import json | ||
from amara.lib.iri import is_absolute | ||
from akara.services import simple_service | ||
from akara.util import copy_headers_to_dict | ||
from akara import request, response, logger | ||
from dplaingestion.selector import getprop, setprop, exists | ||
|
||
COUCH_ID_BUILDER = lambda src, lname: "--".join((src,lname)) | ||
COUCH_REC_ID_BUILDER = lambda src, id_handle: COUCH_ID_BUILDER(src,id_handle.strip().replace(" ","__")) | ||
|
||
@simple_service('POST', 'http://purl.org/la/dp/edan_select_id', 'edan_select_id', 'application/json') | ||
def selid(body,ctype,prop='descriptiveNonRepeating/record_link', alternative_prop='descriptiveNonRepeating/record_ID'): | ||
''' | ||
Service that accepts a JSON document and adds or sets the "id" property to the | ||
value of the property named by the "prop" paramater | ||
''' | ||
tmpl="http://collections.si.edu/search/results.htm?q=record_ID%%3A%s&repo=DPLA" | ||
|
||
if not prop: | ||
response.code = 500 | ||
response.add_header('content-type','text/plain') | ||
return "No id property has been selected" | ||
|
||
try : | ||
data = json.loads(body) | ||
except: | ||
response.code = 500 | ||
response.add_header('content-type','text/plain') | ||
return "Unable to parse body as JSON" | ||
|
||
request_headers = copy_headers_to_dict(request.environ) | ||
source_name = request_headers.get('Source') | ||
|
||
id = None | ||
|
||
if exists(data, prop) or exists(data, alternative_prop): | ||
v = getprop(data,prop, True) | ||
if not v: | ||
v = getprop(data, alternative_prop) | ||
v = tmpl % v | ||
if isinstance(v,basestring): | ||
id = v | ||
else: | ||
if v: | ||
for h in v: | ||
if is_absolute(h): | ||
id = h | ||
if not id: | ||
id = v[0] | ||
|
||
if not id: | ||
response.code = 500 | ||
response.add_header('content-type','text/plain') | ||
return "No id property was found" | ||
|
||
data[u'_id'] = COUCH_REC_ID_BUILDER(source_name, id) | ||
data[u'id'] = hashlib.md5(data[u'_id']).hexdigest() | ||
|
||
return json.dumps(data) |