當前位置: 首頁>>代碼示例>>Python>>正文


Python sqlalchemy_wrapper.SQLAlchemy類代碼示例

本文整理匯總了Python中sqlalchemy_wrapper.SQLAlchemy的典型用法代碼示例。如果您正苦於以下問題:Python SQLAlchemy類的具體用法?Python SQLAlchemy怎麽用?Python SQLAlchemy使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了SQLAlchemy類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_authenticate_with_token

def test_authenticate_with_token():
    from time import time
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, token_life=3 * 60)

    User = auth.User

    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.session.add(user)
    db.session.commit()

    token = user.get_token()
    auth_user = auth.authenticate({'token': token})
    assert auth_user

    token = '555' + user.get_token()
    auth_user = auth.authenticate({'token': token})
    assert not auth_user

    auth_user = auth.authenticate({'token': ''})
    assert not auth_user

    timestamp = int(time()) - auth.token_life + 1
    token = user.get_token(timestamp)
    auth_user = auth.authenticate({'token': token})
    assert auth_user

    timestamp = int(time()) - auth.token_life - 1
    token = user.get_token(timestamp)
    auth_user = auth.authenticate({'token': token})
    assert not auth_user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:32,代碼來源:test_auth.py

示例2: test_replace_hash_password_method

def test_replace_hash_password_method():
    """Can the library work the same with custom ``has_password`` and
    ``password_is_valid`` methods?
    """

    class CustomAuth(authcode.Auth):
        def hash_password(self, secret):
            secret = self.prepare_password(secret)
            return secret[::-1]

        def password_is_valid(self, secret, hashed):
            secret = self.prepare_password(secret)
            if secret is None or hashed is None:
                return False
            return self.hash_password(secret) == hashed

    db = SQLAlchemy('sqlite:///:memory:')
    auth = CustomAuth(SECRET_KEY, db=db)
    User = auth.User
    db.create_all()

    credentials = {'login': u'meh', 'password': 'foobar'}
    user = User(**credentials)
    db.session.add(user)
    db.session.commit()

    assert user.password == 'foobar'[::-1]
    assert user.has_password('foobar')

    auth_user = auth.authenticate(credentials)
    assert user.login == auth_user.login

    auth_user = auth.authenticate({})
    assert not auth_user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:34,代碼來源:test_auth.py

示例3: test_authenticate_with_password

def test_authenticate_with_password():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db)

    User = auth.User

    db.create_all()
    credentials = {'login': u'meh', 'password': 'foobar'}
    user = User(**credentials)
    db.session.add(user)
    db.session.commit()

    auth_user = auth.authenticate(credentials)
    assert user.login == auth_user.login

    auth_user = auth.authenticate({})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'meh'})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'wtf', 'password': 'foobar'})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'meh', 'password': 'lalala'})
    assert not auth_user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:26,代碼來源:test_auth.py

示例4: test_models_mixins

def test_models_mixins():
    db = SQLAlchemy('sqlite:///:memory:')

    class UserMixin(object):
        email = db.Column(db.Unicode(300))

        def __repr__(self):
            return 'overwrited'

    class RoleMixin(object):
        description = db.Column(db.UnicodeText)

    auth = authcode.Auth(SECRET_KEY, db=db, UserMixin=UserMixin, RoleMixin=RoleMixin)
    User = auth.User
    Role = auth.Role

    db.create_all()
    user = User(login=u'meh', password='foobar', email=u'[email protected]')
    db.session.add(user)
    db.flush()

    assert User.__tablename__ == 'users'
    assert user.login == u'meh'
    assert user.email == u'[email protected]'
    assert hasattr(user, 'password')
    assert hasattr(user, 'last_sign_in')
    assert repr(user) == 'overwrited'

    assert hasattr(Role, 'description')
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:29,代碼來源:test_models.py

示例5: test_user_has_empty_password

def test_user_has_empty_password():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, password_minlen=0)

    User = auth.User

    db.create_all()
    user = User(login=u'meh', password=u'')
    db.session.add(user)
    db.session.commit()

    assert user.password != u''

    auth_user = auth.authenticate({'login': u'meh', 'password': u''})
    assert auth_user

    auth_user = auth.authenticate({})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'meh', 'password': None})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'meh'})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'wtf', 'password': ''})
    assert not auth_user

    auth_user = auth.authenticate({'login': u'meh', 'password': 'lalala'})
    assert not auth_user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:30,代碼來源:test_auth.py

示例6: test_init_app

def test_init_app():
    class FakeApp(object):
        pass

    app = FakeApp()
    db = SQLAlchemy(URI1, app)
    assert app.databases
    db.init_app(app)
    assert len(app.databases) == 1
開發者ID:aadu,項目名稱:sqlalchemy-wrapper,代碼行數:9,代碼來源:test_main.py

示例7: test_define_table

def test_define_table():
    db = SQLAlchemy(URI1)
    db.Table('foobar',
             db.Column('foo', db.UnicodeText),
             db.Column('bar', db.UnicodeText))
    db.Table('fizzbuzz', db.metadata,
             db.Column('fizz', db.Integer),
             db.Column('buzz', db.Integer))
    db.create_all()
開發者ID:aadu,項目名稱:sqlalchemy-wrapper,代碼行數:9,代碼來源:test_main.py

示例8: _get_flask_app

def _get_flask_app(roles=False, **kwargs):
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(
        SECRET_KEY, db=db, roles=roles, password_minlen=3, **kwargs)
    User = auth.User

    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.add(user)

    user2 = User(login=u'foo', password='bar')
    db.add(user2)
    db.commit()

    app = Flask('test')
    app.secret_key = os.urandom(32)
    app.testing = True

    @app.route('/protected/')
    @auth.protected()
    def protected():
        return u'Welcome'

    authcode.setup_for_flask(auth, app)
    auth.session = {}
    return auth, app, user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:26,代碼來源:test_views.py

示例9: test_user_model_to_dict

def test_user_model_to_dict():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
    User = auth.User
    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.session.add(user)
    db.commit()

    user_dict = user.to_dict()
    assert user_dict
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:11,代碼來源:test_models.py

示例10: test_role_model

def test_role_model():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
    Role = auth.Role
    db.create_all()
    role = Role(name=u'admin')
    db.session.add(role)
    db.commit()

    assert role.name == u'admin'
    assert repr(role) == '<Role admin>'
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:11,代碼來源:test_models.py

示例11: test_automatic_case_insensitiveness

def test_automatic_case_insensitiveness():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db)
    User = auth.User
    db.create_all()
    user = User(login=u'MeH', password='foobar')
    db.session.add(user)
    db.session.commit()

    assert user.login == u'meh'
    assert User.by_login(u'MEH') == User.by_login(u'MeH') == user
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:11,代碼來源:test_auth.py

示例12: test_backwards_compatibility

def test_backwards_compatibility():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db)
    User = auth.User
    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.session.add(user)
    db.commit()

    assert user._password == user.password
    user._password = 'raw'
    assert user.password == 'raw'
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:12,代碼來源:test_models.py

示例13: test_set_raw_password

def test_set_raw_password():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
    User = auth.User
    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.session.add(user)
    db.session.commit()

    assert user.password != 'foobar'
    user.set_raw_password('foobar')
    assert user.password == 'foobar'
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:12,代碼來源:test_models.py

示例14: test_id_mixin

def test_id_mixin():
    db = SQLAlchemy(URI1)

    class IDMixin(object):
        id = db.Column(db.Integer, primary_key=True)

    class Model(db.Model, IDMixin):
        field = db.Column(db.String)

    db.create_all()

    assert Model.__tablename__ == 'models'
    assert hasattr(Model, 'id')
開發者ID:aadu,項目名稱:sqlalchemy-wrapper,代碼行數:13,代碼來源:test_main.py

示例15: test_get_uhmac

def test_get_uhmac():
    db = SQLAlchemy('sqlite:///:memory:')
    auth = authcode.Auth(SECRET_KEY, db=db)

    User = auth.User

    db.create_all()
    user = User(login=u'meh', password='foobar')
    db.session.add(user)
    db.session.commit()

    assert user.get_uhmac()
    assert user.get_uhmac() == user.get_uhmac()
開發者ID:jpscaletti,項目名稱:authcode,代碼行數:13,代碼來源:test_auth.py


注:本文中的sqlalchemy_wrapper.SQLAlchemy類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。