def create_show(): if session['logged'] == False: return "You need to log in first!" elif session['position'] != "admin": return "Only admins may do this" c = json.loads(request.form['values']) #c = request.get_json(silent=True) print(c) show = entities.Show(name=c['name'], imageurl=c['imageurl'], description=c['description'], seasons=c['seasons'], episodes=c['episodes'], votes=str(0), rating=str(0), rank=str(1)) sessiondb = db.getSession(engine) sessiondb.add(show) sessiondb.commit() #ORDER SHOWS(BASICALLY PUT NEW SHOW AT THE END) shows = sessiondb.query(entities.Show) rankedData = rankShows(shows) i = 0 for item in rankedData: i = i + 1 for show in shows: if show.name == item['name']: show.rank = i sessiondb.commit() return 'Created Show'
def add(): if session['logged'] == False: return "You need to log in first!" session['askingData'] = True data = request.form showName = data['showName'] rating = data['star'] sessiondb = db.getSession(engine) shows = sessiondb.query(entities.Show) show = sessiondb.query( entities.Show).filter(entities.Show.name == showName).first() user_id = str(session['user_id']) userRatings = sessiondb.query( entities.Vote).filter(entities.Vote.voterId == user_id) ratedShows = [] for userRating in userRatings: if userRating != None: ratedShows.append(userRating.showId) #if user hasnt rated this show yet if str(show.id) not in ratedShows: if (show.votes == "0"): show.votes = "1" show.rating = str(int(rating)) else: show.rating = str( round( float(show.rating) + (float(rating) - float(show.rating)) / (int(show.votes) + 1), 12)) show.votes = str(int(show.votes) + 1) sessiondb.commit() vote = entities.Vote(voterId=user_id, showId=str(show.id), rating=str(rating)) sessiondb.add(vote) sessiondb.commit() #if user has already rated this show else: if (show.votes == "1"): show.rating = str(int(rating)) else: #subtract old value value = sessiondb.query(entities.Vote).filter( entities.Vote.voterId == user_id, entities.Vote.showId == str(show.id)).first() value = float(value.rating) average = float(show.rating) nbValues = float(show.votes) show.rating = str( round(((average * nbValues) - value) / (nbValues - 1), 12)) show.votes = str(int(show.votes) - 1) sessiondb.commit() #add new value show.rating = str( round( float(show.rating) + (float(rating) - float(show.rating)) / (int(show.votes) + 1), 12)) show.votes = str(int(show.votes) + 1) sessiondb.commit() oldVote = sessiondb.query(entities.Vote).filter( entities.Vote.voterId == user_id, entities.Vote.showId == str(show.id)).first() oldVote.rating = rating sessiondb.commit() #RANK SHOWS rankedData = rankShows(shows) i = 0 for item in rankedData: i = i + 1 for show in shows: if show.name == item['name']: show.rank = i sessiondb.commit() return redirect("/s/" + format_to_file_name(showName))
def add_mobile(): if request.headers.get("User-Agent") != "android": return "what are you doing here?" else: data = request.form showName = data['showName'] rating = data['rating'] sessiondb = db.getSession(engine) shows = sessiondb.query(entities.Show) show = sessiondb.query( entities.Show).filter(entities.Show.name == showName).first() user_id = int(data['user_id']) user = sessiondb.query( entities.User).filter(entities.User.id == user_id).first() userRatings = sessiondb.query( entities.Vote).filter(entities.Vote.voterId == user_id) ratedShows = [] for userRating in userRatings: if userRating != None: ratedShows.append(userRating.showId) if str(show.id) not in ratedShows: print("You havent vote yet") user.votes = str(int(user.votes) + 1) if (show.votes == "0"): show.votes = "1" show.rating = str(int(round(float(rating)))) else: show.rating = str( round( float(show.rating) + (float(rating) - float(show.rating)) / (int(show.votes) + 1), 12)) show.votes = str(int(show.votes) + 1) sessiondb.commit() vote = entities.Vote(voterId=user_id, showId=str(show.id), rating=str(rating)) sessiondb.add(vote) sessiondb.commit() #return "You haven't voted yet" else: if (show.votes == "1"): show.rating = str(int(round(float(rating)))) else: # subtract old value value = sessiondb.query(entities.Vote).filter( entities.Vote.voterId == user_id, entities.Vote.showId == str(show.id)).first() value = float(value.rating) average = float(show.rating) nbValues = float(show.votes) show.rating = str( round(((average * nbValues) - value) / (nbValues - 1), 12)) show.votes = str(int(show.votes) - 1) sessiondb.commit() # add new value show.rating = str( round( float(show.rating) + (float(rating) - float(show.rating)) / (int(show.votes) + 1), 12)) show.votes = str(int(show.votes) + 1) sessiondb.commit() oldVote = sessiondb.query(entities.Vote).filter( entities.Vote.voterId == user_id, entities.Vote.showId == str(show.id)).first() oldVote.rating = rating sessiondb.commit() print("You already voted") #return "You already voted" rankedData = rankShows(shows) i = 0 for item in rankedData: i = i + 1 for show in shows: if show.name == item['name']: show.rank = i sessiondb.commit() return "You voted " + showName + " with " + rating + " stars"