def create_team_year(): teams = Team.get_teams() years = Year.get_all_years() if request.method == 'POST': TeamYear(team=Team.get_by_school_name(request.form['team']).json(), year=Year.get_year_by_id(request.form['year']).json(), conference=request.form['conf'], wins=request.form['wins'], losses=request.form['losses'], conf_wins=request.form['conf_wins'], conf_losses=request.form['conf_losses'], ap_rank=request.form['rank']).save_to_mongo() return render_template('team_years/create_team_year.jinja2', teams=teams, years=years)
def get_all_prior_games_in_current_year(game_num): """ initially get all user_games in the current year. Only append them to a new list if they have a game_num that is less than the one being queried :param game_num: game number from Game object that states up to which game points will be calced :return: list of games up to game_num """ user_games_prior = [] current_year_user_games = UserGame.get_usergames_by_date( Year.get_current_year().start_date, Year.get_current_year().end_date) for ug in current_year_user_games: if ug.game.game_num <= game_num: user_games_prior.append(ug) return user_games_prior
def create_year(): if request.method == 'POST': start_date = datetime.strptime(request.form['start_date'], "%m/%d/%Y") end_date = datetime.strptime(request.form['end_date'], "%m/%d/%Y") year_label = request.form['year_label'] Year(start_date, end_date, year_label).save_to_mongo() return render_template('years/create_year.jinja2')
def get_next_game(cls): games = [cls(**elem) for elem in Database.find_and_sort(GameConstants.COLLECTION, {"year": Year.get_current_year()._id}, [("date", 1)])] for game in games: if game.date > datetime.now(): return game
def __init__(self, team, year, wins=0, losses=0, conf_wins=0, conf_losses=0, ap_rank=None, conference=None, _id=None): self.team = Team.get_by_school_name(team['school_name']) self.year = Year.get_year_by_id(year['_id']) self.wins = wins self.losses = losses self.conf_wins = conf_wins self.conf_losses = conf_losses self.ap_rank = ap_rank self.conference = conference self._id = uuid.uuid4().hex if _id is None else _id
def user_default_values(): alerts = {} for a in AlertConstants.ALERTS: alerts[a] = 'On' attendance = {} games = Game.get_games_by_year(Year.get_current_year()._id) for i in games: if i.location.city == 'Blacksburg': attendance[i.game_num] = 'Yes' else: attendance[i.game_num] = 'No' return alerts, attendance
def load_game_details(): """ This function will scrape the hokiesports website for a span called "schedule-opponent". From that tag, several elements of a game can be determined and those functions are called within this function. :return: None """ # start date of current year object will determine the schedule pulled from hokiesports.com link = "https://hokiesports.com/schedule.aspx?path=football" headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/5.0'} request = requests.get(link, headers=headers) content = request.content soup = BeautifulSoup(content, "html.parser") opponents = soup.find_all('li', {'class': 'sidearm-schedule-game'}) for o in opponents: # get tag that has opponent name if o.find("span", {"class": "sidearm-schedule-game-opponent-name"}): opponent_name = o.find("span", {"class": "sidearm-schedule-game-opponent-name"}) # get game with opponent # ignore spring game and other games not defined if Database.DATABASE[GameConstants.COLLECTION].find_one( {"$or": [{"home_team": opponent_name.text.strip()}, {"away_team": opponent_name.text.strip()}], "year": Year.get_current_year()._id}): game = Game.get_game_by_opponent(opponent_name.text.strip(), Year.get_current_year()._id) if game.date > datetime.now(): unformatted_time_tag = o.find("div", {"class": "sidearm-schedule-game-opponent-date"}) \ .find("span").find_next_sibling() game.TV = Game.get_game_tv(o) game.time = Game.get_game_time(unformatted_time_tag) game.theme = Game.get_game_theme(o) if game.home_score == 0 and game.away_score == 0 and game.date < datetime.now() \ and game.score_updated_on is None: game.away_score, game.home_score = Game.get_game_score(o, game) game.score_updated_on = datetime.now() game.save_to_mongo()
def get_all_prior_games_in_current_year(cls): """ Get all games in current year by descending order. Iterate through loop and return all games that have already happened. :return: return list of all games that have happened """ prior_games = [] games = [cls(**elem) for elem in Database.find_and_sort(GameConstants.COLLECTION, {"year": Year.get_current_year()._id}, [("date", -1)])] for g in games: if g.date < datetime.now(): prior_games.append(g) return prior_games
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 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 __init__(self, game_num, home_team, away_team, year, date=None, time='TBA', location=None, stadium=None, theme=None, hht_theme=None, TV=None, home_score=0, away_score=0, score_updated_on=None, _id=None): self.game_num = game_num self.home_team = TeamYear.get_by_hokie_sports_name_and_year(home_team, year) self.away_team = TeamYear.get_by_hokie_sports_name_and_year(away_team, year) self.location = Location.get_location_by_id( Team.get_by_school_name(home_team).location._id) if location is None else Location.get_location_by_id( location['_id']) self.stadium = Team.get_by_school_name(home_team).stadium if stadium is None else stadium self.year = Year.get_year_by_id(year) self.date = date self.time = 'TBA' if time == 'TBA' else datetime.strftime(datetime.strptime(time, "%I:%M %p"), "%I:%M %p") self.hht_theme = hht_theme self.theme = theme self.TV = TV self.home_score = home_score self.away_score = away_score self.score_updated_on = score_updated_on self._id = uuid.uuid4().hex if _id is None else _id
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 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)
def admin_schedule(): return render_template("games/admin_schedule.jinja2", games=Game.get_games_by_year( Year.get_current_year()._id))