Esempio n. 1
0
def beer_profile(id):
	beer = model.session.query(model.Beer).get(id)
	user_id = current_user.id

	ratings = beer.ratings # all rating objects for a particular beer
	rating_nums = [] # create list rating_nums
	user_rating = None
	for r in ratings: # loop through each rating object in all rating objects
		if r.rating != 0:
			if r.user_id == current_user.id: # does this rating belong to this user
				user_rating = r # yes, yes it does
			rating_nums.append(r.rating)
	avg_rating = float((sum(rating_nums))/(len(rating_nums)))

	# # using list comprehension
	# ratings = model.session.query(model.Rating).\
	# 		  filter_by(beer_id=id).\
	# 	  	  filter(model.Rating.rating != 0).\
	# 	  	  all() # all rating objects for a particular beer
	# rating_nums = [ r.rating for r in ratings ]

	my_rating = None
	for x in ratings:
		if x.rating == 0 and x.user_id == current_user.id:
			my_rating = x

	# only predict if the user hasn't rated yet
	if user_rating is None or my_rating:
		not_rounded = current_user.predict_rating(beer)
		prediction = int(round(not_rounded))
	else:
		prediction = user_rating.rating
		not_rounded = user_rating.rating


	# to identify beers with similar prediction coefficients
	user_ratings = model.session.query(model.Rating).filter_by(user_id=current_user.id).all()
	rated_beers = []
	for i in user_ratings:
		if i.rating != 0:
			rated_beers.append(i.beer_id)
	not_rated = model.session.query(model.Beer).filter(~model.Beer.id.in_(rated_beers))

	other_predictions = []
	for unrated in not_rated:
		if unrated.id != beer.id:
			other_prediction = current_user.predict_rating(unrated)
			difference = fabs(other_prediction - not_rounded)
			if difference < 0.5:
				mod_prediction = int(round(other_prediction))
				beername = unrated.name
				beerid = unrated.id
				beerimage = unrated.image
				other_predictions.append((other_prediction, beername, beerid, mod_prediction, beerimage))
	similar_five = itertools.islice(other_predictions, 0, 5)

	return render_template("beer_profile.html", beer=beer, userid=user_id,\
		average=avg_rating, user_rating=user_rating, my_rating=my_rating, prediction=prediction,\
		try_five=similar_five)
Esempio n. 2
0
def beer_profile(id):
    beer = model.session.query(model.Beer).get(id)
    user_id = current_user.id
    ratings = beer.ratings
    rating_nums = []
    user_rating = None
    for r in ratings:
        if r.user_id == current_user.id:
            user_rating = r
        rating_nums.append(r.rating)
    avg_rating = float(sum(rating_nums)) / len(rating_nums)

    # prediction code: only predict if the user hasn't rated yet
    if user_rating is None:
        prediction = int(round(current_user.predict_rating(beer)))
    else:
        prediction = user_rating.rating
        # end prediction
    return render_template(
        "beer_profile.html",
        beer=beer,
        user_id=user_id,
        average=avg_rating,
        user_rating=user_rating,
        prediction=prediction,
    )
Esempio n. 3
0
def user_predictions(id):
	if id == current_user.id:
		user_name = current_user.username
		user_id = current_user.id
		all_ratings = model.session.query(model.Rating).filter_by(user_id=id).\
			order_by(model.Rating.rating.desc()).all()
		all_beers = model.session.query(model.Beer).all()
		user_ratings = []
		for r in all_ratings:
			if r.rating != 0:
				user_ratings.append(r)
		how_many = ((len(all_beers))-(len(user_ratings)))

		rated_beers = []
		for i in user_ratings:
			rated_beers.append(i.beer_id)
		not_rated = model.session.query(model.Beer).filter(~model.Beer.id.in_(rated_beers))

		predictions = []
		for beer in not_rated:
			prediction = current_user.predict_rating(beer)
			mod_prediction = int(round(prediction))
			beername = beer.name
			beerid = beer.id
			predictions.append((prediction, beername, beerid, mod_prediction))
		high_prediction = sorted(predictions, key=operator.itemgetter(0), reverse = True)
		top_twenty = itertools.islice(high_prediction, 0, 20)

		return render_template("user_predictions.html", user_name = user_name, \
			ratings=user_ratings, count = how_many, not_rated = not_rated,\
			high_pred = top_twenty, userid=user_id)
Esempio n. 4
0
def user_profile():
	user_name = current_user.username
	user_id = current_user.id
	all_ratings = model.session.query(model.Rating).filter_by(user_id=current_user.id).\
		order_by(model.Rating.rating.desc()).all()
		
	user_ratings = []
	for r in all_ratings:
		if r.rating != 0:
			user_ratings.append(r)
	how_many = len(user_ratings)
	high_rated = itertools.islice(user_ratings, 0, 5)

	rated_beers = []
	for i in user_ratings:
		rated_beers.append(i.beer_id)
	not_rated = model.session.query(model.Beer).filter(~model.Beer.id.in_(rated_beers))

	predictions = []
	for beer in not_rated:
		prediction = current_user.predict_rating(beer)
		mod_prediction = int(round(prediction))
		beername = beer.name
		beerid = beer.id
		beerimage = beer.image
		predictions.append((prediction, beername, beerid, mod_prediction, beerimage))
	high_prediction = sorted(predictions, key=operator.itemgetter(0), reverse = True)
	best_five = itertools.islice(high_prediction, 0, 5)

	return render_template("user_profile.html", high_rated = high_rated, \
	count = how_many, user_name = user_name, userid=user_id,\
	high_pred = best_five)
Esempio n. 5
0
def user_queue():
	user_name = current_user.username
	user_id = current_user.id
	user_queue = model.session.query(model.Rating).filter_by(user_id=current_user.id, rating=0).all()
	how_many = len(user_queue)
		
	in_queue = []
	for i in user_queue:
		in_queue.append(i.beer_id)
	beer_queue = model.session.query(model.Beer).filter(model.Beer.id.in_(in_queue))
		
	predictions = []
	for beer in beer_queue:
		prediction = current_user.predict_rating(beer)
		mod_prediction = int(round(prediction))
		beername = beer.name
		beerid = beer.id
		beerimage = beer.image
		predictions.append((prediction, beername, beerid, mod_prediction, beerimage))
	high_queue = sorted(predictions, key=operator.itemgetter(0), reverse = True)

	all_ratings = model.session.query(model.Rating).filter_by(user_id=current_user.id).\
		order_by(model.Rating.rating.desc()).all()
	rated_beers = []
	for i in all_ratings:
		rated_beers.append(i.beer_id)
	not_queue = model.session.query(model.Beer).filter(~model.Beer.id.in_(rated_beers))

	other_predictions = []
	for beer in not_queue:
		prediction = current_user.predict_rating(beer)
		mod_prediction = int(round(prediction))
		beername = beer.name
		beerid = beer.id
		beerimage = beer.image
		other_predictions.append((prediction, beername, beerid, mod_prediction, beerimage))
	high_prediction = sorted(other_predictions, key=operator.itemgetter(0), reverse = True)
	best_five = itertools.islice(high_prediction, 0, 5)
	print best_five

	return render_template("user_queue.html", user_name = user_name, \
		queue=high_queue, high_pred = best_five, count = how_many, userid=user_id)
Esempio n. 6
0
def beer_profile(id):
    beer = model.session.query(model.Beer).get(id)
    user_id = current_user.id
    ratings = beer.ratings
    rating_nums = []
    user_rating = None
    for r in ratings:
        if r.user_id == current_user.id:
            user_rating = r
        rating_nums.append(r.rating)
    avg_rating = float(sum(rating_nums)) / len(rating_nums)

    # prediction code: only predict if the user hasn't rated yet
    if user_rating is None:
        prediction = int(round(current_user.predict_rating(beer)))
    else:
        prediction = user_rating.rating
    # end prediction
    return render_template("beer_profile.html", beer=beer, user_id=user_id,\
     average=avg_rating, user_rating=user_rating, prediction=prediction)
Esempio n. 7
0
def home_display():
	username = current_user.username
	userid = current_user.id

	# TO SHOW HIGHEST AVERAGE RATINGS, across all beers whether user has rated or not
	# AND MOST RATED / POPULAR BEERS
	all_beers = model.session.query(model.Beer).all()
	all_ratings = []  #becomes a list of lists. each inner list has all ratings for particular beer
	for beer in all_beers:	
		list_ratings = []
		for r in beer.ratings:
			if r.rating != 0:
				list_ratings.append(r)
		all_ratings.append(list_ratings)

	id_avg_ln = []  #stands for beer id, average, length
	for r in all_ratings:
		how_many = len(r)
		rating_nums = []
		for i in r:
			beerid = i.beer_id
			rating = i.rating
			rating_nums.append(rating)
			average = (float(sum(rating_nums))/len(rating_nums))
		id_avg_ln.append((beerid, average, how_many))

	beernames = []
	for i in id_avg_ln:
		beer = model.session.query(model.Beer).filter_by(id=i[0]).one()
		beernames.append((beer.name, beer.image))
	id_avg_ln_nm_d = dict(zip(id_avg_ln, beernames))
	
	id_avg_ln_nm = []   #stands for beer id, average, length, name
	for key in id_avg_ln_nm_d:
		tuple1 = key[0]
		tuple2 = key[1]
		tuple3 = key[2]
		tuple4 = id_avg_ln_nm_d[key]
		id_avg_ln_nm.append((tuple1, tuple2, tuple3, tuple4))
	
	sortby_avg = sorted(id_avg_ln_nm, key=operator.itemgetter(1), reverse = True)
	high_five = itertools.islice(sortby_avg, 0, 5) #lol, high five!

	sortby_len = sorted(id_avg_ln_nm, key=operator.itemgetter(2), reverse = True)
	popular_five = itertools.islice(sortby_len, 0, 5) #popular, like the plastics... or your mom

	# TO SHOW HIGHEST PREDICTION MATCHES
	user_ratings = model.session.query(model.Rating).filter_by(user_id=userid).all()
	rated_beers = []
	for i in user_ratings:
		if i.rating != 0:
			rated_beers.append(i.beer_id)
	not_rated = model.session.query(model.Beer).filter(~model.Beer.id.in_(rated_beers))

	predictions = []
	for beer in not_rated:
		prediction = current_user.predict_rating(beer)
		mod_prediction = int(round(prediction))
		beername = beer.name
		beerid = beer.id
		beerimage = beer.image
		predictions.append((prediction, beername, beerid, mod_prediction, beerimage))
	high_prediction = sorted(predictions, key=operator.itemgetter(0), reverse = True)
	best_five = itertools.islice(high_prediction, 0, 5)

	# whoa, i can't believe i wrote all that
	return render_template("home.html", user_name=username, userid=userid,\
		high_averages=high_five, most_rated=popular_five, high_pred = best_five)