# Path: config.py
# USERS_PER_PAGE = 25
# 
# TOURNAMENTS_PER_PAGE = 15 
# 
# CHAR_USERS_PER_PAGE = 15
# 
# Path: sort_utils.py
# def sort_placementlist(placementlist):
#   sorted_placementlist = sorted(placementlist, key=attrgetter('tournament.date', 'tournament.name'), reverse=True)
#   return sorted_placementlist
# 
# def sort_userlist(userlist):
#   sorted_userlist = sorted(userlist, key=attrgetter('trueskill.mu'), reverse=True)
# 
# Path: misc_utils.py
# def sanitize_url(tournament_name):
# 	tournament_name = tournament_name.replace("%20", ' ')
# 	tournament_name = tournament_name.replace("%2F", '/')
# 	tournament_name = tournament_name.replace("%2D", '-')
# 	tournament_name = tournament_name.replace("%2E", '.')
# 	return tournament_name


from flask import render_template, flash, redirect, request, url_for, g, session
from app import app, db
from models import *
from forms import UserCreate, UserEdit, SetCreate, SetEdit, MatchSubmit, HeadToHead, SearchForm, CharacterFilter, character_choices, character_list, RegionSelect
from sqlalchemy import and_, or_
from config import USERS_PER_PAGE, TOURNAMENTS_PER_PAGE, CHAR_USERS_PER_PAGE 
from sanitize_utils import check_and_sanitize_tag, check_and_sanitize_tag_multiple
from sort_utils import sort_placementlist, sort_userlist
from misc_utils import sanitize_url
from h2h_stats_functions import *
import collections
import sys
  print "session['region_name']", session['region_name']

  flash("Now Viewing Region: " + str(session['region_name']))
  g.region = session['region_name']
  print "g.region", g.region
  return render_template("region.html",
                         region=region,
                         current_region=current_region)
 

# Displays a list of all SSBM characters, each of which links to /character/<character>
@app.route('/browse_characters')
def browse_characters():
  characterlist = main_char_list
  return render_template("browse_characters.html",
                         characterlist=characterlist,
                         current_region=g.region)

 
# Displays all users who play a certain character. Routed to from /browse_characters
@app.route('/character/<character>')
@app.route('/character/<character>/<int:page>')
def character(character, page=1):
  # "Convert" character parameter, which is currently a string, to Character object.
  character_object = Character.query.filter(Character.name==character).first()
  if character_object is None:
    return render_template('404.html'), 404

  # if viewing Global information, don't filter query by g.region
  if g.region=="Global" or g.region=="National":

# Path: config.py
# USERS_PER_PAGE = 25
# 
# TOURNAMENTS_PER_PAGE = 15 
# 
# CHAR_USERS_PER_PAGE = 15
# 
# Path: sort_utils.py
# def sort_placementlist(placementlist):
#   sorted_placementlist = sorted(placementlist, key=attrgetter('tournament.date', 'tournament.name'), reverse=True)
#   return sorted_placementlist
# 
# def sort_userlist(userlist):
#   sorted_userlist = sorted(userlist, key=attrgetter('trueskill.mu'), reverse=True)
# 
# Path: misc_utils.py
# def sanitize_url(tournament_name):
# 	tournament_name = tournament_name.replace("%20", ' ')
# 	tournament_name = tournament_name.replace("%2F", '/')
# 	tournament_name = tournament_name.replace("%2D", '-')
# 	tournament_name = tournament_name.replace("%2E", '.')
# 	return tournament_name


# Path: app/views.py
from flask import render_template, flash, redirect, request, url_for, g, session
from app import app, db
from models import *
from forms import UserCreate, UserEdit, SetCreate, SetEdit, MatchSubmit, HeadToHead, SearchForm, CharacterFilter, character_choices, character_list, RegionSelect
from sqlalchemy import and_, or_
from config import USERS_PER_PAGE, TOURNAMENTS_PER_PAGE, CHAR_USERS_PER_PAGE 
from sanitize_utils import check_and_sanitize_tag, check_and_sanitize_tag_multiple
from sort_utils import sort_placementlist, sort_userlist
from misc_utils import sanitize_url
from h2h_stats_functions import *
import collections
import sys

  print "session['region_name']", session['region_name']

  flash("Now Viewing Region: " + str(session['region_name']))
  g.region = session['region_name']
  print "g.region", g.region
  return render_template("region.html",
                         region=region,
                         current_region=current_region)
 

# Displays a list of all SSBM characters, each of which links to /character/<character>
@app.route('/browse_characters')
def browse_characters():
  characterlist = main_char_list
  return render_template("browse_characters.html",
                         characterlist=characterlist,
                         current_region=g.region)

 
# Displays all users who play a certain character. Routed to from /browse_characters
@app.route('/character/<character>')
@app.route('/character/<character>/<int:page>')
def character(character, page=1):
  # "Convert" character parameter, which is currently a string, to Character object.
  character_object = Character.query.filter(Character.name==character).first()
  if character_object is None:
    return render_template('404.html'), 404

  # if viewing Global information, don't filter query by g.region
  if g.region=="Global" or g.region=="National":

    matching_users = User.query.join(TrueSkill.user, User).filter(and_(TrueSkill.region=="Global", User.character.contains(character_object))).order_by(TrueSkill.cons_mu.desc()).paginate(page, CHAR_USERS_PER_PAGE, False)