Example #1
0
def main():
    with open('dbuploadcred.json') as f:
        creds = json.load(f)

    serviceUsername = creds['username']
    servicePassword = creds['password']
    serviceURL = creds['url']

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    databaseName = "population"

    popDatabase = client.create_database(databaseName)

    if popDatabase.exists():
        print("'{0}' successfully created.\n".format(databaseName))

    # Retrieve the fields in each row.
    for key in population_dict:
        city_name = key
        popSize = population_dict[key]

        #  Create a JSON document that represents all the data in the row.
        jsonDocument = {
            "cityField": city_name,
            "populationField": popSize,
        }

        # Create a document by using the Database API.
        newDocument = popDatabase.create_document(jsonDocument)

        # Check that the documents exist in the database.
        if newDocument.exists():
            print("Document successfully created.")
Example #2
0
def get_user(name):
    if name not in user_dictionary.keys(
    ):  # we need to handle the creation of a new user
        user = User(my_name=name)
        user_dictionary[name] = user
        user.save_me_to_db()
    else:
        myclient = Cloudant(serviceUsername,
                            servicePassword,
                            url=serviceURL,
                            adapter=httpAdapter)
        myclient.connect()
        users_db = myclient.get(user_database_name, remote=True)
        #if users_db is not None:
        users_returned = QueryResult(
            Query(users_db, selector={'_id': {
                '$eq': name
            }}))
        if users_returned is not None:
            list = users_returned.all()
            for u in list:
                t1 = u['_id']
                t2 = str(u['value'])
                # print(t2)
                dec = jsonpickle.decode(t2)
                user_dictionary[t1] = dec
        myclient.disconnect()
    return user_dictionary[name]
def the_area_graph_routing():
    x=[]
    y=[]
    dict_val={}
    p=figure(title="X-axis Gravity Sensor values v/s time",x_axis_label='time',y_axis_label='gravity_x')
    client=Cloudant("<user_id>","<password>",url="<db_url>")
    client.connect()
    myDatabase = client['sample_db']
    result_collection = Result(myDatabase.all_docs,include_docs=True)
    for i in result_collection:
        x.append(i['doc']['payload']['time'])
        y.append(i['doc']['payload']['gravity_x'])
        dict_val[i['doc']['payload']['time']]=i['doc']['payload']['gravity_x']
    x.sort()
    wx=x+x[::-1]
    print(wx)
    y.clear()
    for val in x:
        y.append(dict_val[val])
    print(x)
    zeros=[0 for i in range(len(y))]
    wy=y+zeros
    print(y)
    #p.line(x,y,line_width=2)
    p.patch(wx,wy,fill_alpha=1,line_width=2)
    script, div = components(p)
    return render_template("mod_chart.html",div=div, script=script)
Example #4
0
def getCoursesByAttribute(attribute):
    client = Cloudant(user_name, password, url=url)
    client.connect()
    databaseName = "catalog"
    myDatabase = client[databaseName]
    if (myDatabase.exists()):
        print("Successfully created a database {}".format(databaseName))

    result_collection = list(Result(myDatabase.all_docs, include_docs=True))
    #print ("Retrieved full document:\n{0}\n".format(result_collection[0]))
    end_point = '{0}/{1}'.format(url, databaseName + "/_all_docs")
    params = {'include_docs': 'true'}
    response = client.r_session.get(end_point, params=params)
    resultz = []
    count = 0
    for i in range(0, len(result_collection)):
        tmp = result_collection[i]['doc']
        if ('keywords' in tmp):
            dct = tmp['keywords']
            for entry in dct:
                if (attribute.lower() in entry["text"].lower()):
                    resultz.append(tmp)
                    count += 1
    client.disconnect()
    return resultz
Example #5
0
def main(dict):

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Creating data that will be sent back to client
    data = {}
    data["decks"] = []
    for deck in deckfile["decks"]:
        deckdata = {}
        deckdata["name"] = deck["deckname"]
        deckdata["value"] = deck["value"]
        deckdata["card_amount"] = deck["card_amount"]
        data["decks"].append(deckdata)
    data["tag"] = "get_decks"
    data["message"] = "Decks are presented in Slack by MTG Deck Bot."

    client.disconnect()

    return data
Example #6
0
def getTimeLast():
    client = Cloudant(
        "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix",
        "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff",
        url=
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com"
    )
    client.connect()

    end_point = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "coolstuff" + "/_all_docs?")
    end_point_status = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "status" + "/_all_docs?")

    params = {'include_docs': 'true'}
    response = client.r_session.get(end_point, params=params)
    response_status = client.r_session.get(end_point_status, params=params)
    i = 1
    table = []
    while (i < 7):

        table.append(response.json()['rows'][-i]['doc']['current'])
        # table[i] = (response.json
        # table.insert(i,response.json()['rows'][i]['doc']['current'])
        i = i + 1
    client.disconnect()

    return table
Example #7
0
def cloudant_client_from_url(url):
    from .client import Cloudant
    cant = urlparse(url)
    usrn, pw, host = cant.username, cant.password, cant.scheme + '://' + cant.netloc
    cloudant_client = Cloudant(usrn, pw, host)
    cloudant_client.connect()
    return cloudant_client
Example #8
0
def get_from_db_by_params(user_name, region_name, place_name, db_name, object_example, asdocs=False, dates=None, word_list=None, logic='or_logic', exact=False):
    print("get_from_db_by_params")
    print(user_name + ";" + region_name + ";" + place_name + ";" + db_name + ";" + str(type(object_example)) + ";" +
                                                                                       str(asdocs))
    get_db_client = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=Replay429Adapter(retries=10, initialBackoff=0.01))
    try:
        our_list = []
        get_db_client.connect()
        db = get_db_client.get(db_name, remote=True)
        print("get_from_db_by_params logic is:" + logic)
        selector = generate_single_place_selector(user_name, region_name, place_name, dates, word_list, logic, exact)
        print("the selector is: " + str(selector))
        query_result = QueryResult(Query(db, selector=selector))
        if asdocs is True:
            for doc in query_result:
                our_list.append(doc)
        else:
            for doc in query_result:
                our_list.append(object_example.build_from_document(doc))
    except CloudantException as exc:
        print("CloudantException in get_from_db_by_params")
        print(exc)
        return exc
    except Exception as exc:
        print("non CloudantException exception in get_from_db_by_params")
        print(exc)
        return exc
    finally:
        get_db_client.disconnect()
        print("get_from_db_by_params id: " + str(db_name) + " len: " + str(len(our_list)))
    return our_list
class CouchDB():
    def __init__(self, cfg):
        self._cfg = cfg
        self._client = Cloudant(self._cfg['user'],
                                self._cfg['password'],
                                url=self._cfg['host'])
        self._client.connect()
        self._db = self._get_db()

    def _get_db(self):
        databases = self._client.all_dbs()
        db_name = self._cfg[self._name]
        if not db_name in databases:
            self._client.create_database(db_name)
        return self._client[db_name]

    def update_document(self, document_id, attributes_dict):
        if attributes_dict is None:
            return False

        document = self._db[document_id]
        for key in attributes_dict.keys():
            document[key] = attributes_dict[key]
        document.save()
        return True

    def reconnect(self):
        try:
            self._client.disconnect()
            self._client.connect()
        except Exceptions as e:
            print('Eror: {}'.format(e))
Example #10
0
def getItems(dictUse):
    from cloudant.client import Cloudant
    from cloudant.error import CloudantException
    from cloudant.result import Result, ResultByKey
    import json
    returningList = []
    numberList = []

    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://c2c7ebdf-134b-4f53-9805-d37f520c6933-bluemix.cloudant.com"

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    #name storage_unit
    databaseName = "storage_unit"
    myDatabaseDemo = client.create_database(databaseName)
    result_collection = Result(myDatabaseDemo.all_docs, include_docs=True)
    for doc in result_collection:
        if(doc["doc"]["Blood Type"] == dictUse["Blood Type"] and (doc["doc"]["Location"]).lower() == dictUse["Location"].lower()):
            returningList.append(doc["doc"]["Name"])
            numberList.append(doc["doc"]["Number"])
                

    client.disconnect()
    returnString = ""
    for i in range (len(returningList)):
        if(i == len(returningList) - 1):
            returnString += returningList[i]
        else:
            returnString += returningList[i] + ', '
    return returnString, numberList
Example #11
0
def write_to_Error_log(text):
    user_data = get_user_data()
    msg = "ERROR - Date:" + str(RecDate()) + " AT TIME: " + str(
        RecTime()) + ": " + str(text) + str('\n')
    print("Writting to ERROR LOG file:", msg)
    # log_txt_file="/home/pi/hms/" + str(RecDate())+"-HMS-log.txt"
    # f=open(log_txt_file, 'a')
    # f.write(msg)
    # f.close()

    DATABASE_NAME = "hms_log"
    client = Cloudant(user_data['cloud_acct_username'],
                      user_data['cloud_acct_pword'],
                      url=user_data['cloud_act_url'])

    client.connect()

    my_database = client[DATABASE_NAME]

    json_document = {
        "d": dt.datetime.now().strftime("%m-%d-%Y"),
        "t": dt.datetime.now().strftime("%H:%M:%S"),
        "m": msg
    }
    try:
        new_document = my_database.create_document(json_document)
    except:
        time.sleep(30)
        try:
            new_document = my_database.create_document(json_document)
        except:
            return
    client.disconnect()
def main(dict):
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@be4be6a3-1291-4fff-b5a5-ba96ec59cc51-bluemix.cloudant.com"
    databaseName = "wiam_db"

    #print(dict)

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    myDB = client[databaseName]

    end_point = '{0}/{1}'.format(serviceURL, 'wiam_db/_find')
    headers = {'Content-Type': 'application/json'}
    params = {'include_docs': 'true'}
    data = '{"selector": {"wr_id":"' + str(dict['workid']) + '"}}'
    # Issue the request
    response = client.r_session.post(end_point,
                                     headers=headers,
                                     params=params,
                                     data=data)

    #check to see any docs have returned
    if (len(response.json()['docs']) == 0):
        output_val = {'status': 'NODOCS'}
    else:
        output_val = {'status': response.json()['docs'][0]['status']}

    #print(output_val)

    # Display the response content
    return (output_val)
Example #13
0
def main(dict):

    # Checking if new deck has right values in it
    if not "deckname" in dict:
        return {"error_message": "A deck must have a name value ('deckname')"}
    if not "cardurls" in dict:
        return {
            "error_message":
            "A deck must have atleast one card (in 'cardurls'). A card must have values 'url' and 'amount'."
        }

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Checking if deckname is already in use
    if not deck_name_valid(dict["deckname"], deckfile):
        client.disconnect()
        return {"error_message": "Deck name is already in use."}

    # Getting value for the new deck
    currentval = 0.0
    amount_of_cards = 0
    for url in dict["cardurls"]:
        currentval += get_price(url["url"]) * url["amount"]
        amount_of_cards += url["amount"]
    dict["value"] = round(currentval, 2)
    dict["card_amount"] = amount_of_cards

    # Removing unnecassary stuff from dict
    if "__ow_headers" in dict:
        dict.pop("__ow_headers")
    if "__ow_method" in dict:
        dict.pop("__ow_method")
    if "__ow_path" in dict:
        dict.pop("__ow_path")

    # Adding new deck to the collection
    deckfile["decks"].append(dict)
    deckfile.save()

    # Disconnecting from client
    client.disconnect()

    # Informing user that adding succeeded
    return {
        'message': "A new deck was added succesfully!",
        'deckname': dict["deckname"],
        'amount': amount_of_cards,
        'value': dict["value"],
        'tag': "add"
    }
Example #14
0
def edit_cloudant_system_status_doc(s, v):
    # USERNAME = cloud_acct_username
    # PASSWORD = cloud_acct_pword
    # URL = cloud_act_url
    user_data = get_user_data()
    client = Cloudant(user_data['cloud_acct_username'],
                      user_data['cloud_acct_pword'],
                      url=user_data['cloud_act_url'])
    #client.connect()

    try:
        client.connect()
    except:
        write_to_Error_log(
            "error connecting to cloudant System Status database, will sleep for 30 seconds and then try again"
        )
        time.sleep(30)
        try:
            client.connect()
        except:
            write_to_Error_log(
                "2nd error in a row connecting to cloudant System Status database, will sleep for 5 minute and then skip"
            )
            time.sleep(300)
            return
    my_database = client["system_status"]
    my_document = my_database[s]
    my_document['v'] = v

    my_document['t'] = dt.datetime.now().strftime("%H:%M:%S")
    my_document['d'] = dt.datetime.now().strftime("%m-%d-%Y")
    my_document.save()
    print('\n\n************** system table updated************\n\n')
Example #15
0
def getLastSavedDbState():
    from cloudant.client import Cloudant
    from cloudant.result import Result
    from cloudant.adapters import Replay429Adapter
    from credentials import getUserName, getPassword, getUrl

    #Establish a connection to the cloudant database by providing the credentials necessary
    client = Cloudant(getUserName(),
                      getPassword(),
                      url=getUrl(),
                      adapter=Replay429Adapter(retries=10,
                                               initialBackoff=0.01))
    client.connect()

    #Connect to the "news_articles" database remotely
    db = client.get("news_articles", remote=True)
    #Get all records from the database
    resultset = Result(db.all_docs, include_docs=True)

    news_articles_list = list()
    #Loop through the database records
    for result in resultset:
        #Add all article titles to a list.
        #The news article titles are unique and will be used to check if an article already exists in the database
        news_articles_list.append(result["doc"]["article_title"])
    client.disconnect()

    return news_articles_list
def save_data(Results):
    #check if we run on bluemix
    if 'VCAP_SERVICES' in os.environ:
        vcap_servicesData = json.loads(os.environ['VCAP_SERVICES'])
    else:
        print("On Local PC")
        json_file = open("static/vcap-local.json")
        s = json_file.read()
        vcap_servicesData = json.loads(s)
        vcap_servicesData = vcap_servicesData[u'services']

    # Connect To Cloudant DB
    cloudantNoSQLDBData = vcap_servicesData[u'cloudantNoSQLDB']
    credentials = cloudantNoSQLDBData[0]
    credentialsData = credentials[u'credentials']
    serviceUsername = credentialsData[u'username']
    servicePassword = credentialsData[u'password']
    serviceURL = credentialsData[u'url']

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    database = client['results']
    database.create_document({"data": Results, "Time-in": time.time()})
    client.disconnect()
    return 0
    def insertDbUrl(self, action, link, intent):
        if action == 'url':
            data = {
                'url': link,
                'bot': 'None',
                'intent': intent
            }
        elif action == 'bot':
            data = {
                'url': 'None',
                'bot': link,
                'intent': intent
            }
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        # Perform client tasks...
        session = client.session()

        actionsDB = client['actions']
        my_document = actionsDB.create_document(data)

        # Check that the document exists in the database
        if my_document.exists():
            print 'SUCCESS!!'
Example #18
0
def getDBDtls():
    if request.method == 'POST' and request.form['dbdtls'] == 'False':
        dbusername = request.form['username']
        dbpassword = request.form['password']
        dburl = request.form['url']
        global db_user
        global db_pass
        global db_url
        db_user = dbusername
        db_pass = dbpassword
        db_url = dburl
        client = Cloudant(db_user, db_pass, url=db_url)
        client.connect()
        try:
            myDatabase = client.create_database("user_db")
            global db_svc
            db_svc = {"dbsvc": myDatabase}
        except CloudantException as err:
            print err
            client = Cloudant(db_user, db_pass, url=db_url)
            client.connect()
            client.delete_database("user_db")
            myDatabase = client.create_database("user_db")
            global db_svc
            db_svc = {"dbsvc": myDatabase}
        return render_template('index.html', getUsrDtls=True)
Example #19
0
def main(url_to_check):
    # Use cloudant DB for save websites result.
    # Assumes that the database has been prepared in the past.
    client_cloudant = Cloudant(USERNAME_CLOUDANT,
                               PASSWORD_CLOUDANT,
                               url=URL_CLOUDANT)
    client_cloudant.connect()
    myDatabase = client_cloudant[DATABASE_NAME]
    if myDatabase.exists():
        print("'{0}' database is exist.\n".format(DATABASE_NAME))
    else:
        print("'{0}' database is not exist.\n".format(DATABASE_NAME))

    try:
        my_document = myDatabase[url_to_check["url"]]
        print("Url was exist in database '{0}' .\n".format(DATABASE_NAME))
        return {"website_is_ok": my_document["website_is_ok"]}
    except:
        pass

    if filter_using_nlu(
            url_to_check["url"], BLACK_LIST_CONCEPTS,
            BLACK_LIST_KEYWORDS) == False or filtering_with_visual_recognition(
                url_to_check["url"]) == False:
        myDatabase.create_document({
            "_id": url_to_check["url"],
            "website_is_ok": False
        })

    myDatabase.create_document({
        "_id": url_to_check["url"],
        "website_is_ok": True
    })
    return {"website_is_ok": True}
Example #20
0
def Retrieve_Result():
    #check if we run on bluemix
    if 'VCAP_SERVICES' in os.environ:
        vcap_servicesData = json.loads(os.environ['VCAP_SERVICES'])
    else:
        print("On Local PC")
        json_file = open("vcap-local.json")
        s = json_file.read()
        vcap_servicesData = json.loads(s)
        vcap_servicesData = vcap_servicesData[u'services']

    # Connect To Cloudant DB
    cloudantNoSQLDBData = vcap_servicesData[u'cloudantNoSQLDB']
    credentials = cloudantNoSQLDBData[0]
    credentialsData = credentials[u'credentials']
    serviceUsername = credentialsData[u'username']
    servicePassword = credentialsData[u'password']
    serviceURL = credentialsData[u'url']

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    database_json = client['results']

    result = {u"data": [], u"Time": []}

    for dat in database_json:
        result[u"data"].append(dat[u'data'])
        result[u"Time"].append(dat[u'Time-in'])

    client.disconnect()
    return json.dumps(result)
Example #21
0
def get_from_db(id_list, db_name, object_example=None):
        result_list = []
        client_get_from_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2)
        try:
            client_get_from_db.connect()
            db = client_get_from_db.get(db_name, remote=True)
            if db is None:
                print("error: no tweet db ready yet")
                return ""
            query_result = QueryResult(Query(db, selector={'_id': {'$in': id_list}}))
            index = 0
            for doc in query_result:
                if object_example is "asdocs":
                    result_list.append(doc)
                elif object_example is not None:
                    result_list.append(object_example.build_from_document(doc))
                    index = + 1
                else:
                    dec = jsonpickle.decode(str(doc['value']))
                    result_list.append(dec)
                    index =+ 1
            print("query_result len: " + str(index))
        except CloudantException as exc:
            print("CloudantException in get_from_db")
            print(exc)
            result_list = []
        except Exception as exc:
            print("non CloudantException exception in get_from_db")
            print(exc)
            result_list = []
        finally:
            client_get_from_db.disconnect()
            print("get_from_db id: " + str(db_name) + " len: " + str(len(result_list)))
        return result_list
    def getUrl(self, intent):
        action = {}
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        actionsDB = client['actions']
        result_collection = Result(actionsDB.all_docs, include_docs=True)

        for result in result_collection:
            print result
            if result['doc'] is not None:
                if result['doc']['intent'] == intent:
                    if 'url' in result['doc'] and 'bot' in result['doc']:
                        if result['doc']['url'] is None:
                            action['bot'] = result['doc']['bot']
                        else:
                            action['url'] = result['doc']['url']
                    break
        print "Action: " + str(action)

        # Disconnect from the server
        client.disconnect()

        return action
Example #23
0
def save_list_to_db(inlist, db_name, object_example=None):
    print("save_list_to_db:" + db_name + "list len is: " + str(len(inlist)))
    documents = []
    for obj in inlist:
        if object_example is not None:
            document = obj.build_my_document()
        else:
            val = jsonpickle.encode(obj)
            document = {
                '_id': str(obj.id),
                'value': val
            }
        documents.append(document)
    client_save_to_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2)
    try:
        client_save_to_db.connect()
        db = client_save_to_db.get(db_name, remote=True)
        db.bulk_docs(documents)
    except CloudantException as exc:
        print("CloudantException in save_list_to_db")
        print(exc)
        time.sleep(2)
        try:
            client_save_to_db.connect()
            db = client_save_to_db.get(db_name, remote=True)
            db.bulk_docs(documents)
        except Exception as exc:
            print("second exception after CloudantException exception in save_list_to_db")
            print(exc)
    except Exception as exc:
        print("non CloudantException exception in save_list_to_db")
        print(exc)
    finally:
        client_save_to_db.disconnect()
Example #24
0
def main(dict):
    
    # Checking if user has typed a deck that this function is trying to find
    if not "deckname" in dict:
        return { "error_message": "Type value to 'deckname' to fetch specific deck's information." }
    
    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    
    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")
    
    # Trying to find deck from database
    name_to_find = dict["deckname"]
    result = {}
    for deck in deckfile["decks"]:
        if name_to_find == deck["deckname"]:
            result = deck
            break
    else:
        return { "error_message": f"Couldn't find a deck named {name_to_find}." }
    
    client.disconnect()
    result["tag"] = "get_deck"
    result["message"] = "Deck's information is presented in Slack by MTG Deck Bot!"
    return result
Example #25
0
def makeroom():
    # 接続
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    myDatabase = client[databaseName]

    # 書き込み
    flag = True
    room_id = 0
    while flag:
        flag = False
        tmp = random.randrange(9999)
        print(tmp)
        for item in Result(myDatabase.all_docs, include_docs=True):
            if item["doc"]["room_id"] == tmp:
                flag = True
        if (flag == False):
            room_id = int('{0:04d}'.format(tmp))

    jsonDocument = {
        "room_id": room_id,
        "host_name": request.args.get('host_name'),
        "guest_name": None,
        "host_station": request.args.get('host_station'),
        "guest_station": None
    }
    newDocument = myDatabase.create_document(jsonDocument)
    if newDocument.exists():
        print("ルーム '{0}' が作成されました.".format(newDocument["room_id"]))

    output = {"room_id": newDocument["room_id"]}

    return jsonify(output)
def main(dict):

    serviceUsername = dict['CLOUDANT_ACCOUNT']
    servicePassword = dict['CLOUDANT_PASSWORD']
    serviceURL = dict['CLOUDANT_URL']

    # Use the IBM Cloudant library to create an IBM Cloudant client.
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)

    # Connect to the server
    client.connect()

    databaseName = dict['CLOUDANT_DBNAME']

    # Create an instance of the database.
    myDatabaseDemo = client.create_database(databaseName)

    # Check that the database now exists.
    if myDatabaseDemo.exists():
        print("'{0}' successfully created.\n".format(databaseName))

    service = AssistantV1(iam_apikey=dict['IAM_APIKEY'],
                          version=dict['WDC_VERSION'],
                          url=dict['WDC_URL'])

    response = service.get_workspace(workspace_id=dict['WDC_WORKSPACEID'],
                                     export='true').get_result()

    newDocument = myDatabaseDemo.create_document(response)

    return {'status': 200, 'message': 'Backup realizado com sucesso!'}
Example #27
0
def getCloudantData(id):
    USERNAME = os.environ['cloudant_username']
    PASSWORD = os.environ['cloudant_password']
    URL = os.environ['cloudant_url']
    APIKEY = os.environ['cloudant_apikey']
    DATABASENAME = os.environ['cloudant_databasename']

    # IBM Cloudant Legacy authentication
    client = Cloudant(USERNAME, PASSWORD, url=URL)
    client.connect()

    # IAM Authentication (uncomment if needed, and comment out IBM Cloudant Legacy authentication section above)
    # client = Cloudant.iam(USERNAME, APIKEY)
    # client.connect()

    myDatabase = client[DATABASENAME]
    # query = Query(myDatabase, skip=10, limit=100)
    # result = QueryResult(query, skip=10, limit=100)
    # if myDatabase.exists():
    #     print("'{0}' successfully created.\n".format(databaseName))

    query = Query(myDatabase, selector={"user_id": id})
    doc = query()['docs'][0]

    # for doc in query()['docs']:
    #     print(doc)

    # client.disconnect()
    return doc
Example #28
0
def update_all_to_db():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['purdue_courses']

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    cache = dict()
    for cache_type, cache in deep_caches.items():
        cache.update(cache)

    bulk_update(cache, courses_db)

    # Disconnect from the server
    client.disconnect()
Example #29
0
def main(args):
    # Connect Cloudant
    db_client = Cloudant(args["USERNAME"],
                         args["PASSWORD"],
                         url=args["URL"],
                         adapter=Replay429Adapter(retries=10,
                                                  initialBackoff=0.01))
    db_client.connect()
    freee_tokens = db_client['freee_tokens']
    doc_id = args['TOKEN_DOC_ID']

    with Document(freee_tokens, doc_id) as document:
        refresh_token = document['refresh_token']
        payload = {
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
            'client_id': args['CLIENT_ID'],
            'client_secret': args['CLIENT_SECRET'],
            'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob'
        }
        response = requests.post(
            'https://accounts.secure.freee.co.jp/public_api/token',
            data=payload).json()
        print(response)
        document['access_token'] = response['access_token']
        document['expires_in'] = response['expires_in']
        document['refresh_token'] = response['refresh_token']
        document['created_at'] = response['created_at']
    db_client.disconnect()
    return {'result': 'OK!'}
Example #30
0
def req_my_data():
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ba62d85b-fc08-4706-8a9e-ba91bfbf6174-bluemix.cloudant.com"
    my_cl_db_name = 'my_cl_db'

    client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    client.connect()  # Connect to the server
    my_cl_db_handler = cloudant.database.CloudantDatabase(
        client, my_cl_db_name)

    # Assignment Q1: Find all reviews from member A1004AX2J2HXGL.
    selector = {'member_id': {'$eq': 'A1004AX2J2HXGL'}}
    docs_collection = my_cl_db_handler.get_query_result(selector)
    for each_doc in docs_collection:
        print(each_doc)

    # Assignment Q2: Find the number of reviews by each member.
    resp = my_cl_db_handler.get_search_result(ddoc_id='py_ddoc',
                                              index_name='py_index',
                                              query='*:*',
                                              counts=["member_id"])
    print('Number of reviews by each member: ', resp['counts'])

    # Assignment Q3: Find the product ids of all products with at least one review rating less than 3.
    selector = {'rating': {'$lt': 3}}
    fields = ['product id']
    docs_collection = my_cl_db_handler.get_query_result(selector, fields)
    for each_doc in docs_collection:
        print(each_doc)

    client.disconnect()
Example #31
0
def main():
    client = Cloudant(config.username, config.password, account=config.account)

    client.connect()

    dbs = client.all_dbs()

    output = []
    for db in dbs:
    	print 'Retrieving stats for {0}...'.format(db)
        db = CloudantDatabase(client, db)
        print "db: " + json.dumps(db)
        output.append(db.doc_count())

	print json.dumps(output, indent=4)
	print json.dumps(sort(output), indent=4)

    client.disconnect()
    def getClassifierId(self):
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        classifierDB = client['classifierdb']
        result_collection = Result(classifierDB.all_docs, include_docs=True)

        for result in result_collection:
            print result['doc']
            if result['doc'] is not None:
                if 'status' in result['doc'] and result['doc']['status'] == 'A':
                    classifier = result['doc']['classifierId']
        print "Classifier: " + str(classifier)

        # Disconnect from the server
        client.disconnect()
        return classifier
class CloudantHelper:
    COUCHDB_USER = ""
    COUCHDB_PASSWORD = ""
    COUCHDB_URL = ""

    dbclient = None

    def __init__(self, userid, password, url):
        self.init(userid, password, url)
        pass

    def init(self, userid, password, url):
        self.COUCHDB_URL = url
        self.COUCHDB_USER = userid
        self.COUCHDB_PASSWORD = password

        print("Connecting to Cloudant..")
        self.dbclient = Cloudant(self.COUCHDB_USER, self.COUCHDB_PASSWORD, url=self.COUCHDB_URL)

        # Connect to the server
        self.dbclient.connect()
        print("Connected to Cloudant!")

    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 queryAll(self, database=None, field=None, value=None):
        resultsArray = []
        if self.dbclient is None:
            self.dbclient.connect()

        db = self.dbclient[database]
        result_collection = Result(db.all_docs, include_docs=True)

        count = 0
        for result in result_collection:
            if result['doc'] is not None:
                if field in result['doc'] and result['doc'][field] == value:
                    resultsArray.append(result['doc'])
                    count += 1
        return resultsArray

    def disconnect(self):
        # Disconnect from the server
        if self.dbclient is not None:
            self.dbclient.disconnect()
            print("Disconnected from Cloudant.")
Example #34
0
File: db.py Project: aqualove/api
def make_db_connection():
    services = current_app.config['cf_services']
    creds = services['cloudantNoSQLDB'][0]['credentials']
    client = Cloudant(creds['username'], creds['password'], url=creds['url'])
    client.connect()
    return client
Example #35
0
class CloudantDB(threading.Thread):

    def _readConfig(self):
        update = False

        if not os.path.isdir(self._homeDir):
            print "Creating homeDir"
            os.makedirs(self._homeDir)

        if os.path.isfile(self._configFileName):
            self._config.read(self._configFileName)
        else:
            print "Config file not found"
            update = True

        if not self._config.has_section('REDIS'):
            print "Adding Redis part"
            update = True
            self._config.add_section("REDIS")

        if not self._config.has_option("REDIS", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("REDIS", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("REDIS", "ServerPort"):
            print "No Server Port"
            update = True
            self._config.set("REDIS", "ServerPort", "6379")

        if not self._config.has_section('CLOUDANT'):
            print "Adding Cloudant part"
            update = True
            self._config.add_section("CLOUDANT")

        if not self._config.has_option("CLOUDANT", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("CLOUDANT", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("CLOUDANT", "Username"):
            print "No Username"
            update = True
            self._config.set("CLOUDANT", "Username", "Didditulle")

        if not self._config.has_option("CLOUDANT", "Password"):
            print "No Password"
            update = True
            self._config.set("CLOUDANT", "Password", "geheim")

        if update:
            with open(self._configFileName, 'w') as f:
                self._config.write(f)

    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self._homeDir        = os.path.expanduser("~/.sensomatic")
        self._configFileName = self._homeDir + '/config.ini'
        self._config         = ConfigParser.ConfigParser()
        self._readConfig()
        self._redis          = redis.StrictRedis(host=self._config.get("REDIS", "ServerAddress"), port=self._config.get("REDIS", "ServerPort"), db=0)
        self._cloudant       = Cloudant(self._config.get("CLOUDANT", "Username"), self._config.get("CLOUDANT", "Password"), url=self._config.get("CLOUDANT", "ServerAddress"))
        self.checkDB()

    def checkDB(self):
        self._cloudant.connect()
        if "usshorizon" in self._cloudant.all_dbs():
            self._database = self._cloudant['usshorizon']
            return True
        else:
            print "Create DB"
            self._database = self._cloudant.create_database('usshorizon')
            if self._database.exists():
                print "Success"
                return True
            else:
                print "Error"
                return False

    def getData(self):
        data = {'timestamp':time.time()}
        for key in self._redis.keys():
            k = key.split('/')
            l = len(k)
            w = data
            for i in range(l):
                if k[i] not in w:
                    w[k[i]] = {}
                w = w[k[i]]
                if i == l - 1:
                    w['value'] = self._redis.get(key)
        return data

    def run(self):
        while True:
            d = self.getData()
            # print d
            try:
                if self.checkDB():
                    self._database.create_document(d)
            except Exception as e:
                print e
                time.sleep(60)
            time.sleep(10)
f.close()
print cred


# データベース名の取得
f = open('database_name.json', 'r')
dbn = json.load(f)
f.close()
print dbn['name']

client = Cloudant(cred['credentials']['username'], 
                  cred['credentials']['password'], 
                  url=cred['credentials']['url'])

# Connect to the server
client.connect()

# DB選択
db = client[dbn['name']]

# CSVを読んでループを回す
fn = 'weather_code.csv';
reader = csv.reader(codecs.open(fn),delimiter=',',quoting=csv.QUOTE_NONE)
for id, e_description,j_description  in reader:
    print id, e_description,j_description
    data = {
        "_id": id,
        "e_description": e_description,
        "j_description": j_description
        }
    sendGridFrom = config['sendGrid']['from']
    sendGridSubject = config['sendGrid']['subject']


###########################################################
# define cloudant database in bluemix to hold daily results
###########################################################

if config['cloudant']['username'] == None:
    createDB = False
    queryDB = False
else:
    createDB = True
    queryDB = True
    cloudant = Cloudant(config['cloudant']['username'], config['cloudant']['password'], url=config['cloudant']['url'])
    cloudant.connect()
    vsistatsDb = cloudant["vsistats"]

######################################
## Flags to control script logic
######################################
creatTickets=True

######################################
# Enable Logging
######################################

central = pytz.timezone("US/Central")
today = central.localize(datetime.now())
logging.basicConfig( filename='events.log', format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p',level=logging.WARNING)
Example #38
0
from cloudant.client import Cloudant

cloud   = Cloudant("3a4d4cf0-aed2-4916-8413-fa0177d2129f-bluemix", "24b4187fbd39510e84cc2cf10184cebf97ea56b836aab8ce4590ffe6477ae925", url = "https://3a4d4cf0-aed2-4916-8413-fa0177d2129f-bluemix.cloudant.com")
cloud.connect()

db   = cloud['usshorizon']
worf = db['_design/ansiroom.json']
data = worf.get_view("bedplant")

for i in data.result:
    try:
        print "%d -> %d" % (int(i['key']), int(i['value']))
    except:
        pass
        #print i
class UnitTestDbBase(unittest.TestCase):
    """
    The base class for all unit tests targeting a database
    """

    @classmethod
    def setUpClass(cls):
        """
        If targeting CouchDB, Set up a CouchDB instance otherwise do nothing.
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            if os.environ.get('DB_URL') is None:
                os.environ['DB_URL'] = 'http://127.0.0.1:5984'

            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                if os.environ.get('DB_USER'):
                    del os.environ['DB_USER']
                if os.environ.get('DB_PASSWORD'):
                    del os.environ['DB_PASSWORD']
                return

            if os.environ.get('DB_USER') is None:
                os.environ['DB_USER_CREATED'] = '1'
                os.environ['DB_USER'] = '******'.format(
                    unicode_(uuid.uuid4())
                    )
                os.environ['DB_PASSWORD'] = '******'
                resp = requests.put(
                    '{0}/_config/admins/{1}'.format(
                        os.environ['DB_URL'],
                        os.environ['DB_USER']
                        ),
                    data='"{0}"'.format(os.environ['DB_PASSWORD'])
                    )
                resp.raise_for_status()

    @classmethod
    def tearDownClass(cls):
        """
        If necessary, clean up CouchDB instance once all tests are complete.
        """
        if (os.environ.get('RUN_CLOUDANT_TESTS') is None and
            os.environ.get('DB_USER_CREATED') is not None):
            resp = requests.delete(
                '{0}://{1}:{2}@{3}/_config/admins/{4}'.format(
                    os.environ['DB_URL'].split('://', 1)[0],
                    os.environ['DB_USER'],
                    os.environ['DB_PASSWORD'],
                    os.environ['DB_URL'].split('://', 1)[1],
                    os.environ['DB_USER']
                    )
                )
            del os.environ['DB_USER_CREATED']
            del os.environ['DB_USER']
            resp.raise_for_status()

    def setUp(self):
        """
        Set up test attributes for unit tests targeting a database
        """
        self.set_up_client()

    def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None,
                      timeout=(30,300)):
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            admin_party = False
            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                admin_party = True
            self.user = os.environ.get('DB_USER', None)
            self.pwd = os.environ.get('DB_PASSWORD', None)
            self.url = os.environ['DB_URL']
            self.client = CouchDB(
                self.user,
                self.pwd,
                admin_party,
                url=self.url,
                connect=auto_connect,
                auto_renew=auto_renew,
                encoder=encoder,
                timeout=timeout
            )
        else:
            self.account = os.environ.get('CLOUDANT_ACCOUNT')
            self.user = os.environ.get('DB_USER')
            self.pwd = os.environ.get('DB_PASSWORD')
            self.url = os.environ.get(
                'DB_URL',
                'https://{0}.cloudant.com'.format(self.account))
            self.client = Cloudant(
                self.user,
                self.pwd,
                url=self.url,
                x_cloudant_user=self.account,
                connect=auto_connect,
                auto_renew=auto_renew,
                encoder=encoder,
                timeout=timeout
            )


    def tearDown(self):
        """
        Ensure the client is new for each test
        """
        del self.client

    def db_set_up(self):
        """
        Set up test attributes for Database tests
        """
        self.client.connect()
        self.test_dbname = self.dbname()
        self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname)
        self.db.create()

    def db_tear_down(self):
        """
        Reset test attributes for each test
        """
        self.db.delete()
        self.client.disconnect()
        del self.test_dbname
        del self.db

    def dbname(self, database_name='db'):
        return '{0}-{1}-{2}'.format(database_name, self._testMethodName, unicode_(uuid.uuid4()))

    def populate_db_with_documents(self, doc_count=100, **kwargs):
        off_set = kwargs.get('off_set', 0)
        docs = [
            {'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
            for i in range(off_set, off_set + doc_count)
        ]
        return self.db.bulk_docs(docs)

    def create_views(self):
        """
        Create a design document with views for use with tests.
        """
        self.ddoc = DesignDocument(self.db, 'ddoc001')
        self.ddoc.add_view(
            'view001',
            'function (doc) {\n emit(doc._id, 1);\n}'
        )
        self.ddoc.add_view(
            'view002',
            'function (doc) {\n emit(doc._id, 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view003',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
        )
        self.ddoc.add_view(
            'view004',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view005',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
        )
        self.ddoc.add_view(
            'view006',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
            '_count'
        )
        self.ddoc.save()
        self.view001 = self.ddoc.get_view('view001')
        self.view002 = self.ddoc.get_view('view002')
        self.view003 = self.ddoc.get_view('view003')
        self.view004 = self.ddoc.get_view('view004')
        self.view005 = self.ddoc.get_view('view005')
        self.view006 = self.ddoc.get_view('view006')

    def create_search_index(self):
        """
        Create a design document with search indexes for use
        with search query tests.
        """
        self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
        self.search_ddoc['indexes'] = {'searchindex001': {
                'index': 'function (doc) {\n  index("default", doc._id); \n '
                'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
                'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
            }
        }
        self.search_ddoc.save()

    def load_security_document_data(self):
        """
        Create a security document in the specified database and assign
        attributes to be used during unit tests
        """
        self.sdoc = {
            'admins': {'names': ['foo'], 'roles': ['admins']},
            'members': {'names': ['foo1', 'foo2'], 'roles': ['developers']}
        }
        self.mod_sdoc = {
            'admins': {'names': ['bar'], 'roles': ['admins']},
            'members': {'names': ['bar1', 'bar2'], 'roles': ['developers']}
        }
        if os.environ.get('RUN_CLOUDANT_TESTS') is not None:
            self.sdoc = {
                'cloudant': {
                    'foo1': ['_reader', '_writer'],
                    'foo2': ['_reader']
                }
            }
            self.mod_sdoc = {
                'cloudant': {
                    'bar1': ['_reader', '_writer'],
                    'bar2': ['_reader']
                }
            }
        if os.environ.get('ADMIN_PARTY') == 'true':
            resp = requests.put(
                '/'.join([self.db.database_url, '_security']),
                data=json.dumps(self.sdoc),
                headers={'Content-Type': 'application/json'}
            )
        else:
            resp = requests.put(
                '/'.join([self.db.database_url, '_security']),
                auth=(self.user, self.pwd),
                data=json.dumps(self.sdoc),
                headers={'Content-Type': 'application/json'}
            )
        self.assertEqual(resp.status_code, 200)
class UnitTestDbBase(unittest.TestCase):
    """
    The base class for all unit tests targeting a database
    """

    @classmethod
    def setUpClass(cls):
        """
        If targeting CouchDB, Set up a CouchDB instance otherwise do nothing.
          
        Note: Admin Party is currently unsupported so we must create a 
          CouchDB user for tests to function with a CouchDB instance if one is
          not provided.
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            if os.environ.get('DB_URL') is None:
                os.environ['DB_URL'] = 'http://127.0.0.1:5984'

            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                if os.environ.get('DB_USER'):
                    del os.environ['DB_USER']
                if os.environ.get('DB_PASSWORD'):
                    del os.environ['DB_PASSWORD']
                return

            if os.environ.get('DB_USER') is None:
                os.environ['DB_USER_CREATED'] = '1'
                os.environ['DB_USER'] = '******'.format(
                    unicode_(uuid.uuid4())
                    )
                os.environ['DB_PASSWORD'] = '******'
                resp = requests.put(
                    '{0}/_config/admins/{1}'.format(
                        os.environ['DB_URL'],
                        os.environ['DB_USER']
                        ),
                    data='"{0}"'.format(os.environ['DB_PASSWORD'])
                    )
                resp.raise_for_status()

    @classmethod
    def tearDownClass(cls):
        """
        If necessary, clean up CouchDB instance once all tests are complete.
        """
        if (os.environ.get('RUN_CLOUDANT_TESTS') is None and
            os.environ.get('DB_USER_CREATED') is not None):
            resp = requests.delete(
                '{0}://{1}:{2}@{3}/_config/admins/{4}'.format(
                    os.environ['DB_URL'].split('://', 1)[0],
                    os.environ['DB_USER'],
                    os.environ['DB_PASSWORD'],
                    os.environ['DB_URL'].split('://', 1)[1],
                    os.environ['DB_USER']
                    )
                )
            del os.environ['DB_USER_CREATED']
            del os.environ['DB_USER']
            resp.raise_for_status()

    def setUp(self):
        """
        Set up test attributes for unit tests targeting a database
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            admin_party = False
            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                admin_party = True
            self.user = os.environ.get('DB_USER', None)
            self.pwd = os.environ.get('DB_PASSWORD', None)
            self.url = os.environ['DB_URL']
            self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url)
        else:
            self.account = os.environ.get('CLOUDANT_ACCOUNT')
            self.user = os.environ.get('DB_USER')
            self.pwd = os.environ.get('DB_PASSWORD')
            self.url = os.environ.get(
                'DB_URL',
                'https://{0}.cloudant.com'.format(self.account))
            self.client = Cloudant(
                self.user,
                self.pwd,
                url=self.url,
                x_cloudant_user=self.account)

    def tearDown(self):
        """
        Ensure the client is new for each test
        """
        del self.client

    def db_set_up(self):
        """
        Set up test attributes for Database tests
        """
        self.client.connect()
        self.test_dbname = self.dbname()
        self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname)
        self.db.create()

    def db_tear_down(self):
        """
        Reset test attributes for each test
        """
        self.db.delete()
        self.client.disconnect()
        del self.test_dbname
        del self.db

    def dbname(self, database_name='db'):
        return '{0}-{1}'.format(database_name, unicode_(uuid.uuid4()))

    def populate_db_with_documents(self, doc_count=100, **kwargs):
        off_set = kwargs.get('off_set', 0)
        docs = [
            {'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
            for i in range(off_set, off_set + doc_count)
        ]
        return self.db.bulk_docs(docs)

    def create_views(self):
        """
        Create a design document with views for use with tests.
        """
        self.ddoc = DesignDocument(self.db, 'ddoc001')
        self.ddoc.add_view(
            'view001',
            'function (doc) {\n emit(doc._id, 1);\n}'
        )
        self.ddoc.add_view(
            'view002',
            'function (doc) {\n emit(doc._id, 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view003',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
        )
        self.ddoc.add_view(
            'view004',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view005',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
        )
        self.ddoc.add_view(
            'view006',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
            '_count'
        )
        self.ddoc.save()
        self.view001 = self.ddoc.get_view('view001')
        self.view002 = self.ddoc.get_view('view002')
        self.view003 = self.ddoc.get_view('view003')
        self.view004 = self.ddoc.get_view('view004')
        self.view005 = self.ddoc.get_view('view005')
        self.view006 = self.ddoc.get_view('view006')

    def create_search_index(self):
        """
        Create a design document with search indexes for use
        with search query tests.
        """
        self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
        self.search_ddoc['indexes'] = {'searchindex001': {
                'index': 'function (doc) {\n  index("default", doc._id); \n '
                'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
                'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
            }
        }
        self.search_ddoc.save()
Example #41
0
  "auth-method": "token",
  "auth-token": "qwertyuiop"
}

try:
  deviceCli = ibmiotf.device.Client(deviceOptions)
except Exception as e:
  print("Caught exception connecting device: %s" % str(e))
  sys.exit()

try:
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ee2a9736-5f50-4046-8fb5-f57c210f05a7-bluemix.cloudant.com"
    cloudant_client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    cloudant_client.connect()
    print "Successfully connected"
except Exception as e:
    print "Unable to connect to cloudant service!"
    sys.exit()

feedback_db = cloudant_client['ttpfeedback'] #opening ttpfeedback in cloudantdb
docs_count = feedback_db.doc_count()
current_count = docs_count
flag_count = input("How many participants?") #the count of the no. of participants in the event
pub_count = 1
flag = True
# result_collection  = Result(feedback_db.all_docs, include_docs = True) #collection
deviceCli.connect()
ratings = {} #ratings dict/hastable
def publish():
class CloudantClientTests(UnitTestDbBase):
    """
    Cloudant specific client unit tests
    """

    def test_cloudant_context_helper(self):
        """
        Test that the cloudant context helper works as expected.
        """
        try:
            with cloudant(self.user, self.pwd, account=self.account) as c:
                self.assertIsInstance(c, Cloudant)
                self.assertIsInstance(c.r_session, requests.Session)
                self.assertEqual(c.r_session.auth, (self.user, self.pwd))
        except Exception as err:
            self.fail('Exception {0} was raised.'.format(str(err)))
    
    def test_constructor_with_account(self):
        """
        Test instantiating a client object using an account name
        """
        # Ensure that the client is new
        del self.client
        self.client = Cloudant(self.user, self.pwd, account=self.account)
        self.assertEqual(
            self.client.server_url,
            'https://{0}.cloudant.com'.format(self.account)
            )

    def test_connect_headers(self):
        """
        Test that the appropriate request headers are set
        """
        try:
            self.client.connect()
            self.assertEqual(
                self.client.r_session.headers['X-Cloudant-User'],
                self.account
                )
            agent = self.client.r_session.headers.get('User-Agent')
            ua_parts = agent.split('/')
            self.assertEqual(len(ua_parts), 6)
            self.assertEqual(ua_parts[0], 'python-cloudant')
            self.assertEqual(ua_parts[1], sys.modules['cloudant'].__version__)
            self.assertEqual(ua_parts[2], 'Python')
            self.assertEqual(ua_parts[3], '{0}.{1}.{2}'.format(
                sys.version_info[0], sys.version_info[1], sys.version_info[2])),
            self.assertEqual(ua_parts[4], os.uname()[0]),
            self.assertEqual(ua_parts[5], os.uname()[4])
        finally:
            self.client.disconnect()

    def test_db_updates_infinite_feed_call(self):
        """
        Test that infinite_db_updates() method call constructs and returns an
        InfiniteFeed object
        """
        try:
            self.client.connect()
            db_updates = self.client.infinite_db_updates()
            self.assertIsInstance(db_updates, InfiniteFeed)
            self.assertEqual(
                db_updates._url, '/'.join([self.client.server_url, '_db_updates']))
            self.assertIsInstance(db_updates._r_session, requests.Session)
            self.assertFalse(db_updates._raw_data)
            self.assertDictEqual(db_updates._options, {'feed': 'continuous'})
        finally:
            self.client.disconnect()

    def test_billing_data(self):
        """
        Test the retrieval of billing data
        """
        try:
            self.client.connect()
            expected = [
                'data_volume',
                'total',
                'start',
                'end',
                'http_heavy',
                'http_light'
                ]
            # Test using year and month
            year = 2016
            month = 1
            data = self.client.bill(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.bill()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with a type
        string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_volume_usage_data(self):
        """
        Test the retrieval of volume usage data
        """
        try:
            self.client.connect()
            expected = [
                'data_vol',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = 2016
            month = 12
            data = self.client.volume_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.volume_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with a type
        string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_requests_usage_data(self):
        """
        Test the retrieval of requests usage data
        """
        try:
            self.client.connect()
            expected = [
                'requests',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = 2016
            month = 1
            data = self.client.requests_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.requests_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with
        a type string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_shared_databases(self):
        """
        Test the retrieval of shared database list
        """
        try:
            self.client.connect()
            self.assertIsInstance(self.client.shared_databases(), list)
        finally:
            self.client.disconnect()

    def test_generate_api_key(self):
        """
        Test the generation of an API key for this client account
        """
        try:
            self.client.connect()
            expected = ['key', 'password', 'ok']
            api_key = self.client.generate_api_key()
            self.assertTrue(all(x in expected for x in api_key.keys()))
            self.assertTrue(api_key['ok'])
        finally:
            self.client.disconnect()

    def test_cors_configuration(self):
        """
        Test the retrieval of the current CORS configuration for this client
        account
        """
        try:
            self.client.connect()
            expected = ['allow_credentials', 'enable_cors', 'origins']
            cors = self.client.cors_configuration()
            self.assertTrue(all(x in expected for x in cors.keys()))
        finally:
            self.client.disconnect()

    def test_cors_origins(self):
        """
        Test the retrieval of the CORS origins list
        """
        try:
            self.client.connect()
            origins = self.client.cors_origins()
            self.assertIsInstance(origins, list)
        finally:
            self.client.disconnect()

    def test_disable_cors(self):
        """
        Test disabling CORS (assuming CORS is enabled)
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test CORS disable
            self.assertEqual(self.client.disable_cors(), {'ok': True})
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()

    def test_update_cors_configuration(self):
        """
        Test updating CORS configuration
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test updating CORS settings, overwriting origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.com'],
                True)
            self.assertEqual(result, {'ok': True})
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected = ['https://ibm.com']
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Test updating CORS settings, adding to origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.cloudant.com']
                )
            self.assertEqual(result, {'ok': True})
            del updated_cors
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected.append('https://ibm.cloudant.com')
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()