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)
def read(poll_id): if request.method == 'GET': doc = Document(couch, poll_id) doc.fetch() return doc.json() if request.method == 'POST': data = request.get_json() data['pollID'] = poll_id data['remote_addr'] = request.remote_addr data['user_agent'] = request.headers.get('User-Agent') doc = couch.create_document(data) return doc['_id'], 201
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, }
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, }