def show(): """ Show calories :return: JSON or HTML page """ session = current_app.config['db'] items = session\ .query(WineCalories)\ .order_by(asc(func.lower(WineCalories.name))) if is_json_request(request): return jsonify(items=[x.serialize for x in items]) else: return render_template(template_prefix+'view.html', items=items)
def show(): """ Shows the serving temperatures :return: JSON or HTML page """ session = current_app.config['db'] items = session\ .query(Temperature)\ .order_by(asc(Temperature.temp)) if is_json_request(request): return jsonify(items=[x.serialize for x in items]) else: return render_template(template_prefix+'view.html', items=items)
def list_user_reviews(user_id): """ Returns the current logged in user reviews. :param user_id: User id to return reviews for. :return: JSON or HTML page """ session = current_app.config['db'] reviews = session.query(UserReview)\ .filter_by(user_id=user_id)\ .order_by(UserReview.date_created.desc()) if is_json_request(request): return jsonify(items=[x.serialize for x in reviews]) else: return render_template(template_prefix+"user_reviews_list.html", reviews=reviews)
def show(): """ Show wine types :return: JSON or HTML page """ session = current_app.config['db'] winetypes = session\ .query(WineType.id, WineType.name)\ .order_by(asc(func.lower(WineType.name))) if is_json_request(request): query = session\ .query(WineType)\ .order_by(asc(func.lower(WineType.name))) return jsonify(items=[x.serialize for x in query]) else: return render_template(template_prefix+'view.html', winetypes=winetypes)
def show_branditem(stockitem_id): """ Shows a specific brand item and all the user reviews. Includes a percentage breakdown of the ratings given. :param stockitem_id: WineBrand id :return: JSON or HTML page """ from json_util import ReviewPercentage session = current_app.config['db'] item = session\ .query(WineBrand)\ .filter_by(id=stockitem_id)\ .one() reviews = session\ .query(UserReview)\ .filter_by(winebrand_id=stockitem_id)\ .order_by(UserReview.date_created.desc()) # Counts and groups the ratings totalcount = session\ .query(func.count(UserReview.rating).label('totalcount'))\ .filter(UserReview.winebrand_id == stockitem_id)\ .subquery() # Calculate percentages for each rating counter = session\ .query(UserReview.rating, ((100.00 * func.count(UserReview.rating) .label('count')) / totalcount.c.totalcount).label('percent'))\ .filter(UserReview.winebrand_id == stockitem_id)\ .group_by(UserReview.rating, totalcount.c.totalcount)\ .order_by(UserReview.rating.desc()) if is_json_request(request): schema = ReviewPercentage() return jsonify( {"items": [x.serialize for x in reviews], "persentages": [schema.dump(x).data for x in counter] }) else: return render_template(template_prefix+"brandview.html", item=item, reviews=reviews, counter=counter)
def show(): """ Shows the wine types and number of each available. :return: JSON or HTML page """ from json_util import WineCaveHomeListSchema session = current_app.config['db'] wines = session\ .query(WineType, func.count(WineBrand.id).label('count'))\ .join(WineBrand) \ .group_by(WineType.name, WineType.id, WineType.color_id, WineType.glass_type_id, WineType.calorie_id, WineType.abv_id, WineType.temperature_id, WineType.user_id, WineType.date_created, WineType.date_edited) \ .order_by(asc(func.lower(WineType.name))) if is_json_request(request): schema = WineCaveHomeListSchema() return jsonify(items=[schema.dump(x).data for x in wines]) else: return render_template(template_prefix+'topview.html', wines=wines)
def show_brand(winetype_id): """ For a wine type, show the brands available. Also returns the most assigned rating for each brand. :param winetype_id: WineType id :return: JSON or HTML page """ from json_util import WineBrandListSchema session = current_app.config['db'] winetype = session.query(WineType).filter_by(id=winetype_id).one() # Counts the number of each rating counter = session\ .query(UserReview.winebrand_id, UserReview.rating, func.count(UserReview.rating).label('count'))\ .join(WineBrand)\ .join(WineType)\ .filter(WineBrand.winetype_id == winetype_id)\ .group_by(UserReview.winebrand_id, UserReview.rating)\ .order_by(UserReview.winebrand_id, desc('count'))\ .subquery() # Gets the max of the count maxer = session\ .query(counter.c.winebrand_id, func.max(counter.c.count).label('maxcount'))\ .group_by(counter.c.winebrand_id,counter.c.count)\ .subquery() # Gets the rating that matches max tops = session\ .query(UserReview.winebrand_id, UserReview.rating, func.count(UserReview.rating).label('topcount'))\ .join(maxer, maxer.c.winebrand_id == UserReview.winebrand_id)\ .group_by(UserReview.winebrand_id, UserReview.rating, maxer.c.maxcount)\ .having(func.count(UserReview.rating) == maxer.c.maxcount)\ .subquery() # In case multiple winners exists, choose the highest rating only_one = session\ .query(UserReview.winebrand_id, func.max(tops.c.rating).label('bestrating'))\ .outerjoin(tops, UserReview.winebrand_id == tops.c.winebrand_id)\ .group_by(UserReview.winebrand_id, tops.c.rating)\ .subquery() wines = session\ .query(WineBrand, only_one.c.bestrating.label('rating'))\ .outerjoin(only_one, WineBrand.id == only_one.c.winebrand_id)\ .filter(WineBrand.winetype_id == winetype_id)\ .order_by(func.lower(WineBrand.brand_name), WineBrand.vintage.asc()) if is_json_request(request): schema = WineBrandListSchema() return jsonify({"winetype": winetype.serialize, "items": [schema.dump(x).data for x in wines]}) else: return render_template(template_prefix+'brandsview.html', winetype=winetype, wines=wines)
def list_user_wines(user_id): """ Returns the current logged in user created wines :param user_id: User id to return wines for :return: JSON or HMTL page """ from json_util import WineBrandListSchema session = current_app.config['db'] # Counts the number of each rating counter = session\ .query(UserReview.winebrand_id, UserReview.rating, func.count(UserReview.rating).label('count'))\ .join(WineBrand)\ .join(WineType)\ .filter(WineBrand.user_id == user_id)\ .group_by(UserReview.winebrand_id, UserReview.rating)\ .order_by(UserReview.winebrand_id, desc('count'))\ .subquery() # Gets max for each rating maxer = session\ .query(counter.c.winebrand_id, func.max(counter.c.count).label('maxcount'))\ .group_by(counter.c.winebrand_id, counter.c.count)\ .subquery() # Select the top rating tops = session\ .query(UserReview.winebrand_id, UserReview.rating, func.count(UserReview.rating).label('topcount'))\ .join(maxer, maxer.c.winebrand_id == UserReview.winebrand_id)\ .group_by(UserReview.winebrand_id, UserReview.rating, maxer.c.maxcount)\ .having(func.count(UserReview.rating) == maxer.c.maxcount)\ .distinct()\ .subquery() # In case multiple top ratings exist, choose the highest rating only_one = session\ .query(UserReview.winebrand_id, func.max(tops.c.rating).label('bestrating'))\ .outerjoin(tops, UserReview.winebrand_id == tops.c.winebrand_id)\ .group_by(UserReview.winebrand_id, tops.c.rating)\ .subquery() wines = session\ .query(WineBrand, only_one.c.bestrating.label('rating'))\ .outerjoin(only_one, WineBrand.id == only_one.c.winebrand_id)\ .filter(WineBrand.user_id == user_id)\ .order_by(func.lower(WineBrand.brand_name), WineBrand.vintage.asc()) if is_json_request(request): schema = WineBrandListSchema() return jsonify(items=[schema.dump(x).data for x in wines]) else: return render_template(template_prefix+"user_wines_list.html", wines=wines)