def get_leaderboard(): """ This function will get all games up to and including the game that is selected for the leaderboard. The function receives the game._id of the game to be viewed and a dict is built to sum up the total of points accumulated by each user :return: leaderboard template with unzipped lists of leaders and the totals, as well as the user game details for the game passed into the function and pass through of the game id """ games_prior = Game.get_all_prior_games_in_current_year() if games_prior: if request.method == 'GET': max_game = games_prior[0] elif request.method == 'POST': max_game = Game.get_game_by_id(request.form['game_view']) user_games_prior = UserGame.get_all_prior_games_in_current_year(max_game.game_num) user_totals = defaultdict(int) for ug in user_games_prior: user_totals[ug.user._id] += ug.total_points user_totals_sorted = sorted(user_totals.items(), key=operator.itemgetter(1), reverse=True) leaders = [x[0] for x in user_totals_sorted] points = [x[1] for x in user_totals_sorted] latest_user_games = UserGame.get_attendance_by_game(max_game._id) return render_template("user_games/leaderboard.jinja2", leaders=leaders, points=points, latest_user_games=latest_user_games, games_prior=games_prior, max_game=max_game) else: return render_template("user_games/no_games_played.jinja2")
def admin_edit_scores(game_id): if request.method == 'POST': usergame = UserGame.get_usergame_by_id(request.form['usergame']) usergame.home_score = request.form['home_score' + usergame._id] usergame.away_score = request.form['away_score' + usergame._id] usergame.admin_enter = 'Yes' usergame.score_updated_on = datetime.datetime.now() usergame.save_to_mongo() scores = UserGame.get_attendance_by_game(game_id) return render_template("user_games/edit_scores.jinja2", scores=scores)
def user_dashboard(): year = Year.get_current_year() if request.method == 'POST': attendance = UserGame.get_attendance_by_user(session['user'], year.start_date, year.end_date) for a in attendance: a.home_score = request.form['home_score' + str(a.game.game_num)] a.away_score = request.form['away_score' + str(a.game.game_num)] a.attendance = request.form['attendance' + str(a.game.game_num)] # since user is saving all scores, only update the updated_on date for those games that are not readonly if a.game.date > datetime.datetime.now(): a.admin_enter = 'No' a.score_updated_on = datetime.datetime.now() a.save_to_mongo() attendance = UserGame.get_attendance_by_user(session['user'], year.start_date, year.end_date) return render_template("users/dashboard.jinja2", attendance=attendance, cutoff=datetime.datetime.now()+datetime.timedelta(days=2))
def detail(game_id): user = session['user'] this_game = Game.get_game_by_id(game_id) # format the date for the detail screen this_game.date = this_game.date.strftime('%B %d') # getting attendance for each type to pass along to template yes_attendance = UserGame.get_attendance_by_game_and_status(game_id, 'Yes') maybe_attendance = UserGame.get_attendance_by_game_and_status( game_id, 'Maybe') no_attendance = UserGame.get_attendance_by_game_and_status(game_id, 'No') # get preview for game preview = Preview.get_preview_by_game(game_id) # get food for game food_for_game = Food.get_food_by_game(game_id) # get have_tickets for game have_tickets_for_game = HaveTicket.get_havetickets_by_game(game_id) # get want_tickets for game want_tickets_for_game = WantTicket.get_wanttickets_by_game(game_id) # get convo for game convos = GameConversation.get_convo_by_game(game_id) return render_template("games/game_detail.jinja2", game=this_game, preview=preview, yes_attendance=yes_attendance, maybe_attendance=maybe_attendance, no_attendance=no_attendance, food_for_game=food_for_game, have_tickets_for_game=have_tickets_for_game, want_tickets_for_game=want_tickets_for_game, convos=convos, user=user, DoD=(date.today() - date(2004, 11, 28)).days)
def user_administration(): if request.method == 'POST': user = User(f_name=request.form['fname'], l_name=request.form['lname'], email=request.form['email'], prognosticator=request.form['prognosticator'], admin_created='Yes' ) alerts, attendance = User.user_default_values() user.insert_new_user() for alert in alerts: new_alert = Alert(user._id, alert, alerts[alert]) new_alert.save_to_mongo() for na in attendance: new_attendance = UserGame(user=user.json(), game=Game.get_game_by_num(na, Year.get_current_year()._id)._id, attendance=attendance[na], home_score=0, away_score=0, game_date=0) new_attendance.save_to_mongo() users = User.get_all_users() return render_template('users/admin_dashboard.jinja2', users=users)
def admin_delete_user(user_id): user = User.get_user_by_id(user_id) alerts = Alert.get_alerts_by_user(user_id) attendance = UserGame.get_all_attendance_by_user(user_id) user.remove_user() for alert in alerts: alert.remove_alerts() for atten in attendance: atten.remove_user_games() users = User.get_all_users() return render_template('users/admin_dashboard.jinja2', users=users)
def new_user(): """ :return: """ if request.method == 'POST': email = request.form['email'] try: if User.new_user_valid(email, request.form['pword'], request.form['confirmpword']): fname = request.form['fname'] lname = request.form['lname'] pword = Utils.hash_password(request.form['pword']) phone = request.form['phone'] location = request.form['location'] if User.check_offline_user_exist(email) is not None: user = User.get_user_by_email(email) user.f_name = fname user.l_name = lname user.password = pword user.admin_created = 'No' user.prognosticator = 'Yes' user.admin = 'No' user.phone = phone user.location = location user.created_on = datetime.datetime.utcnow() user.save_to_mongo() else: user = User(fname, lname, email, pword, phone=phone, location=location, created_on=datetime.datetime.utcnow()) user.save_to_mongo() alerts, attendance = User.user_default_values() for alert in alerts: Alert(user=user._id, alert=alert, yes_no='On').save_to_mongo() for na in attendance: UserGame(user=user.json(), game=Game.get_game_by_num(na, Year.get_current_year()._id)._id, attendance=attendance[na], home_score=0, away_score=0, game_date=0).save_to_mongo() session['user'] = user._id session['useradmin'] = user.admin return redirect(url_for('alerts.manage_alerts')) except UserErrors.UserError as e: return render_template("users/new_user.jinja2", error=e.message) else: return render_template("users/new_user.jinja2")
def create_game(): if request.method == 'POST': new_game = Game(game_num=request.form['game_num'], away_team=request.form['away_team'], home_team=request.form['home_team'], year=request.form['year'], location=Location.get_location_by_id( request.form['location']).json(), stadium=request.form['stadium'], date=datetime.strptime(request.form['date'], "%m/%d/%Y")) new_game.save_to_mongo() users = User.get_all_users() if new_game.location.city == 'Blacksburg': attendance = 'Yes' else: attendance = 'No' for u in users: UserGame(user=u.json(), game=new_game._id, attendance=attendance, game_date=None, home_score=0, away_score=0).save_to_mongo() years = Year.get_all_years() teams = Team.get_teams() locations = Location.get_all_locations() stadiums = Game.get_all_stadiums() stadiums.sort() return render_template("games/create_game.jinja2", teams=teams, locations=locations, stadiums=stadiums, years=years)
from src.models.user_games.user_game import UserGame from src.common.database import Database __author__ = 'hooper-p' Database.initialize() UserGame.send_reminder()
__author__ = 'hooper-p' from src.common.database import Database from src.models.user_games.user_game import UserGame Database.initialize() UserGame.update_user_points()