Exemple #1
0
def get_recommendation(uid):

	'''
		random from a 'pool' of object what is not already in 'control'
		return None if cannot find one
	'''
	def get_unique_random(control, pool):
		# loop until 
		while True:
			# if exhausted of choices, return None
			if len(pool) <= 0:
				return None

			# re-random
			choice = random.choice(pool)

			# remove the choice from pool
			try:
				pool.remove(choice)
			except ValueError:
				pass # or scream: thing not in some_list!
			except AttributeError:
				pass # call security, some_list not quacking like a list!

			# return the choice if unique, otherwise keep looping
			if (choice not in control):
				return choice




	recommendations = []

	# --------------------------------------------
	#  From User Top 5
	# --------------------------------------------
	# choose more from higher view count category
	weight = 4;
	top5 = db.get_top_five_categories(uid)
	logger.debug('*'* 120)
	logger.debug('Selecting from User Preferences')
	logger.debug('*'*120)
	logger.debug('\n\r')
	for cat in top5:
		logger.debug('x'* 100)
		logger.debug('for categories '+cat['cid'])
		logger.debug('x'*100)
		logger.debug('\n\r')

		logger.debug('Selecting from user with rating = 0')
		logger.debug('|'*60)

		# this ensure we have at least one user with 0 rating for each category
		products = db.queryDB2(get_product_for_cid_with_rating_condition.format(cat['cid'], '= 0'))
		db.resolveProducts(products)
		choice = get_unique_random(recommendations, products)

		logger.debug('Choice:')
		print_choice(choice)

		if choice is not None:
			recommendations.append(choice)

		logger.debug('Recommendations:')
		print_products(recommendations)

		logger.debug('Selecting from user with rating > 0')
		logger.debug('|'*50)

		products = db.queryDB2(get_product_for_cid_with_rating_condition.format(cat['cid'], '> 0'))
		db.resolveProducts(products)

		logger.debug('Products:')
		print_products(products)

		for x in range(0,weight):
			if len(products) <= 0:
				break
			choice = random.choice(products)
			recommendations.append(choice)
			# remove the choosen item so the item
			# cannot appear twice in the same list
			products.remove(choice)

		logger.debug('Recommendations:')
		print_products(recommendations)

		weight -= 1

	# --------------------------------------------
	#  Fill with Global Top 5
	# --------------------------------------------
	# choose more from higher view count category
	if len(recommendations) < 10:
		weight = 5;
		global_top5 = db.get_global_top_five()
		for cat in global_top5:
			products = db.get_product_category(cat['cid'])
			for x in range(0,weight):
				choice = get_unique_random(recommendations, products)
				if choice is None:
					break
				recommendations.append(choice)
			weight -= 1

	# --------------------------------------------
	#  Fill with Random
	# --------------------------------------------
	# fill remaining space with random products
	# note that 'if' were used instead of 'while'
	# to prevent possible infinite loop from
	# exhaustion of chioce in the entire system
	if len(recommendations) < 10:
		products = db.get_random_product()
		for x in range(0,len(recommendations)):
			choice = get_unique_random(recommendations, products)
			if choice is None:
				break
			recommendations.append(choice)
		
	return recommendations;
Exemple #2
0
def handle_message(msg):
    roomid = extract_chat_id(msg)
    msg['from_buyer'] = (msg['buyer_uid'] == msg['sender'])
    db.storeMessage(msg)
    emit('msg_receive', msg, room=roomid)
Exemple #3
0
def handle_connection(uid):
    print('a user has connected [uid]: ' + uid)
    chatrooms = db.getChatRooms(uid)
    for cr in chatrooms:
        roomid = extract_chat_id(cr)
        join_room(roomid)
Exemple #4
0
def get_category_info(cid):
    return jsonify({'result': db.get_category_info(cid)})
Exemple #5
0
def post_product():
    return jsonify({'resut': db.post_product(request.json)})
Exemple #6
0
def get_Messages():
    return db.get_Messages(request.json)
Exemple #7
0
def get_product_count(cid):
    return jsonify({'result': db.get_product_count(cid)})
Exemple #8
0
def getChatRooms(uid):
    result = db.getChatRooms(uid)
    return jsonify({'result': result})
Exemple #9
0
def post_create_chat_room():
    return db.post_create_chat_room(request.json)
Exemple #10
0
def get_avg_rating(sid):
    return db.get_avg_rating(sid)
Exemple #11
0
def post_track():
    return db.post_track_user_data(request.json)
Exemple #12
0
def post_submit_feedback():
    if request.json['rating'] <= 3:
        print('Notify Kaidee Stuff!')
    return db.post_submit_feedback(request.json)
Exemple #13
0
def get_category_list():
    return db.get_category_list()
Exemple #14
0
def get_product_category(cid):
    result = db.get_product_category(cid)
    return jsonify({'result': result})
Exemple #15
0
def get_Product(iid):
    return db.get_Product(iid)
Exemple #16
0
def select():
    return jsonify(db.selectAll())