Esempio n. 1
0
def userProducts(user_id):

    if request.method == 'GET':
        #lookup all products for in users collection
        response = jsonify(userProducts=p_ctrl.get_products_by_user_id(user_id))
        #respond array of products and a 200
        return response, 200
    
    if request.method == 'POST':
        body = request.get_json()
        #check if product is already in DB
        product_id = p_ctrl.verify_product_by_name_and_brand(body['product_name'], body['brand_name'])
        if product_id == None:    
            #Add product if not
            product_id = p_ctrl.add_product_to_products(body['product_name'], body['brand_name'], body['product_category'])
        #create db relationship between user and product
        response = jsonify(p_ctrl.add_user_to_product(
            user_id, 
            product_id, 
            body.get('product_size'),
            body.get('product_status'),
            body.get('product_notes'),
            body.get('product_color'),
            body.get('stars'),
            body.get('review'),
            body.get('product_image_url'),
            body.get('product_category')
        ))
        e_ctrl.add_event(user_id, "added a product", "product", product_id)
        return response, 201

    if request.method == 'PUT':
        body = request.get_json()
        p = body['product']
        product_id = p_ctrl.verify_product_by_name_and_brand(p['product_name'], p['brand_name'])
        response = jsonify(p_ctrl.edit_user_to_product(
            user_id,
            product_id, 
            p.get('product_size'),
            p.get('product_status'),
            p.get('product_notes'),
            p.get('product_color'),
            p.get('stars'),
            p.get('review'),
            p.get('product_image_url'),
            p.get('product_category')
        ))
        return response, 202

    if request.method == 'DELETE':
        body = request.get_json()
        p_ctrl.remove_user_from_product(body['product_id'])
        return "Product Removed", 204
Esempio n. 2
0
def find_prob(user_id):

    # 1885
    # fetch user from database
    all_prod_likes_file = open('../data/number_all_prod_likes.json', 'r')
    all_prod_likes = json.loads(all_prod_likes_file.read())
    all_prod_likes_file.close()

    all_users_file = open('../data/number_all_user_reviews.json', 'r')
    all_users = json.loads(all_users_file.read())
    all_users_file.close()
    counter = 0

    # ******** fetch user info from database *********
    from db_controller import user_controller as u_ctrl
    user_info = u_ctrl.get_user_as_dictionary(user_id)

    from db_controller import product_controller as p_ctrl
    products = p_ctrl.get_products_by_user_id(user_id)

    # age processing
    if user_info['birthday'] != '':
        birthday = user_info['birthday']
        from datetime import date
        age = int(date.today().year) - int(birthday[0:4])
    else:
        age = None

    current_user = {
        'age': age,
        'location': user_info['location'],
        'skin_tone': user_info['skin_tone'],
        'reviews': []
    }
    # 'reviews':[{'rating':1,'productID':'sephorid'}]
    # ********* built up current user from db fetch **************
    for product in products:
        current_user['reviews'].append({
            'rating': product['product_rating'],
            'productID': product['sephora_id']
        })

    # ******** fetch products ids to check against from file **********
    productIDs = []
    for prod in all_prod_likes:
        counter += 1
        productIDs.append(prod)
        if counter > 1100:
            break
        # if counter > 11004:
        #   break
        # if counter > productNum:

    # keep store here
    res = [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
    storage = {}
    # iterate over specified prodicts and return probability values
    for productID in productIDs:
        prod_like_ref = all_prod_likes[productID]
        user_prod_likes = prod_like_ref['L']
        user_prod_dislikes = prod_like_ref['D']

        # user:simIndex
        storage_like_simIndex = {}
        storage_dislike_simIndex = {}
        for name in user_prod_likes:
            # name = str(name)
            b = str(name)
            user = all_users[b]
            index = compoundSimilarityIndex(current_user, user)
            storage_like_simIndex[b] = index

        for name in user_prod_dislikes:
            # name = str[name]python3
            b = str(name)
            user = all_users[b]
            index = compoundSimilarityIndex(current_user, user)
            storage_dislike_simIndex[b] = index

        probability = prob_user_likes_product(productID, storage_like_simIndex,
                                              storage_dislike_simIndex)

        # select maximum values algo
        nextTupl = False
        for index, tupl in enumerate(res):
            # cascade change
            if nextTupl != False:
                temp = res[index]
                res[index] = nextTupl
                nextTupl = temp
            elif probability > tupl[0]:
                nextTupl = res[index]
                res[index] = (probability, productID)

        storage[productID] = probability

    #return top 5 in a tuple
    # print(res)
    # unpack and push to db
    r_ctrl.remove_recommendation(user_id)
    # remove all recommendations from db
    for index, tupl in enumerate(res):
        rank = index + 1
        sephora_product_id = tupl[1]
        # save to database
        # make sure this saves the other id, not the sephora id
        product_id = p_ctrl.get_product_id_by_sephora_product_id(
            sephora_product_id)
        # product = p_ctrl.get_product_as_dictionary(product_id)
        res = r_ctrl.add_recommendation(user_id, product_id, rank)


# print(productIDs)
# start = time.clock()
# probs  = find_prob(productIDs,current_user)
# print(probs)
# end = time.clock()
# interval = end - start
# print(interval)
# print(probs[1])

# get rid of anons

# print similarityIndex(sampleUser0,sampleUser1)
# print similarityIndex(sampleUser0,sampleUser3)

# diff is high
# shift info to a mongodb
# make async worker go off and fetch stuff
# make other cool visualizations with data
# a graph of products

# make new file of usernames removed go through motions and push that up
# with rec engine
Esempio n. 3
0
def find_prob(user_id):

    # 1885                                                                                                                                                                      
    # fetch user from database                                                                                                                                                                                                      
    all_prod_likes_file = open('../data/number_all_prod_likes.json','r')
    all_prod_likes = json.loads(all_prod_likes_file.read())
    all_prod_likes_file.close()

    all_users_file = open('../data/number_all_user_reviews.json','r')
    all_users = json.loads(all_users_file.read())
    all_users_file.close()
    counter = 0

    # ******** fetch user info from database *********
    from db_controller import user_controller as u_ctrl
    user_info = u_ctrl.get_user_as_dictionary(user_id)

    from db_controller import product_controller as p_ctrl
    products = p_ctrl.get_products_by_user_id(user_id)

    # age processing
    if user_info['birthday'] != '':
        birthday = user_info['birthday']
        from datetime import date
        age = int(date.today().year) - int(birthday[0:4])
    else: 
        age = None

    current_user = {
        'age':age,
        'location':user_info['location'],
        'skin_tone':user_info['skin_tone'],
        'reviews':[]
    }
    # 'reviews':[{'rating':1,'productID':'sephorid'}]
    # ********* built up current user from db fetch **************
    for product in products:
        current_user['reviews'].append({
            'rating':product['product_rating'],
            'productID':product['sephora_id']
            })

    # ******** fetch products ids to check against from file **********
    productIDs = []
    for prod in all_prod_likes:
        counter +=1
        productIDs.append(prod)
        if counter > 1100:
                break
        # if counter > 11004:
        #   break
        # if counter > productNum:

    # keep store here
    res = [(0,0),(0,0),(0,0),(0,0),(0,0)]
    storage={}
    # iterate over specified prodicts and return probability values
    for productID in productIDs:
        prod_like_ref = all_prod_likes[productID]
        user_prod_likes = prod_like_ref['L']
        user_prod_dislikes = prod_like_ref['D']

        # user:simIndex
        storage_like_simIndex = {}
        storage_dislike_simIndex = {}
        for name in user_prod_likes:
            # name = str(name)
            b = str(name)
            user = all_users[b]
            index = compoundSimilarityIndex(current_user,user)
            storage_like_simIndex[b] = index

        for name in user_prod_dislikes:
            # name = str[name]python3
            b = str(name)
            user = all_users[b]
            index = compoundSimilarityIndex(current_user,user)
            storage_dislike_simIndex[b] = index

        probability = prob_user_likes_product(productID,storage_like_simIndex,storage_dislike_simIndex)

        # select maximum values algo
        nextTupl = False
        for index,tupl in enumerate(res):
            # cascade change
            if nextTupl != False:
                temp = res[index] 
                res[index] = nextTupl
                nextTupl = temp
            elif probability>tupl[0]:
                nextTupl = res[index]
                res[index] = (probability,productID)

        storage[productID] = probability

    #return top 5 in a tuple
    # print(res)
    # unpack and push to db
    r_ctrl.remove_recommendation(user_id)
    # remove all recommendations from db
    for index,tupl in enumerate(res):
        rank = index+1
        sephora_product_id = tupl[1]
        # save to database
        # make sure this saves the other id, not the sephora id
        product_id = p_ctrl.get_product_id_by_sephora_product_id(sephora_product_id)
        # product = p_ctrl.get_product_as_dictionary(product_id)
        res = r_ctrl.add_recommendation(user_id, product_id, rank)

# print(productIDs)
# start = time.clock()
# probs  = find_prob(productIDs,current_user)
# print(probs)
# end = time.clock()
# interval = end - start
# print(interval)
# print(probs[1])

# get rid of anons 

# print similarityIndex(sampleUser0,sampleUser1)
# print similarityIndex(sampleUser0,sampleUser3)

# diff is high
# shift info to a mongodb
# make async worker go off and fetch stuff
# make other cool visualizations with data
# a graph of products 

# make new file of usernames removed go through motions and push that up 
# with rec engine
Esempio n. 4
0
def userProfile(user_id):
    if request.method == 'GET':
        user = u_ctrl.get_user_as_dictionary(user_id)
        userProducts = p_ctrl.get_products_by_user_id(user_id)
        response = jsonify({'user': user, 'userProducts': userProducts})
        return response, 200