Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a451532442394170 #22

Merged
merged 1 commit into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions openregistry/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gevent.monkey
gevent.monkey.patch_all()
import os
import simplejson
from logging import getLogger
from libnacl.sign import Signer, Verifier
from pyramid.authorization import ACLAuthorizationPolicy as AuthorizationPolicy
Expand All @@ -14,7 +15,7 @@

from openregistry.api.auth import AuthenticationPolicy, authenticated_role, check_accreditation
from openregistry.api.database import set_api_security
from openregistry.api.utils import forbidden, request_params, load_plugins
from openregistry.api.utils import forbidden, request_params, load_plugins, json_body, couchdb_json_decode
from openregistry.api.constants import ROUTE_PREFIX

LOGGER = getLogger("{}.init".format(__name__))
Expand All @@ -34,9 +35,11 @@ def main(global_config, **settings):
config.add_request_method(request_params, 'params', reify=True)
config.add_request_method(authenticated_role, reify=True)
config.add_request_method(check_accreditation)
config.add_renderer('prettyjson', JSON(indent=4))
config.add_renderer('jsonp', JSONP(param_name='opt_jsonp'))
config.add_renderer('prettyjsonp', JSONP(indent=4, param_name='opt_jsonp'))
config.add_request_method(json_body, 'json_body', reify=True)
config.add_renderer('json', JSON(serializer=simplejson.dumps))
config.add_renderer('prettyjson', JSON(indent=4, serializer=simplejson.dumps))
config.add_renderer('jsonp', JSONP(param_name='opt_jsonp', serializer=simplejson.dumps))
config.add_renderer('prettyjsonp', JSONP(indent=4, param_name='opt_jsonp', serializer=simplejson.dumps))

# search for plugins
plugins = settings.get('plugins') and settings['plugins'].split(',')
Expand All @@ -48,6 +51,7 @@ def main(global_config, **settings):
if aserver:
config.registry.admin_couchdb_server = aserver
config.registry.db = db
couchdb_json_decode()

# Document Service key
config.registry.docservice_url = settings.get('docservice_url')
Expand Down
22 changes: 21 additions & 1 deletion openregistry/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
from schematics.types import StringType
from jsonpatch import make_patch, apply_patch as _apply_patch

import simplejson
import couchdb
from pyramid.compat import text_
from decimal import Decimal

from openregistry.api.events import ErrorDesctiptorEvent
from openregistry.api.constants import LOGGER, TZ, ROUTE_PREFIX
from openregistry.api.interfaces import IContentConfigurator
Expand Down Expand Up @@ -490,4 +495,19 @@ def serialize_document_url(document):
if not document.hash:
path = [i for i in urlparse(url).path.split('/') if len(i) == 32 and not set(i).difference(hexdigits)]
return generate_docservice_url(request, doc_id, False, '{}/{}'.format(path[0], path[-1]))
return generate_docservice_url(request, doc_id, False)
return generate_docservice_url(request, doc_id, False)


def json_body(self):
return simplejson.loads(text_(self.body, self.charset), parse_float = Decimal)


def couchdb_json_decode():
my_encode = lambda obj, dumps=simplejson.dumps: dumps(obj, allow_nan=False, ensure_ascii=False)

def my_decode(string_):
if isinstance(string_, couchdb.util.btype):
string_ = string_.decode('utf-8')
return simplejson.loads(string_, parse_float=Decimal)

couchdb.json.use(decode=my_decode, encode=my_encode)