def update_game(game_id): ''' updating database according to changes made in edit_game template, only available for admin ''' if session['admin']: DB_GAME_LIST.update( {'_id': ObjectId(game_id)}, { 'name': request.form.get('name'), 'publisher': request.form.get('publisher'), 'picture_link': request.form.get('picture_link'), 'wiki_link': request.form.get('wiki_link'), 'game_description': request.form.get('game_description') }) return redirect(url_for('admin_tab')) return render_template('no_login.html')
def top_games(): ''' rendering template for top_games ''' # initialization for all variables required for # upating the game keys required to show and update results total_ratings = DB_REVIEWS.aggregate([{ '$group': { '_id': '$game_name', 'count': { '$sum': '$rating' } } }]) total_reviews = DB_REVIEWS.aggregate([{ '$group': { '_id': '$game_name', 'count': { '$sum': 1 } } }]) grp_average = DB_GAME_LIST.aggregate([{ '$group': { '_id': '$name', 'avgAmount': { '$avg': { '$divide': ['$total_rating', '$total_reviews'] } } } }]) # updating game keys in order to show # correct averages, ratings and total ratings for rating in total_ratings: DB_GAME_LIST.update({'name': rating['_id']}, {'$set': { 'total_rating': rating['count'] }}) for review in total_reviews: DB_GAME_LIST.update({'name': review['_id']}, {'$set': { 'total_reviews': review['count'] }}) for grp in grp_average: if grp['avgAmount'] is None: continue average = round(grp['avgAmount'], 2) DB_GAME_LIST.update({'name': grp['_id']}, {'$set': { 'average': average }}) # sort by average, rating and total rating # in order to show results correctly on template best_avg = DB_GAME_LIST.find().sort('average', -1).limit(6) most_reviews = DB_GAME_LIST.find().sort('total_reviews', -1).limit(6) most_rating = DB_GAME_LIST.find().sort('total_rating', -1).limit(6) return render_template('top_games.html', best_avg=best_avg, most_reviews=most_reviews, most_rating=most_rating)