# Path: nflpool/infrastructure/supressor.py
# class suppress(pyramid_handlers.action):
#     def __init__(self, _=None, **kw):
#         kw["request_method"] = "NOT_A_HTTP_VERB"
#         super().__init__(**kw)
# 
# Path: nflpool/services/account_service.py
# class AccountService:
#     @staticmethod
#     def create_account(email, first_name, last_name, plain_text_password, twitter):
#         session = DbSessionFactory.create_session()
# 
#         account = Account()
#         account.email = email
#         account.first_name = first_name[0].upper() + first_name[1:]
#         account.last_name = last_name[0].upper() + last_name[1:]
#         account.password_hash = AccountService.hash_text(plain_text_password)
# 
#         if secret.su_email == account.email:
#             account.is_super_user = True
# 
#         if twitter != "" and twitter[0] != "@":
#             twitter = "@" + twitter
# 
#         account.twitter = twitter
# 
#         session.add(account)
#         session.commit()
# 
#         return account
# 
#     @classmethod
#     def find_account_by_email(cls, email):
# 
#         if not email or not email.strip():
#             return None
# 
#         email = email.lower().strip()
# 
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.email == email).first()
# 
#         return account
# 
#     @staticmethod
#     def hash_text(plain_text_password):
#         hashed_text = sha512_crypt.encrypt(plain_text_password, rounds=150000)
#         return hashed_text
# 
#     @classmethod
#     def get_authenticated_account(cls, email, plain_text_password):
#         account = AccountService.find_account_by_email(email)
#         if not account:
#             return None
# 
#         if not sha512_crypt.verify(plain_text_password, account.password_hash):
#             return None
# 
#         return account
# 
#     @classmethod
#     def find_account_by_id(cls, user_id):
#         if not user_id:
#             return None
# 
#         user_id = user_id.strip()
# 
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.id == user_id).first()
# 
#         return account
# 
#     @staticmethod
#     def create_reset_code(email):
# 
#         account = AccountService.find_account_by_email(email)
#         if not account:
#             return None
# 
#         session = DbSessionFactory.create_session()
# 
#         reset = PasswordReset()
#         reset.used_ip_address = "1.2.3.4"  # set for real
#         reset.user_id = account.id
# 
#         session.add(reset)
#         session.commit()
# 
#         return reset
# 
#     @classmethod
#     def find_reset_code(cls, code):
# 
#         if not code or not code.strip():
#             return None
# 
#         session = DbSessionFactory.create_session()
#         reset = session.query(PasswordReset).filter(PasswordReset.id == code).first()
# 
#         return reset
# 
#     @classmethod
#     def use_reset_code(cls, reset_code, user_ip):
#         session = DbSessionFactory.create_session()
# 
#         reset = (
#             session.query(PasswordReset).filter(PasswordReset.id == reset_code).first()
#         )
# 
#         if not reset:
#             return
# 
#         reset.used_ip_address = user_ip
#         reset.was_used = True
#         reset.used_date = datetime.datetime.now()
# 
#         session.commit()
# 
#     @classmethod
#     def set_password(cls, plain_text_password, account_id):
#         print("Resetting password for user {}".format(account_id))
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.id == account_id).first()
# 
#         if not account:
#             print("Warning: Cannot reset password, no account found.")
#             return
# 
#         print("New password set.")
#         account.password_hash = AccountService.hash_text(plain_text_password)
#         session.commit()
# 
#     @classmethod
#     def get_account_info(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         account_info = session.query(Account).filter(Account.id == user_id).all()
# 
#         return account_info
# 
#     @classmethod
#     def get_account_date(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         account_created = session.query(Account.created).first()
#         account_string = str(account_created[0])
#         account_date_split = account_string.split()
#         account_date = account_date_split[0]
# 
#         return account_date
# 
#     @classmethod
#     def seasons_played(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         seasons_played = (
#             session.query(PlayerPicks.season)
#             .distinct(PlayerPicks.season)
#             .filter(Account.id == user_id)
#             .order_by(PlayerPicks.season.desc())
#         )
# 
#         return seasons_played


import logbook
import nflpool.infrastructure.static_cache as static_cache
import pyramid.renderers
import pyramid.httpexceptions as exc
import nflpool.infrastructure.cookie_auth as cookie_auth
from nflpool.infrastructure.supressor import suppress
from nflpool.services.account_service import AccountService




class BaseController:
    def __init__(self, request):
        self.request = request
        self.build_cache_id = static_cache.build_cache_id

        layout_render = pyramid.renderers.get_renderer(
            "nflpool:templates/shared/_layout.pt"
        )
        impl = layout_render.implementation()
        self.layout = impl.macros["layout"]

        log_name = "Ctrls/" + type(self).__name__.replace("Controller", "")
        self.log = logbook.Logger(log_name)


# Path: nflpool/infrastructure/supressor.py
# class suppress(pyramid_handlers.action):
#     def __init__(self, _=None, **kw):
#         kw["request_method"] = "NOT_A_HTTP_VERB"
#         super().__init__(**kw)
# 
# Path: nflpool/services/account_service.py
# class AccountService:
#     @staticmethod
#     def create_account(email, first_name, last_name, plain_text_password, twitter):
#         session = DbSessionFactory.create_session()
# 
#         account = Account()
#         account.email = email
#         account.first_name = first_name[0].upper() + first_name[1:]
#         account.last_name = last_name[0].upper() + last_name[1:]
#         account.password_hash = AccountService.hash_text(plain_text_password)
# 
#         if secret.su_email == account.email:
#             account.is_super_user = True
# 
#         if twitter != "" and twitter[0] != "@":
#             twitter = "@" + twitter
# 
#         account.twitter = twitter
# 
#         session.add(account)
#         session.commit()
# 
#         return account
# 
#     @classmethod
#     def find_account_by_email(cls, email):
# 
#         if not email or not email.strip():
#             return None
# 
#         email = email.lower().strip()
# 
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.email == email).first()
# 
#         return account
# 
#     @staticmethod
#     def hash_text(plain_text_password):
#         hashed_text = sha512_crypt.encrypt(plain_text_password, rounds=150000)
#         return hashed_text
# 
#     @classmethod
#     def get_authenticated_account(cls, email, plain_text_password):
#         account = AccountService.find_account_by_email(email)
#         if not account:
#             return None
# 
#         if not sha512_crypt.verify(plain_text_password, account.password_hash):
#             return None
# 
#         return account
# 
#     @classmethod
#     def find_account_by_id(cls, user_id):
#         if not user_id:
#             return None
# 
#         user_id = user_id.strip()
# 
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.id == user_id).first()
# 
#         return account
# 
#     @staticmethod
#     def create_reset_code(email):
# 
#         account = AccountService.find_account_by_email(email)
#         if not account:
#             return None
# 
#         session = DbSessionFactory.create_session()
# 
#         reset = PasswordReset()
#         reset.used_ip_address = "1.2.3.4"  # set for real
#         reset.user_id = account.id
# 
#         session.add(reset)
#         session.commit()
# 
#         return reset
# 
#     @classmethod
#     def find_reset_code(cls, code):
# 
#         if not code or not code.strip():
#             return None
# 
#         session = DbSessionFactory.create_session()
#         reset = session.query(PasswordReset).filter(PasswordReset.id == code).first()
# 
#         return reset
# 
#     @classmethod
#     def use_reset_code(cls, reset_code, user_ip):
#         session = DbSessionFactory.create_session()
# 
#         reset = (
#             session.query(PasswordReset).filter(PasswordReset.id == reset_code).first()
#         )
# 
#         if not reset:
#             return
# 
#         reset.used_ip_address = user_ip
#         reset.was_used = True
#         reset.used_date = datetime.datetime.now()
# 
#         session.commit()
# 
#     @classmethod
#     def set_password(cls, plain_text_password, account_id):
#         print("Resetting password for user {}".format(account_id))
#         session = DbSessionFactory.create_session()
# 
#         account = session.query(Account).filter(Account.id == account_id).first()
# 
#         if not account:
#             print("Warning: Cannot reset password, no account found.")
#             return
# 
#         print("New password set.")
#         account.password_hash = AccountService.hash_text(plain_text_password)
#         session.commit()
# 
#     @classmethod
#     def get_account_info(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         account_info = session.query(Account).filter(Account.id == user_id).all()
# 
#         return account_info
# 
#     @classmethod
#     def get_account_date(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         account_created = session.query(Account.created).first()
#         account_string = str(account_created[0])
#         account_date_split = account_string.split()
#         account_date = account_date_split[0]
# 
#         return account_date
# 
#     @classmethod
#     def seasons_played(cls, user_id):
#         session = DbSessionFactory.create_session()
# 
#         seasons_played = (
#             session.query(PlayerPicks.season)
#             .distinct(PlayerPicks.season)
#             .filter(Account.id == user_id)
#             .order_by(PlayerPicks.season.desc())
#         )
# 
#         return seasons_played


# Path: nflpool/controllers/base_controller.py
import logbook
import nflpool.infrastructure.static_cache as static_cache
import pyramid.renderers
import pyramid.httpexceptions as exc
import nflpool.infrastructure.cookie_auth as cookie_auth
from nflpool.infrastructure.supressor import suppress
from nflpool.services.account_service import AccountService





class BaseController:
    def __init__(self, request):
        self.request = request
        self.build_cache_id = static_cache.build_cache_id

        layout_render = pyramid.renderers.get_renderer(
            "nflpool:templates/shared/_layout.pt"
        )
        impl = layout_render.implementation()
        self.layout = impl.macros["layout"]

        log_name = "Ctrls/" + type(self).__name__.replace("Controller", "")
        self.log = logbook.Logger(log_name)


    @property