示例#1
0
def init_db(_):
    global dev_mode

    top_folder = os.path.dirname(wintellect_demo.__file__)
    rel_file = os.path.join('db', 'wintellect_demo.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)

    CmsService.init_test_data()
    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 get_page_by_url(cls, url):
        if not url:
            return None

        url = url.lower().strip()
        session = DbSessionFactory.create_session()

        query = session.query(CmsPage) \
            .filter(CmsPage.url == url) \
            .order_by(CmsPage.created_date.desc())

        page = query.first()

        return page
    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
示例#10
0
    def add_page(cls, url, html, is_redirect=False, redirect_url=None):
        if not url or not url.strip():
            raise ValueError('url cannot be empty')

        url = url.lower().strip()
        session = DbSessionFactory.create_session()

        page = CmsPage()
        page.url = url
        page.html = html
        page.is_redirect = is_redirect
        page.redirect_url = redirect_url

        session.add(page)
        session.commit()

        return page