def query(self, database=None, selectorField=None, value=None):

        if self.dbclient is None:
            self.dbclient.connect()

        db = self.dbclient[database]
        query = Query(db)
        if query is not None:
            with query.custom_result(selector={selectorField: value}) as res:
                if res is not None:
                    return res[0]
                else:
                    return None
        else:
            return None
 def test_custom_result_context_manager(self):
     """
     Test that custom_result yields a context manager and returns expected
     content
     """
     self.populate_db_with_documents(100)
     query = Query(
         self.db,
         selector={'_id': {'$lt': 'julia050'}},
         fields=['_id'],
         r=1
     )
     with query.custom_result(sort=[{'_id': 'desc'}]) as rslt:
         self.assertEqual(
             rslt[10:13],
             [{'_id': 'julia039'}, {'_id': 'julia038'}, {'_id': 'julia037'}]
         )
Beispiel #3
0
 def test_custom_result_context_manager(self):
     """
     Test that custom_result yields a context manager and returns expected
     content
     """
     self.populate_db_with_documents(100)
     query = Query(
         self.db,
         selector={'_id': {'$lt': 'julia050'}},
         fields=['_id'],
         r=1
     )
     with query.custom_result(sort=[{'_id': 'desc'}]) as rslt:
         self.assertEqual(
             rslt[10:13],
             [{'_id': 'julia039'}, {'_id': 'julia038'}, {'_id': 'julia037'}]
         )
Beispiel #4
0
def get_imageList():
    if db:
        docs = []
        query = Query(db, selector={'_id': {'$gt': 0}})
        i = 0
        max_iter = 8
        with query.custom_result() as rslt:
            start = random.randint(1, 32)
            print(start)
            rslt = rslt[start:]
            for doc in rslt:
                if i < max_iter:
                    file_name = doc['file_name']
                    file_id = doc['_id']
                    bytes_buffer = io.BytesIO()
                    res = cos.download_fileobj(
                        Bucket=credentials_cos['BUCKET'],
                        Key=file_name,
                        Fileobj=bytes_buffer)
                    byte_value = bytes_buffer.getvalue()
                    # str_value = byte_value.decode()

                    # print( str_value)
                    base64_file = base64.b64encode(byte_value)
                    base64_string = base64_file.decode(ENCODING)
                    data = {
                        'file_name': file_name,
                        'file_id': file_id,
                        'image_base64': base64_string
                    }
                    if file_name.find("classification") != -1:
                        docs.append(data)
                        i = i + 1
                else:
                    break
        random.shuffle(docs)
        return jsonify(docs)
    else:
        print('No database')
        return jsonify([])