def ratings_edit(rating_id): form = RatingForm(request.form) form.beer.choices = [(g.id, g.name) for g in Beer.query.all()] form.flavor.choices = [(g.id, g.name) for g in Flavor.query.all()] if not form.validate(): return render_template("ratings/edit.html", form=form, id=rating_id) r = Rating.query.get(rating_id) r.rating = form.rating.data r.comment = form.comment.data r.account_id = current_user.id db.session().commit() return redirect(url_for("ratings_index"))
def ratings_create(route_id): form = RatingForm(request.form) if form.validate(): rating = Rating.query.filter_by(account_id=current_user.id).filter_by( route_id=route_id).first() if rating is None: rating = Rating(int(form.new_rating.data), int(route_id)) rating.account_id = current_user.id rating.set_rater_data(current_user.height, current_user.weight, current_user.arm_span) db.session().add(rating) else: rating._set_rating_value(int(form.new_rating.data)) rating.set_rater_data(current_user.height, current_user.weight, current_user.arm_span) db.session().commit() return redirect(url_for("routes_index"))
def ratings_create(): form = RatingForm(request.form) form.beer.choices = [(g.id, g.name) for g in Beer.query.all()] form.flavor.choices = [(g.id, g.name) for g in Flavor.query.all()] if not form.validate(): return render_template("ratings/new.html", form=form) r = Rating(form.rating.data, form.comment.data) r.account_id = current_user.id r.beer_id = form.beer.data db.session().add(r) db.session().commit() for g in form.flavor.data: rf = RatingFlavor(r.id, g) db.session().add(rf) db.session().commit() return redirect(url_for("ratings_index"))
def add_rating(movie_id): rating = Rating.query.filter_by(movie_id=movie_id, user_id=current_user.id).first() form = RatingForm(request.form) if not form.validate(): return render_template("movies/view.html", movie=Movie.query.get(movie_id), form=form, rating=rating) if not rating: rating = Rating() rating.movie_id = movie_id rating.user_id = current_user.id rating.want_to_watch = False db.session().add(rating) rating.rating = form.rating.data db.session().commit() return redirect(url_for("movies_view", movie_id=movie_id))
def movies_view(movie_id): # Count the number of ratings for each category res = Rating.count_ratings_per_category(movie_id) # Assign the number of ratings to the correct category. Set the value to zero if there're no ratings. ratings = [0] * 10 for result in res: ratings[result.get("rating") - 1] = result.get("count") # Find the category with the most ratings max_rating = max(max(ratings), 1) # Weight each category based on the highest number of ratings weighted_ratings = [r / max_rating for r in ratings] if current_user.is_authenticated: rating = Rating.query.filter_by(movie_id=movie_id, user_id=current_user.id).first() else: rating = None return render_template("movies/view.html", movie=Movie.query.get(movie_id), form=RatingForm(obj=rating), rating=rating, ratings=weighted_ratings)
def routes_index(): routes_to_show = Route.query.order_by(Route.grade).all() return render_template("routes/list.html", routes=routes_to_show, form=RatingForm())
def ratings_form(): f = RatingForm() f.beer.choices = [(g.id, g.name) for g in Beer.query.all()] f.flavor.choices = [(g.id, g.name) for g in Flavor.query.all()] return render_template("ratings/new.html", form=f)