async def get_user_no_read(id): try: ch_feed = await get_changes(id) except HTTPError: return None try: if '_deleted' in ch_feed['doc'] and ch_feed['doc']['_deleted']: return None doc = Document(db, ch_feed['doc']['_id']) doc.update(ch_feed['doc']) return doc except: return None
def test_update_doc(db, _id, doc_updates): """Test document update by Id.""" orig_doc = Document(db, _id) if not orig_doc.exists(): raise ValueError(f'Document with id {_id} not found') try: # with Document(db, _id, decoder=json_decoder) as orig_doc: with Document(db, _id) as orig_doc: orig_doc.update(doc_updates) print(f'SUCCESS UPDATE: Document with id {_id}: {orig_doc}') except Exception as e: print(f'FAILED UPDATE: {e}') raise
def post(self): body = load_body(scrap_schema) db = get_scraps_db() doc = Document(db) data = scrap_schema.dump(body).data body = data.pop('body') content_type = data.pop('content_type') doc.update(**data) doc.create() doc.put_attachment('body', content_type, body) return doc
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 as_document(self, item_dict): document = Document(self.db, item_dict["_id"]) document.update(item_dict) return document
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, }