本文整理汇总了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
示例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
示例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
示例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')
示例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
示例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
示例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()
示例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
示例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
示例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>'
示例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
示例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'
示例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'
示例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')
示例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()