本文整理汇总了Python中cloudant.document.Document.json方法的典型用法代码示例。如果您正苦于以下问题:Python Document.json方法的具体用法?Python Document.json怎么用?Python Document.json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cloudant.document.Document
的用法示例。
在下文中一共展示了Document.json方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_retrieve_document_json
# 需要导入模块: from cloudant.document import Document [as 别名]
# 或者: from cloudant.document.Document import json [as 别名]
def test_retrieve_document_json(self):
"""
Test the document dictionary renders as json appropriately
"""
doc = Document(self.db)
doc['_id'] = 'julia006'
doc['name'] = 'julia'
doc['age'] = 6
doc_as_json = doc.json()
self.assertIsInstance(doc_as_json, str)
self.assertEqual(json.loads(doc_as_json), doc)
示例2: save_doc
# 需要导入模块: from cloudant.document import Document [as 别名]
# 或者: from cloudant.document.Document import json [as 别名]
def save_doc(self, doc, encode_attachments=True, force_update=False,
**params):
""" Save a document. It will use the `_id` member of the document
or request a new uuid from CouchDB. IDs are attached to
documents on the client side because POST has the curious property of
being automatically retried by proxies in the event of network
segmentation and lost responses. (Idee from `Couchrest <http://github.com/jchris/couchrest/>`)
@param doc: dict. doc is updated
with doc '_id' and '_rev' properties returned
by CouchDB server when you save.
@param force_update: boolean, if there is conlict, try to update
with latest revision
@param params, list of optionnal params, like batch="ok"
@return res: result of save. doc is updated in the mean time
"""
if doc is None:
doc1 = {}
else:
doc1, schema = _maybe_serialize(doc)
if '_attachments' in doc1 and encode_attachments:
doc1['_attachments'] = resource.encode_attachments(doc['_attachments'])
if '_id' in doc1:
docid = doc1['_id'] if six.PY3 else doc1['_id'].encode('utf-8')
couch_doc = Document(self.cloudant_database, docid)
couch_doc.update(doc1)
try:
# Copied from Document.save to ensure that a deleted doc cannot be saved.
headers = {}
headers.setdefault('Content-Type', 'application/json')
put_resp = couch_doc.r_session.put(
couch_doc.document_url,
data=couch_doc.json(),
headers=headers
)
put_resp.raise_for_status()
data = put_resp.json()
super(Document, couch_doc).__setitem__('_rev', data['rev'])
except HTTPError as e:
if e.response.status_code != 409:
raise
if force_update:
couch_doc['_rev'] = self.get_rev(docid)
couch_doc.save()
else:
raise ResourceConflict
res = couch_doc
else:
res = self.cloudant_database.create_document(doc1)
if 'batch' in params and ('id' in res or '_id' in res):
doc1.update({ '_id': res.get('_id')})
else:
doc1.update({'_id': res.get('_id'), '_rev': res.get('_rev')})
if schema:
for key, value in six.iteritems(doc.__class__.wrap(doc1)):
doc[key] = value
else:
doc.update(doc1)
return {
'id': res['_id'],
'rev': res['_rev'],
'ok': True,
}