Source

hydra / apps / externals / updates / bind.py

Full commit
def update_doc(doc, req):
    http = require('lib/http')
    if not doc:
        resp = http['404']('unable to bind document that does not exists', req)
        return [None, resp]

    content = require('lib/content')['provider']()
    data = content.decode(req)

    if not isinstance(data, dict):
        msg = 'request data should be object, got %r' % type(data)
        return [None, http['400'](msg, req)]

    missed_params = []
    if 'system' not in data:
        missed_params.append('system')
    if 'id' not in data:
        missed_params.append('id')
    if 'host' not in data:
        missed_params.append('host')

    if missed_params:
        missed = ', '.join(missed_params)
        msg = 'object missed keys: %s' % missed
        return [None, http['400'](msg, req)]

    require('lib/doc/version')['make_as_attachment'](doc)
    require('lib/doc/meta')['sign'](doc, req)

    key = data.pop('system')
    doc['.meta'].setdefault('externals', {})
    doc['.meta']['externals'].setdefault(key, {})
    doc['.meta']['externals'][key].update(data)

    return [doc, http['201']({'status': 'ok', 'id': doc['_id']}, req)]