def awards_home(): ''' This view page list all awards in awards table. This page doesn't allow editing. ''' conn, cur = getDb() awards = award.Awards(conn, cur) # limit, page and order args # required for each table page limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' sortby={'attr':'name', 'property':'asc'} # check search value if 'name' in request.args: search_name = request.args['name'] award_list, total = awards.get_awards_search_by('name', search_name, limit=limit, offset=offset) else: award_list, total = awards.get_awards(limit=limit, offset=offset) return render_template('awards_home.html', awardtable=award.awardtable, awards=award_list, total=total, limit=limit, page=page, sortby=sortby)
def teamrosters_home(): conn, cur = getDb() teamrosters = teamroster.Teamrosters(conn, cur) print('TEAMROSTERS PAGE') if request.method == 'GET': print ('GET REQUEST', request.args) limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit # check search value if 'team_name' in request.args: search_name = request.args['team_name'] teamroster_list,total = teamrosters.get_teamrosters_search_by('name', search_name, limit, offset) else: teamroster_list, total = teamrosters.get_teamrosters(limit, offset) return render_template('teamrosters_home.html', teamrostertable=teamroster.teamrostertable, teamrosters=teamroster_list, total=total, limit=limit, page=page) return render_template('leagues_home.html', leaguetable=league.leaguetable, leagues=l, total=total, limit=limit, page=page, sortby=sortby)
def teamroster_page(): if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() teamrosters = teamroster.Teamrosters(conn, cur) players = player.Players(conn, cur) teams = team.Teams(conn, cur) print('TEAMROSTERS PAGE') if request.method == 'GET': print ('GET REQUEST', request.args) limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit print('page:',page,'limit',limit,'offset',offset) teamroster_list, total = teamrosters.get_teamrosters(limit, offset) player_list, pp = players.get_players(100,0) team_list,tp = teams.get_teams(100,0) return render_template('teamrosters.html', teamrostertable=teamroster.teamrostertable, teamrosters=teamroster_list, players=player_list, teams=team_list, total=total, limit=limit, page=page) elif request.method == 'POST': print('ADD TEAMROSTER') player_id = request.form['player'] team_id = request.form['team'] limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit teamroster_obj = teamroster.Teamroster(player_id, team_id) teamrosters.add_teamroster(teamroster_obj) teamroster_list, total= teamrosters.get_teamrosters(limit,offset) player_list,pp = players.get_players(100,0) team_list,tp = teams.get_teams(100,0) return render_template('teamrosters.html', teamrostertable=teamroster.teamrostertable, teamrosters=teamroster_list, players=player_list, teams=team_list, total=total, limit=limit, page=page) elif request.method == 'DEL': print ('DELETE REQUEST:TEAMROSTERS PAGE') print (request.form) idlist = request.form.getlist('ids[]') print ('IDS: ', idlist) if idlist == []: try: idlist = [request.form['id']] print ('IDS: ', idlist) except: return json.dumps({'status':'OK', 'idlist':idlist}) print ('IDS: ', idlist) print(json.dumps({'status':'OK', 'idlist':idlist})) for _id in idlist: print (_id) teamrosters.delete_teamroster(_id) return json.dumps({'status':'OK', 'idlist':idlist})
def teamroster_from_id(teamroster_id): if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() teamrosters = teamroster.Teamrosters(conn, cur) players=player.Players(conn,cur) teams=team.Teams(conn,cur) if request.method == 'GET': l = teamrosters.get_teamroster(teamroster_id) if l: return json.dumps({'status':'OK', 'teamroster':l.getAttrs()}) else: return json.dumps({'status':'FAILED'}) elif request.method == 'POST': print("POST METHOD REQUEST") lid = request.form['id'] player_id = request.form['player'] team_id = request.form['team'] limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit teamroster_obj = teamroster.Teamroster(player_id,team_id) teamrosters.update_teamroster(lid,teamroster_obj) teamroster_list, total= teamrosters.get_teamrosters(limit,offset) player_list,pp = players.get_players(100,0) team_list,tp = teams.get_teams(100,0) return render_template('teamrosters.html', teamrostertable=teamroster.teamrostertable, teamrosters=teamroster_list, players=player_list, teams=team_list, total=total, limit=limit, page=page)
def search_award_stat(key): '''Routing function for search award_stat by its name. *GET request* renders the award_stats.html with award_stat comes from the search result. ''' if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() award_stats = award_stat.AwardStats(conn, cur) awards = award.Awards(conn, cur) players = player.Players(conn, cur) seasons = season.Seasons(conn, cur) limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' offset = page*limit award_stat_list, total = award_stats.get_award_stats_search_by('name', key, limit, offset) award_list, ta = awards.get_awards(limit, offset) player_list,tp = players.get_players(100,0) # get list object season_list = seasons.get_seasons() # get list object sortby={'attr':'name', 'property':'asc'} return render_template('award_stats.html', award_stattable=award_stat.award_stattable, award_stats=award_stat_list, awards=award_list, seasons=season_list, players=player_list, total=total, limit=limit, page=page, sortby=sortby)
def search_coach(key): '''Routing function for search coach by its name. *GET request* renders the countries.html with coaches comes from the search result. ''' if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() coaches = coach.Coaches(conn, cur) countries = country.Countries(conn, cur) limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit print('page:',page,'limit',limit,'offset',offset) sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' l, total_coaches = coaches.get_coaches_search_by('name', key, limit, offset) c, total_countries = countries.get_countries() sortby={'attr':'name', 'property':'asc'} return render_template('coaches.html', coachtable=coach.coachtable, coaches=l, countries=c, total=total_coaches, limit=limit, page=page)
def coaches_home(): ''' Routing function for coaches-home page. This view page lists all coaches in coaches table. This page doesn't allow editing. ''' conn, cur = getDb() coaches = coach.Coaches(conn, cur) countries = country.Countries(conn, cur) print('coaches PAGE') if request.method == 'GET': # handle GET request limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit print('page:',page,'limit',limit,'offset',offset) sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' l, total_coaches = coaches.get_coaches() sortby={'attr':'name', 'property':'asc'} # check search value if 'name' in request.args: search_name = request.args['name'] l, total_coaches = coaches.get_coaches_search_by('name', search_name, limit, offset) else: l, total_coaches = coaches.get_coaches(limit, offset) return render_template('coaches_home.html', coachtable=coach.coachtable, coaches=l, total=total_coaches, limit=limit, page=page)
def countries_home(): ''' Routing function for countries-home page. This view page lists all countries in countries table. This page doesn't allow editing. ''' conn, cur = getDb() countries = country.Countries(conn, cur) print('countries PAGE for normal user') # limit, page and order args # required for each table page if pagination used ! limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' # check search value if 'name' in request.args: search_name = request.args['name'] c, total = countries.get_countries_by('name', search_name, limit=limit, offset=offset) else: c, total = countries.get_countries(limit=limit, offset=offset) return render_template('countries_home.html', countrytable=country.countrytable, countries=c, total=total, limit=limit, page=page, sortby=sortby)
def updateUser(): """Routing function for updating user. This url allows only POST request. Updates the user which attributes recieved from *request.form* and returns the JSON object. :returns: status and error :rtype: JSON object """ if "username" not in session: return abort(403) conn, cur = getDb() username = request.form["username"] if session["username"] != username: return abort(403) password = request.form["password"] passwordnew = request.form["passwordnew"] users = user.Users(conn, cur) muser = users.get_user(username) if password == muser.password: muser.password = passwordnew try: users.update_user(username, muser) return json.dumps({"status": "OK", "user": username, "pass": passwordnew}) except: error = sys.exc_info()[0] return json.dumps({"status": "FAILED", "error": error}) else: error = "The password you entered is wrong!" return json.dumps({"status": "FAILED", "error": error})
def signin(): """Routing function for signin page.""" conn, cur = getDb() error = None roles = None if request.method == "POST": username = request.form["username"] password = request.form["password"] if login_success(username, password): error = "Logged in!" cur.execute("SELECT role, lastlogin FROM users WHERE username='******';" % username) role, lastlogin = cur.fetchone() g.role = role g.lastlogin = lastlogin session["username"] = request.form["username"] now = getCurrTimeStr() cur.execute("UPDATE users SET lastlogin='******' WHERE username='******'" % (now, username)) cur.execute("UPDATE users SET online=TRUE WHERE username='******'" % username) conn.commit() else: error = "Invalid username or password!" if "username" in session: username = session["username"] cur.execute("SELECT role, lastlogin FROM users WHERE username='******';" % username) role, lastlogin = cur.fetchone() g.role = role g.lastlogin = lastlogin return render_template("signin.html")
def signup(): """Routing function for signup page. It allows only *POST* request. If the valid values are gotten from the request.form then new user is created and inserted to the db. """ if request.method == "POST": try: conn, cur = getDb() username = request.form["username"] password = request.form["password"] email = str(request.form["email"]) role = request.form["role"] regtime = getCurrTimeStr() lastlogin = regtime online = False usr = user.User(username, password, email, role, lastlogin, regtime, online) users = user.Users(conn, cur) users.add_user(usr) return redirect(url_for("admin")) except IntegrityError: conn.rollback() roles = None error = "This username already registered." return render_template("adminpanel.html", error=error, roles=roles) else: conn.rollback() error = sys.exc_info()[0] roles = None return render_template("adminpanel.html", error=error, roles=roles)
def schedule_home(): conn, cur = getDb() schedules = schedule.Schedules(conn, cur) teams = team.Teams(conn, cur) seasons = season.Seasons(conn, cur) leagues = league.Leagues(conn, cur) print("SCHEDULES PAGE") if request.method == "GET": # handle GET request print("GET REQUEST", request.args) limit = int(request.args["limit"]) if "limit" in request.args else 10 page = int(request.args["page"]) if "page" in request.args else 0 offset = page * limit print("page:", page, "limit", limit, "offset", offset) sortby = request.args["sortby"] if "sortby" in request.args else "name" order = request.args["order"] if "order" in request.args else "asc" if "name" in request.args: search_name = request.args["name"] l, total = schedules.get_schedules_search_by("name", search_name, limit=limit, offset=offset) else: l, total = schedules.get_schedules(limit=limit, offset=offset) return render_template( "schedules_home.html", scheduletable=schedule.scheduletable, schedules=l, total=total, limit=limit, page=page, sortby=sortby, )
def leagues_home(): ''' Routing function for leagues-home page. This view page lists all leagues in leagues table. This page doesn't allow editing. ''' conn, cur = getDb() leagues = league.Leagues(conn, cur) countries = country.Countries(conn, cur) # limit, page and order args # required for each table page limit = int(request.args['limit']) if 'limit' in request.args else 10 page = int(request.args['page']) if 'page' in request.args else 0 offset = page*limit sortby = request.args['sortby'] if 'sortby' in request.args else 'name' order = request.args['order'] if 'order' in request.args else 'asc' sortby={'attr':'name', 'property':'asc'} # check search value if 'name' in request.args: search_name = request.args['name'] l, total = leagues.get_leagues_search_by('name', search_name, limit=limit, offset=offset) else: l, total = leagues.get_leagues(limit=limit, offset=offset) return render_template('leagues_home.html', leaguetable=league.leaguetable, leagues=l, total=total, limit=limit, page=page, sortby=sortby)
def search_schedule(key): if "username" not in session: return render_template("error.html", err_code=401) conn, cur = getDb() schedules = schedule.Schedules(conn, cur) teams = team.Teams(conn, cur) seasons = season.Seasons(conn, cur) leagues = league.Leagues(conn, cur) limit = int(request.args["limit"]) if "limit" in request.args else 10 page = int(request.args["page"]) if "page" in request.args else 0 sortby = request.args["sortby"] if "sortby" in request.args else "name" order = request.args["order"] if "order" in request.args else "asc" offset = page * limit schedule_list, total = schedules.get_schedules_search_by("name", key, limit, offset) team_list, tp = teams.get_teams(100, 0) # get list object season_list = seasons.get_seasons() # get list object league_list, tk = leagues.get_leagues(100, 0) sortby = {"attr": "name", "property": "asc"} return render_template( "schedules.html", scheduletable=schedule.scheduletable, schedules=schedule_list, seasons=season_list, teams=team_list, leagues=league_list, total=total, limit=limit, page=page, sortby=sortby, )
def view_country(country_name): '''Routing function for country info page. This view renders the country_page.html with the general information and related table statistics for given country. :param country_name: name of the country, string ''' conn, cur = getDb() countries = country.Countries(conn, cur) c = countries.get_country_name(country_name) if c is None: # return not found error return render_template('error.html', err_code=404) # else render country page with required args leagues = league.Leagues(conn, cur) league_list, total = leagues.get_leagues_by('country_id', c._id) league_dict = {'leagues':league_list, 'total': total } coaches = coach.Coaches(conn, cur) coach_list, total = coaches.get_coaches_by('country_id', c._id) coaches_dict = {'coaches':coach_list, 'total': total } return render_template('country_page.html', country=c, league_dict=league_dict, player_dict=None, coach_dict=coaches_dict)
def coach_from_id(lid): '''Routing function for getting coach from its id. *GET request* returns JSON object. *POST request* updates coach which has id equal to the *lid* with the values recieved from the request.form. After all it renders coaches.html :param lid: id of the coach, int ''' if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() coaches = coach.Coaches(conn, cur) countries = country.Countries(conn, cur) if request.method == 'GET': # handle GET request # return json object l= coaches.get_coach(lid) if l: return json.dumps({'status':'OK', 'coach':l.getAttrs()}) else: return json.dumps({'status':'FAILED'}) elif request.method == 'POST': # handle POST request # UPDATE item print("POST METHOD REQUEST") lid = request.form['id'] name = request.form['name'] surname = request.form['surname'] country_id = request.form['country'] coach_img = request.files['file'] limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit order = request.form['sortby'] if 'sortby' in request.form else 'name' order = request.form['order'] if 'order' in request.form else 'asc' lg = coach.Coach(name, surname, country_id) coaches.update_coach(lid, lg) # save image to path # image path should start with appname: # real_save_path = 'final4' + <save_path> # because app runs in the upper folder. if coach_img: # get generated image path for new coach object save_path = lg.img_path(lid) coach_img.save(app.config['APP_FOLDER']+save_path) l, total_coaches = coaches.get_coaches() c, total_countries = countries.get_countries() sortby={'attr':'name', 'property':'asc'} return render_template('coaches.html', coachtable=coach.coachtable, coaches=l, countries=c, total=total_coaches, limit=limit, page=page)
def search_season(key): if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() seasons = season.Seasons(conn, cur) result = seasons.get_seasons_by(key, 'name') return render_template('seasons.html', seasontable=season.seasontable, seasons=result)
def login_success(username, password): conn, cur = getDb() cur.execute("SELECT * FROM users WHERE username='******' AND password='******'" % (username, password) ) user = cur.fetchone() if user: return True return False
def users(): """Routing function for users page.""" conn, cur = getDb() usr = user.Users(conn, cur) users = usr.get_users() if "username" in session: g.role = "admin" return render_template("users.html", users=users, error=None, usertable=user.usertable)
def schedule_from_id(schedule_id): if "username" not in session: return render_template("error.html", err_code=401) conn, cur = getDb() schedules = schedule.Schedules(conn, cur) teams = team.Teams(conn, cur) seasons = season.Seasons(conn, cur) leagues = league.Leagues(conn, cur) if request.method == "GET": # handle GET request schedule_obj = schedules.get_schedule(schedule_id) if schedule_obj: return json.dumps({"status": "OK", "schedule": schedule_obj.getAttrs()}) else: return json.dumps({"status": "FAILED"}) elif request.method == "POST": # handle POST request print("POST METHOD REQUEST") schedule_id = request.form["id"] date = request.form["date"] saloon = request.form["saloon"] score1 = request.form["score1"] score2 = request.form["score2"] state = request.form["state"] team1_id = request.form["team1_name"] team2_id = request.form["team2_name"] league_id = request.form["league_name"] season_id = request.form["season_year"] # limit: number of result showing each page # offset: selectedpage x limit limit = int(request.form["limit"]) if "limit" in request.form else 10 page = int(request.form["page"]) if "page" in request.form else 0 offset = page * limit order = request.form["sortby"] if "sortby" in request.form else "name" order = request.form["order"] if "order" in request.form else "asc" schedule_obj = schedule.Schedule(team1_id, team2_id, season_id, league_id, date, saloon, score1, score2, state) schedules.update_schedule(schedule_id, schedule_obj) schedule_list, total = schedules.get_schedules(limit, offset) team_list, tp = teams.get_teams(100, 0) # get list object season_list = seasons.get_seasons() # get list object league_list, tk = leagues.get_leagues(100, 0) sortby = {"attr": "name", "property": "asc"} return render_template( "schedules.html", scheduletable=schedule.scheduletable, schedules=schedule_list, seasons=season_list, teams=team_list, leagues=league_list, total=total, limit=limit, page=page, sortby=sortby, )
def profile(username): """Routing function for user profile page. Renders *templates/profile.html* for user which has given username. :param username: username string """ conn, cur = getDb() usr = user.Users(conn, cur) puser = usr.get_user(username) return render_template("profile.html", user=puser, usertable=user.usertable)
def get_match_from_id(match_id): '''Routing function for getting match from its id. *GET request* returns JSON object. *POST request* updates match which has id equal to the *lid* with the values recieved from the request.form. After all it renders matches.html :param lid: id of the match, int ''' conn, cur = getDb() matches = match.Matches(conn, cur) schedules = schedule.Schedules(conn, cur) if request.method == 'GET': # handle GET request match_obj= matches.get_match(match_id) if match_obj: return json.dumps({'status':'OK', 'match':match_obj.getAttrs()}) else: return json.dumps({'status':'FAILED'}) elif request.method == 'POST': # handle POST request print("POST METHOD REQUEST") print(request.form) schedule_id = request.form['schedule'] #FK #T1_name = request.form['T1_name'] #T2_name = request.form['T2_name'] T1_3PT = request.form['T1_3PT'] T1_2PT = request.form['T1_2PT'] T1_block = request.form['T1_block'] T1_reb = request.form['T1_reb'] T1_rate = request.form['T1_rate'] T2_3PT = request.form['T2_3PT'] T2_2PT = request.form['T2_2PT'] T2_block = request.form['T2_block'] T2_reb = request.form['T2_reb'] T2_rate = request.form['T2_rate'] # limit: number of result showing each page # offset: selectedpage x limit limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit order = request.form['sortby'] if 'sortby' in request.form else 'name' order = request.form['order'] if 'order' in request.form else 'asc' match_obj = match.Match(schedule_id, T1_3PT, T1_2PT, T1_block, T1_reb, T1_rate, T2_3PT, T2_2PT, T2_block, T2_reb, T2_rate) matches.update_match(match_id, match_obj) match_list, total = matches.get_matches(limit, offset) schedule_list, ts = schedules.get_schedules() # get list object sortby={'attr':'name', 'property':'asc'} return render_template('matches.html', matchtable=match.matchtable, matches=match_list, schedules=schedule_list, total=total, limit=limit, page=page, sortby=sortby)
def seasons_home(): conn, cur = getDb() seasons = season.Seasons(conn, cur) print('SEASONS PAGE') if request.method == 'GET': if 'name' in request.args: search_name = request.args['name'] l = seasons.get_seasons_by(search_name, 'name') #l = seasons.get_seasons_search_by('name', search_name) else: l = seasons.get_seasons() return render_template('seasons_home.html', seasontable=season.seasontable, seasons=l)
def award_stat_from_id(award_stat_id): '''Routing function for getting award_stat from its id. *GET request* returns JSON object. *POST request* updates award_stat which has id equal to the *lid* with the values recieved from the request.form. After all it renders award_stats.html :param lid: id of the award_stat, int ''' if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() award_stats = award_stat.AwardStats(conn, cur) awards = award.Awards(conn, cur) players = player.Players(conn, cur) seasons = season.Seasons(conn, cur) if request.method == 'GET': # handle GET request award_stat_obj= award_stats.get_award_stat(award_stat_id) if award_stat_obj: return json.dumps({'status':'OK', 'award_stat':award_stat_obj.getAttrs()}) else: return json.dumps({'status':'FAILED'}) elif request.method == 'POST': # handle POST request print("POST METHOD REQUEST") award_stat_id = request.form['id'] award_id = request.form['award_name'] player_id = request.form['player_name'] season_id = request.form['season_year'] # limit: number of result showing each page # offset: selectedpage x limit limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit order = request.form['sortby'] if 'sortby' in request.form else 'name' order = request.form['order'] if 'order' in request.form else 'asc' award_stat_obj = award_stat.AwardStat(award_id, player_id, season_id) award_stats.update_award_stat(award_stat_id, award_stat_obj) award_stat_list, total = award_stats.get_award_stats(limit, offset) award_list, ta = awards.get_awards(limit, offset) player_list,tp = players.get_players(100,0) # get list object season_list = seasons.get_seasons() # get list object sortby={'attr':'name', 'property':'asc'} return render_template('award_stats.html', award_stattable=award_stat.award_stattable, award_stats=award_stat_list, awards=award_list, seasons=season_list, players=player_list, total=total, limit=limit, page=page, sortby=sortby)
def admin(): """Routing function for admin page. This page allows *POST* and *GET* requests. *GET request:* If the user signed in adminpanel page is rendered. Otherwise signin page is rendered. *POST request:* Checks the request.form values for registered users. If the values are valid it adds user to the session and renders the adminpanel. Otherwise error message is flashed. """ conn, cur = getDb() error = None roles = None if request.method == "POST": username = request.form["username"] password = request.form["password"] if login_success(username, password): error = "Logged in!" query = "SELECT role, lastlogin FROM users WHERE username=%s" cur.execute(query, (username,)) role, lastlogin = cur.fetchone() g.role = role g.lastlogin = lastlogin session["username"] = request.form["username"] now = getCurrTimeStr() query = "UPDATE users SET lastlogin=%s WHERE username=%s" cur.execute(query, (now, username)) query = "UPDATE users SET online=TRUE WHERE username=%s" cur.execute(query, (username,)) conn.commit() else: error = "Invalid username or password!" if "username" in session: username = session["username"] query = "SELECT role, lastlogin FROM users WHERE username=%s" cur.execute(query, (username,)) role, lastlogin = cur.fetchone() g.role = role g.lastlogin = lastlogin else: flash("Wrong username or password") return render_template("signin.html") return render_template("adminpanel.html", error=error, roles=roles)
def view_schedule(schedule_id): conn, cur = getDb() schedules = schedule.Schedules(conn, cur) teams = team.Teams(conn, cur) seasons = season.Seasons(conn, cur) leagues = league.Leagues(conn, cur) l = schedules.get_schedule(schedule_id) if l is None: # return not found error return render_template("error.html", err_code=404) # else render country page with required args return render_template("schedule_page.html", schedule=l)
def logout(): """Routing function for logout page. When this page called if there is a user logged in, then the user removed from the session. After all it redirects to the home page. """ conn, cur = getDb() if "username" in session: username = session["username"] query = "UPDATE users SET online=FALSE WHERE username=%s" cur.execute(query, (username,)) conn.commit() g.role = "guest" session.pop("username", None) return redirect(url_for("home"))
def view_award(award_id): '''Routing function for award info page. It renders the award_page.html with related statistical information. ''' conn, cur = getDb() awards = award.Awards(conn, cur) l = awards.get_award(award_id) if l is None: # return not found error return render_template('error.html', err_code=404) # else render country page with required args return render_template('award_page.html', award=l)
def view_match(match_id): '''Routing function for match info page. It renders the match_page.html with related statistical information. ''' conn, cur = getDb() matches = match.Matches(conn, cur) schedules = schedule.Schedules(conn, cur) l = matches.get_match(match_id) if l is None: # return not found error return render_template('error.html', err_code=404) # else render country page with required args return render_template('match_page.html', match=l)
def league_from_id(lid): '''Routing function for getting league from its id. *GET request* returns JSON object. *POST request* updates league which has id equal to the *lid* with the values recieved from the request.form. After all it renders leagues.html :param lid: id of the league, int ''' if 'username' not in session: return render_template('error.html', err_code=401) conn, cur = getDb() leagues = league.Leagues(conn, cur) countries = country.Countries(conn, cur) if request.method == 'GET': # handle GET request l= leagues.get_league(lid) if l: return json.dumps({'status':'OK', 'league':l.getAttrs()}) else: return json.dumps({'status':'FAILED'}) elif request.method == 'POST': # handle POST request print("POST METHOD REQUEST") lid = request.form['id'] name = request.form['name'] country_id = request.form['country'] # limit: number of result showing each page # offset: selectedpage x limit limit = int(request.form['limit']) if 'limit' in request.form else 10 page = int(request.form['page']) if 'page' in request.form else 0 offset = page*limit order = request.form['sortby'] if 'sortby' in request.form else 'name' order = request.form['order'] if 'order' in request.form else 'asc' lg = league.League(name, country_id) leagues.update_league(lid, lg) l, total = leagues.get_leagues(limit, offset) c, total_countries = countries.get_countries() sortby={'attr':'name', 'property':'asc'} return render_template('leagues.html', leaguetable=league.leaguetable, leagues=l, countries=c, total=total, limit=limit, page=page, sortby=sortby)