Beispiel #1
0
 def __contains__(self, id):
     try:
         _, headers, _ = _doc_resource(self.resource, id).head()
         self.cc(headers)
         return True
     except http.ResourceNotFound:
         return False
Beispiel #2
0
 def get(self, id, default=None, **options):
     try:
         _, headers, data = _doc_resource(self.resource, id).get_json(**options)
         self.cc(headers)
     except http.ResourceNotFound:
         return default
     if hasattr(data, 'items'):
         return Document(data)
     else:
         return data
Beispiel #3
0
 def get_attachment(self, id_or_doc, filename, default=None):
     if isinstance(id_or_doc, util.strbase):
         id = id_or_doc
     else:
         id = id_or_doc['_id']
     try:
         _, headers, data = _doc_resource(self.resource, id).get(filename)
         self.cc(headers)
         return data
     except http.ResourceNotFound:
         return default
Beispiel #4
0
    def put_attachment(self, doc, content, filename=None, content_type=None):
        if filename is None:
            if hasattr(content, 'name'):
                filename = os.path.basename(content.name)
            else:
                raise ValueError('no filename specified for attachment')
        if content_type is None:
            content_type = ';'.join(
                filter(None, mimetypes.guess_type(filename))
            )

        resource = _doc_resource(self.resource, doc['_id'])
        status, headers, data = resource.put_json(filename, body=content, headers={
            'Content-Type': content_type
        }, rev=doc['_rev'])
        self.cc(headers)
        doc['_rev'] = data['rev']
Beispiel #5
0
 def delete_attachment(self, doc, filename):
     resource = _doc_resource(self.resource, doc['_id'])
     _, headers, data = resource.delete_json(filename, rev=doc['_rev'])
     self.cc(headers)
     doc['_rev'] = data['rev']
Beispiel #6
0
 def delete(self, doc):
     if doc['_id'] is None:
         raise ValueError('document ID cannot be None')
     _, headers, _ = _doc_resource(self.resource, doc['_id']).delete_json(rev=doc['_rev'])
     self.cc(headers)
Beispiel #7
0
 def __setitem__(self, id, content):
     resource = _doc_resource(self.resource, id)
     status, headers, data = resource.put_json(body=content)
     self.cc(headers)
     content.update({'_id': data['id'], '_rev': data['rev']})
Beispiel #8
0
 def __getitem__(self, id):
     _, headers, data = _doc_resource(self.resource, id).get_json()
     self.cc(headers)
     return Document(data)
Beispiel #9
0
 def __delitem__(self, id):
     resource = _doc_resource(self.resource, id)
     status, headers, data = resource.head()
     self.cc(headers)
     resource.delete_json(rev=headers['etag'].strip('"'))