Esempio n. 1
0
def fetchTimes(dbname):
    with couchdb('admin', 'isthisroomoccupied', url='http://127.0.0.1:5984/') as client:
        db = client[dbname]
        docs = []
        for document in db:
            docs.append(document['date'])
        return docs
Esempio n. 2
0
def add_function(map_function_source, reduce_function_source, db_config,
                 language, benchmark):
    with couchdb(db_config['user'],
                 db_config['password'],
                 url=db_config['url']) as couch:
        db = couch[db_config['name']]
        design_doc = db.get_design_document(db_config['design_doc'])
        logger.info('Working with design document {} from DB {}.'.format(
            design_doc['_id'], db.database_name))
        design_doc['language'] = language
        view = design_doc.get_view(db_config['view'])
        if view is None:
            logger.info('View {} does not exist, creating a new one.'.format(
                db_config['view']))
            design_doc.add_view(db_config['view'], map_function_source,
                                reduce_function_source)
        else:
            logger.info('View {} exists, updating.'.format(view.view_name))
            design_doc.update_view(db_config['view'], map_function_source,
                                   reduce_function_source)
        logger.debug(design_doc.json())
        design_doc.save()
        logger.info('Design doc successfully saved.')
        logger.debug(design_doc.info())
        if benchmark:
            view = design_doc.get_view(db_config['view'])
            do_benchmark(couch, view)
Esempio n. 3
0
def check_user_email(user_email: str):
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        users = client['users']
        selector = {'email': {'$eq': user_email}}
        docs = users.get_query_result(selector)
        if len(docs[0]):
            return User_inDB(**docs[0][0])
Esempio n. 4
0
 def listDatabases(self):
     with couchdb(self._user, self._pass, url=self._url) as client:
         print(f"Documents Database")
         print(f"========= =====================")
         for db in client.all_dbs():
             print(f"{client[db].doc_count():9} {db}")
         print(f"========= =====================")
Esempio n. 5
0
def fetchAll(dbname):
    with couchdb('admin', 'isthisroomoccupied', url='http://127.0.0.1:5984/') as client:
        my_database = client[dbname]
        docs = json.loads('{"docs": []}')
        for document in my_database:
            docs['docs'].append(document)
        return docs
Esempio n. 6
0
 def getDatabaseDesigns(self, dbname):
     with couchdb(self._user, self._pass, url=self._url) as client:
         db = client[dbname]
         docs = db.design_documents()
         for item in docs:
             doc = item['doc']
             for k, v in doc.items():
                 print(f"{k}\n  {v}")
Esempio n. 7
0
def update_user(user_id: str, update_dict: dict):
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        users = client['users']
        user = users[user_id]
        for key, value in update_dict.items():
            user[key] = value
            user.save()
        return user
Esempio n. 8
0
 def __init__(self, password, user='******', url='http://localhost:5984'):
     self._user = user
     self._pass = password
     self._url = url
     self._user_db = "_users"
     self._metadata = {}
     with couchdb(self._user, self._pass, url=self._url) as client:
         self._metadata = client.metadata()
Esempio n. 9
0
def update_site(site_id: str, update_dict: dict):
    with couchdb(USER_S, PASSWORD_S, url=COUCHDB_URL_S) as client:
        sites = client['sites']
        site = sites[site_id]
        for key, value in update_dict.items():
            site[key] = value
            site.save()
        return site
Esempio n. 10
0
def create_site(site: Site_in, slug: str, owner: str):
    site_dict = site.dict()
    site_dict.update([('_id', slug), ('owner_id', owner),
                      ('created_on_utc',
                       datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'))])
    with couchdb(USER_S, PASSWORD_S, url=COUCHDB_URL_S) as client:
        sites = client['sites']
        new_site = sites.create_document(site_dict)
        return new_site
Esempio n. 11
0
 def test_couchdb_context_helper(self):
     """
     Test that the couchdb context helper works as expected.
     """
     try:
         with couchdb(self.user, self.pwd, url=self.url) as c:
             self.assertIsInstance(c, CouchDB)
             self.assertIsInstance(c.r_session, requests.Session)
     except Exception as err:
         self.fail('Exception {0} was raised.'.format(str(err)))
Esempio n. 12
0
 def test_couchdb_context_helper(self):
     """
     Test that the couchdb context helper works as expected.
     """
     try:
         with couchdb(self.user, self.pwd, url=self.url) as c:
             self.assertIsInstance(c, CouchDB)
             self.assertIsInstance(c.r_session, requests.Session)
     except Exception as err:
         self.fail('Exception {0} was raised.'.format(str(err)))
Esempio n. 13
0
def create_user(user: User_in):
    user_dict = user.dict()
    hash_password = get_password_hash(user_dict['password'])
    user_dict.pop('password')
    user_dict.update([('password_hash', hash_password),
                      ('registered_on_utc',
                       datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')),
                      ('role', 'standard')])
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        users = client['users']
        new_user = users.create_document(user_dict)
        return new_user
Esempio n. 14
0
 def getDatabaseUsers(self, dbname):
     with couchdb(self._user, self._pass, url=self._url) as client:
         db = client[dbname]
         print(f"Admin party:\n{db.admin_party}")
         creds = db.creds
         print("Database credentials document:")
         pprint(creds)
         print("Security documents:")
         counter = 0
         with cloudant.security_document.SecurityDocument(db) as secdoc:
             print(f"[{counter}] {secdoc.json()}")
             counter += 1
    def test_end_to_end(self):
        """
        End to end database and document crud tests

        """
        with couchdb(self.user, self.passwd, url='http://127.0.0.1:5984') as c:
            session = c.session()
            self.assertEqual(session['userCtx']['name'], self.user)

            db = c.create_database(self.dbname)

            try:
                self.assertTrue(self.dbname in c)
                self.assertTrue(db.exists())

                # creating docs
                doc1 = db.new_document()
                doc2 = db.create_document({'_id': 'womp', 
                    "testing": "document2"})
                doc3 = db.create_document({"testing": "document3"})

                self.assertTrue('_id' in doc1)
                self.assertTrue('_rev' in doc1)
                self.assertTrue('_id' in doc2)
                self.assertTrue('_rev' in doc2)
                self.assertTrue('_id' in doc3)
                self.assertTrue('_rev' in doc3)

                # verifying access via dict api
                self.assertTrue(doc1['_id'] in db)
                self.assertTrue(doc2['_id'] in db)
                self.assertTrue(doc3['_id'] in db)

                self.assertTrue(db[doc1['_id']] == doc1)
                self.assertTrue(db[doc2['_id']] == doc2)
                self.assertTrue(db[doc3['_id']] == doc3)
                # test working context for updating docs
                with doc2 as working_doc:
                    working_doc['field1'] = [1, 2, 3]
                    working_doc['field2'] = {'a': 'b'}

                self.assertEqual(
                    c[self.dbname]['womp']['field2'],
                    {'a': 'b'}
                )

            finally:
                # remove test database
                c.delete_database(self.dbname)
Esempio n. 16
0
    def test_end_to_end(self):
        """
        End to end database and document crud tests

        """
        with couchdb(self.user, self.passwd, url='http://127.0.0.1:5984') as c:
            session = c.session()
            self.assertEqual(session['userCtx']['name'], self.user)

            db = c.create_database(self.dbname)

            try:
                self.assertIn(self.dbname, c)
                self.assertTrue(db.exists())

                # creating docs
                doc1 = db.new_document()
                doc2 = db.create_document({'_id': 'womp', 
                    "testing": "document2"})
                doc3 = db.create_document({"testing": "document3"})

                self.assertIn('_id', doc1)
                self.assertIn('_rev', doc1)
                self.assertIn('_id', doc2)
                self.assertIn('_rev', doc2)
                self.assertIn('_id', doc3)
                self.assertIn('_rev', doc3)

                # verifying access via dict api
                self.assertIn(doc1['_id'], db)
                self.assertIn(doc2['_id'], db)
                self.assertIn(doc3['_id'], db)

                self.assertEqual(db[doc1['_id']], doc1)
                self.assertEqual(db[doc2['_id']], doc2)
                self.assertEqual(db[doc3['_id']], doc3)
                # test working context for updating docs
                with doc2 as working_doc:
                    working_doc['field1'] = [1, 2, 3]
                    working_doc['field2'] = {'a': 'b'}

                self.assertEqual(
                    c[self.dbname]['womp']['field2'],
                    {'a': 'b'}
                )

            finally:
                # remove test database
                c.delete_database(self.dbname)
Esempio n. 17
0
    def listUsers(self):
        """
        List all users known to CouchDB instance.

        Returns:

        """
        with couchdb(self._user, self._pass, url=self._url) as client:
            db = client["_users"]
            alldocs = db.all_docs(include_docs=True)
            for doc in alldocs['rows']:
                if doc['doc']['_id'].startswith("org.couchdb.user"):
                    print(f"{doc['doc']['_id']}")
                    print(f"  username: {doc['doc']['name']}")
                    print(f"  roles: {','.join(doc['doc']['roles'])}")
Esempio n. 18
0
    def compactDatabase(self, dbname):
        """
        https://docs.couchdb.org/en/stable/maintenance/compaction.html

        Args:
            dbname:

        Returns:

        """
        with couchdb(self._user, self._pass, url=self._url) as client:
            url = "/".join((self._url, dbname, "_compact"))
            logging.debug("Compacting %s", dbname)
            response = client.r_session.post(
                url, headers={'Content-Type': 'application/json'})
            logging.debug(response)
            logging.debug(response.text)
Esempio n. 19
0
def import_batch(docs, db_config):
    """Bulk import a batch of ElasticSearch-like documents into CouchDB"""
    t1 = process_time()
    docs_objects = []
    for d in docs:
        if d:
            doc_object = json.loads(d)
            source = doc_object['_source']['nacp_orig']
            source.update({'_id': doc_object['_id']})
            docs_objects.append(preprocess_doc(source))

    with couchdb(db_config['user'],
                 db_config['password'],
                 url=db_config['url']) as couch:
        db = couch[db_config['name']]
        db.bulk_docs(docs_objects)
    t2 = process_time()
    return len(docs_objects), t2 - t1
Esempio n. 20
0
    def addUser(self, username, password, roles: []):
        """
        Adds a user to the CouchDB instance. A user can then be added to a database.
        Args:
            username:
            password:
            roles:

        Returns:

        """
        credentials = {
            "_id": f"org.couchdb.user:{username}",
            "name": username,
            "type": "user",
            "roles": roles,
            "password": password
        }
        with couchdb(self._user, self._pass, url=self._url) as client:
            user_db = client["_users"]
            user_db.create_document(credentials, throw_on_exists=True)
Esempio n. 21
0
def del_site(site_id: str):
    with couchdb(USER_S, PASSWORD_S, url=COUCHDB_URL_S) as client:
        sites = client['sites']
        site = sites[site_id]
        site.delete()
Esempio n. 22
0
def get_site(site_id: str):
    with couchdb(USER_S, PASSWORD_S, url=COUCHDB_URL_S) as client:
        sites = client['sites']
        if (site_id in sites):
            return sites[site_id]
Esempio n. 23
0
def get_sites(skip: int = 0, limit: int = 100):
    with couchdb(USER_S, PASSWORD_S, url=COUCHDB_URL_S) as client:
        sitesdb = client['sites']
        sites = [site for site in sitesdb]
    return sites
Esempio n. 24
0
 def __init__(db_user, db_pass, db_url, db_name, id_pattern=None):
     self.db_client_context = cloudant.couchdb(db_user, db_pass, url=db_url)
     self.id_pattern = id_pattern
     self.db_name = db_name
Esempio n. 25
0
def couchdb_admin_session():
    return couchdb(settings.COUCH_DB_ADMIN_USER,
                   settings.COUCH_DB_ADMIN_PWD,
                   url=settings.COUCH_DB_URL)
Esempio n. 26
0
def get_local_db():
    with couchdb(settings.COUCH_DB_USER,
                 settings.COUCH_DB_PWD,
                 url=settings.COUCH_DB_URL) as session:
        db = session["tellme"]
        yield db
Esempio n. 27
0
                        '--purge',
                        help='Purge the DB before importing',
                        action='store_true',
                        default=False)
    args = parser.parse_args()

    db_config = {
        'user': args.username,
        'password': args.password,
        'url': args.endpoint,
        'name': args.dbname
    }

    # Ensure our DB exists before actually importing
    with couchdb(db_config['user'],
                 db_config['password'],
                 url=db_config['url']) as couch:
        try:
            db = couch[args.dbname]
            if args.purge:
                db.delete()
                logger.info('Database {} purged.'.format(args.dbname))
        except KeyError:
            pass
        db = couch.create_database(args.dbname, throw_on_exists=False)
        if db.exists():
            logger.info('Database {} created or already exists'.format(
                args.dbname))

    import_all(args.docs_file, db_config, args.concurrency, args.chunks)
Esempio n. 28
0
def get_users(skip: int = 0, limit: int = 100):
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        usersdb = client['users']
        users = [user for user in usersdb]
    return users
Esempio n. 29
0
def get_user(user_id: str):
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        users = client['users']
        if (user_id in users):
            return users[user_id]
Esempio n. 30
0
def openCouchSession():
    with couchdb('admin', 'isthisroomoccupied', url='http://127.0.0.1:5984/') as client:
    # Context handles connect() and disconnect() for you.
    # Perform library operations within this context.  Such as:
        print(client.all_dbs()) 
Esempio n. 31
0
def del_user(user_id: str):
    with couchdb(USER_U, PASSWORD_U, url=COUCHDB_URL_U) as client:
        users = client['users']
        user = users[user_id]
        user.delete()
Esempio n. 32
0
database = sys.argv[3]

host = "http://localhost:5984"

#view document
_doc = {
    "_id": "_design/gene_target",
    "views": {
        "counts": {
            "map": "function(doc) { emit(doc.primary_target,1); }",
            "reduce": "function(k,v) {return sum(v);}",
        }
    }
}

with couchdb(username, password, url=host) as client:
    db = client[database]
    view = Document(db, "_design/gene_target")
    if view.exists():
        print "Exists"
    else:
        try:
            view = db.create_document(_doc)
        except:
            print "Error"
    result = db.get_view_result("_design/most_severe_consequences",
                                "counts",
                                group=True)
    for item in result:
        print res