def init_db(_):
    global dev_mode

    top_folder = os.path.dirname(show_off_web_app.__file__)
    rel_file = os.path.join('db', 'show_off_web_app.sqlite')
    if dev_mode:
        rel_file = rel_file.replace('.sqlite', '_dev.sqlite')
    else:
        rel_file = rel_file.replace('.sqlite', '_prod.sqlite')

    db_file = os.path.join(top_folder, rel_file)
    DbSessionFactory.global_init(db_file)
    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
    def create_account(email, plain_text_password):
        session = DbSessionFactory.create_session()

        account = Account()
        account.email = email
        account.password_hash = AccountService.hash_text(plain_text_password)

        session.add(account)
        session.commit()

        return account
    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
    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
    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()
    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()
    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