# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/player_picks.py
# class PlayerPicks(SqlAlchemyBase):
#     __tablename__ = "PlayerPicks"
#     pick_id = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     user_id = sqlalchemy.Column(sqlalchemy.String, sqlalchemy.ForeignKey("Account.id"))
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     date_submitted = sqlalchemy.Column(sqlalchemy.DATETIME)
#     conf_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ConferenceInfo.conf_id")
#     )
#     division_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("DivisionInfo.division_id")
#     )
#     rank = sqlalchemy.Column(sqlalchemy.Integer)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     multiplier = sqlalchemy.Column(sqlalchemy.Integer, default=1)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     pick_type = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("PickTypes.pick_type_id")
#     )
# 
# Path: nflpool/data/seasoninfo.py
# class SeasonInfo(SqlAlchemyBase):
#     __tablename__ = "SeasonInfo"
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
#     current_season = sqlalchemy.Column(sqlalchemy.Integer)
#     season_start_date = sqlalchemy.Column(sqlalchemy.String)
#     home_team = sqlalchemy.Column(sqlalchemy.String)
#     away_team = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/services/time_service.py
# class TimeService:
#     @staticmethod
#     def get_time():
#         """Create a service to get the time - there were too many instances of getting the current time in
#         the codebase making testing difficult."""
#         # Change now_time for testing
#         # Use this one for production:
#         now_time = pendulum.now(tz=pendulum.timezone("America/New_York"))
#         # Use this one for testing:
#         # now_time = pendulum.datetime(2018, 9, 18, 6, 21, tz='America/New_York')
# 
#         return now_time
# 
#     @staticmethod
#     def get_week():
#         session = DbSessionFactory.create_session()
#         season_row = session.query(SeasonInfo).filter(SeasonInfo.id == "1").first()
# 
#         season_start = pendulum.parse(season_row.season_start_date)
# 
#         diff = TimeService.get_time() - season_start
#         week = int((diff.days / 7) + 1)
# 
#         return week


from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.player_picks import PlayerPicks
from nflpool.data.seasoninfo import SeasonInfo
from nflpool.services.time_service import TimeService
        # return list that contains player standings for most recent week in results table
        if season is None:
            season = get_seasons()

        sqlstr = (
            "SELECT SUM(w.points_earned) as total_points, a.first_name, a.last_name, a.id "
            "from WeeklyPlayerResults w, PlayerPicks p, Account a "
        )
        sqlstr += "WHERE w.pick_id = p.pick_id AND p.user_id = a.id "
        sqlstr += "AND w.season = " + str(season) + " "
        sqlstr += "AND p.season = " + str(season) + " "
        sqlstr += (
            "AND w.week = (SELECT MAX(week) from WeeklyPlayerResults WHERE season = "
            + str(season)
            + ") "
        )
        sqlstr += "GROUP BY p.user_id "
        sqlstr += "ORDER BY total_points DESC"

        session = DbSessionFactory.create_session()
        standings = session.execute(sqlstr)

        dict_standings = [dict(row) for row in standings]

        session.close()
        return dict_standings

    @staticmethod
    def update_player_pick_points():
        season = get_seasons()

# Path: nflpool/data/dbsession.py
# class DbSessionFactory:
#     factory = None
# 
#     # Start a database session at app startup
#     @staticmethod
#     def global_init(db_file):
#         if DbSessionFactory.factory:
#             return
# 
#         if not db_file or not db_file.strip():
#             raise Exception("You must specify a data file.")
# 
#         conn_str = "sqlite:///" + db_file
#         print("Connecting to db with conn string: {}".format(conn_str))
# 
#         engine = sqlalchemy.create_engine(conn_str, echo=False)
# 
#         SqlAlchemyBase.metadata.create_all(engine)
#         DbSessionFactory.factory = sqlalchemy.orm.sessionmaker(bind=engine)
# 
#     @staticmethod
#     def create_session():
#         return DbSessionFactory.factory()
# 
# Path: nflpool/data/player_picks.py
# class PlayerPicks(SqlAlchemyBase):
#     __tablename__ = "PlayerPicks"
#     pick_id = sqlalchemy.Column(
#         sqlalchemy.Integer, primary_key=True, autoincrement=True
#     )
#     user_id = sqlalchemy.Column(sqlalchemy.String, sqlalchemy.ForeignKey("Account.id"))
#     season = sqlalchemy.Column(sqlalchemy.Integer, index=True)
#     date_submitted = sqlalchemy.Column(sqlalchemy.DATETIME)
#     conf_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ConferenceInfo.conf_id")
#     )
#     division_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("DivisionInfo.division_id")
#     )
#     rank = sqlalchemy.Column(sqlalchemy.Integer)
#     team_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("TeamInfo.team_id")
#     )
#     multiplier = sqlalchemy.Column(sqlalchemy.Integer, default=1)
#     player_id = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("ActiveNFLPlayers.player_id")
#     )
#     pick_type = sqlalchemy.Column(
#         sqlalchemy.Integer, sqlalchemy.ForeignKey("PickTypes.pick_type_id")
#     )
# 
# Path: nflpool/data/seasoninfo.py
# class SeasonInfo(SqlAlchemyBase):
#     __tablename__ = "SeasonInfo"
#     id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
#     current_season = sqlalchemy.Column(sqlalchemy.Integer)
#     season_start_date = sqlalchemy.Column(sqlalchemy.String)
#     home_team = sqlalchemy.Column(sqlalchemy.String)
#     away_team = sqlalchemy.Column(sqlalchemy.String)
# 
# Path: nflpool/services/time_service.py
# class TimeService:
#     @staticmethod
#     def get_time():
#         """Create a service to get the time - there were too many instances of getting the current time in
#         the codebase making testing difficult."""
#         # Change now_time for testing
#         # Use this one for production:
#         now_time = pendulum.now(tz=pendulum.timezone("America/New_York"))
#         # Use this one for testing:
#         # now_time = pendulum.datetime(2018, 9, 18, 6, 21, tz='America/New_York')
# 
#         return now_time
# 
#     @staticmethod
#     def get_week():
#         session = DbSessionFactory.create_session()
#         season_row = session.query(SeasonInfo).filter(SeasonInfo.id == "1").first()
# 
#         season_start = pendulum.parse(season_row.season_start_date)
# 
#         diff = TimeService.get_time() - season_start
#         week = int((diff.days / 7) + 1)
# 
#         return week


# Path: nflpool/services/standings_service.py
from nflpool.data.dbsession import DbSessionFactory
from nflpool.data.player_picks import PlayerPicks
from nflpool.data.seasoninfo import SeasonInfo
from nflpool.services.time_service import TimeService

        # return list that contains player standings for most recent week in results table
        if season is None:
            season = get_seasons()

        sqlstr = (
            "SELECT SUM(w.points_earned) as total_points, a.first_name, a.last_name, a.id "
            "from WeeklyPlayerResults w, PlayerPicks p, Account a "
        )
        sqlstr += "WHERE w.pick_id = p.pick_id AND p.user_id = a.id "
        sqlstr += "AND w.season = " + str(season) + " "
        sqlstr += "AND p.season = " + str(season) + " "
        sqlstr += (
            "AND w.week = (SELECT MAX(week) from WeeklyPlayerResults WHERE season = "
            + str(season)
            + ") "
        )
        sqlstr += "GROUP BY p.user_id "
        sqlstr += "ORDER BY total_points DESC"

        session = DbSessionFactory.create_session()
        standings = session.execute(sqlstr)

        dict_standings = [dict(row) for row in standings]

        session.close()
        return dict_standings

    @staticmethod
    def update_player_pick_points():
        season = get_seasons()

        week = TimeService.get_week()