本文整理汇总了Python中sqlalchemy_wrapper.SQLAlchemy.commit方法的典型用法代码示例。如果您正苦于以下问题:Python SQLAlchemy.commit方法的具体用法?Python SQLAlchemy.commit怎么用?Python SQLAlchemy.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy_wrapper.SQLAlchemy
的用法示例。
在下文中一共展示了SQLAlchemy.commit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_flask_app
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
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
示例2: test_user_model_to_dict
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
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
示例3: test_role_model
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
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>'
示例4: test_formset_missing_objs
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_formset_missing_objs():
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
class Address(db.Model):
__tablename__ = 'addresses'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship('User',
backref=db.backref('addresses', lazy='dynamic'))
def __repr__(self):
return self.email
db.create_all()
class FormAddress(f.Form):
_model = Address
email = f.Text(validate=[f.ValidEmail])
def __repr__(self):
return '<FormAddress %s>' % (self.email.value,)
class FormUser(f.Form):
_model = User
name = f.Text()
addresses = f.FormSet(FormAddress, parent='user')
user = User(name=u'John Doe')
db.add(user)
a1 = Address(email=u'[email protected]', user=user)
db.add(a1)
a2 = Address(email=u'[email protected]', user=user)
db.add(a2)
a3 = Address(email=u'[email protected]', user=user)
db.add(a3)
db.commit()
print([(a.id, a.email) for a in user.addresses])
data = {
'name': u'Jane Doe',
'formaddress.1-email': u'[email protected]',
'formaddress.3-email': u'[email protected]',
'formaddress.4-email': u'[email protected]',
}
form = FormUser(data, user)
assert form.is_valid()
assert form.addresses.missing_objs == [a2]
示例5: test_backwards_compatibility
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
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'
示例6: test_custom_templates
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_custom_templates():
db = SQLAlchemy('sqlite:///:memory:')
options = {
'template_sign_in': 'sign-in.html',
'template_sign_out': 'sign-out.html',
'template_reset': 'reset-password.html',
'template_reset_email': 'reset-password-email.html',
'template_change_password': 'change-password.html',
}
inbox = []
def send_email(user, subject, msg):
inbox.append(msg)
auth = authcode.Auth(SECRET_KEY, db=db, **options)
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.add(user)
db.commit()
custom_templates = os.path.join(
os.path.dirname(__file__),
'custom_templates'
)
app = Flask('test', template_folder=custom_templates)
app.secret_key = os.urandom(32)
app.testing = True
authcode.setup_for_flask(auth, app, send_email=send_email)
auth.session = {}
client = app.test_client()
resp = client.get(auth.url_sign_in)
assert resp.data == b'OK SIGN IN'
resp = client.get(auth.url_reset_password)
assert resp.data == b'OK RESET PASSWORD'
data = dict(login=user.login, _csrf_token=auth.get_csrf_token())
resp = client.post(auth.url_reset_password, data=data)
assert inbox[0] == 'OK RESET PASSWORD EMAIL'
auth.login(user)
resp = client.get(auth.url_change_password)
assert resp.data == b'OK CHANGE PASSWORD'
url = '{0}?_csrf_token={1}'.format(auth.url_sign_out, auth.get_csrf_token())
resp = client.get(url)
assert resp.data == b'OK SIGN OUT'
示例7: test_aggregated_query
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_aggregated_query():
db = SQLAlchemy(URI1)
class Unit(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60))
price = db.Column(db.Integer)
db.create_all()
db.add(Unit(price=25))
db.add(Unit(price=5))
db.add(Unit(price=10))
db.add(Unit(price=3))
db.commit()
res = db.query(db.func.sum(Unit.price).label('price')).first()
assert res.price == 43
示例8: test_multiple_databases
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_multiple_databases():
db1 = SQLAlchemy(URI1)
db2 = SQLAlchemy(URI2)
ToDo1 = create_test_model(db1)
ToDo2 = create_test_model(db2)
db1.create_all()
db2.create_all()
db1.add(ToDo1('A', 'a'))
db1.add(ToDo1('B', 'b'))
db2.add(ToDo2('Q', 'q'))
db1.add(ToDo1('C', 'c'))
db1.commit()
db2.commit()
assert db1.query(ToDo1).count() == 3
assert db2.query(ToDo2).count() == 1
示例9: test_user_model
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_user_model():
db = SQLAlchemy('sqlite:///:memory:')
auth = authcode.Auth(SECRET_KEY, db=db, roles=True)
assert auth.users_model_name == 'User'
assert auth.roles_model_name == 'Role'
User = auth.User
db.create_all()
user = User(login=u'meh', password='foobar')
db.session.add(user)
db.commit()
assert user.login == u'meh'
assert user.email == user.login
assert hasattr(user, 'password')
assert hasattr(user, 'last_sign_in')
assert repr(user) == '<User meh>'
示例10: test_save
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_save():
db = SQLAlchemy()
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.Unicode, nullable=False)
email = db.Column(db.Unicode)
message = db.Column(db.UnicodeText, nullable=False)
db.create_all()
class MyContactForm(f.Form):
_model = Contact
subject = f.Text(validate=[f.Required])
email = f.Text(validate=[f.ValidEmail])
message = f.Text(validate=[
f.Required(message=u'write something!')
])
# Create new object
data = {
'subject': u'foo',
'message': u'bar',
'email': u'[email protected]',
}
form = MyContactForm(data)
assert form.is_valid()
contact = form.save()
print('contact', contact.__dict__)
assert isinstance(contact, Contact)
assert contact.id is None
assert contact.subject == data['subject']
assert contact.message == data['message']
assert contact.email == data['email']
db.commit()
# Update object
data['message'] = u'lalala'
form = MyContactForm(data, obj=contact)
assert form.is_valid()
contact = form.save()
assert contact.id is not None
assert contact.message == data['message']
db.commit()
示例11: test_prefix_save
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_prefix_save():
db = SQLAlchemy()
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.Unicode, nullable=False)
email = db.Column(db.Unicode)
message = db.Column(db.UnicodeText, nullable=False)
db.create_all()
class MyContactForm(f.Form):
_model = Contact
subject = f.Text(validate=[f.Required])
email = f.Text(validate=[f.ValidEmail])
message = f.Text(validate=[
f.Required(message=u'write something!')
])
data = {
'meh-subject': u'Hello',
'meh-message': u'Welcome',
}
form = MyContactForm(data, prefix='meh')
assert form.is_valid()
contact = form.save()
assert isinstance(contact, Contact)
db.commit()
assert contact.subject == data['meh-subject']
assert contact.message == data['meh-message']
data = {
'meh-subject': u'foo',
'meh-message': u'bar',
}
form = MyContactForm(data, obj=contact, prefix='meh')
assert form.is_valid()
assert form.has_changed
form.save()
db.commit()
assert contact.subject == data['meh-subject']
assert contact.message == data['meh-message']
示例12: test_user_model_methods
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_user_model_methods():
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.by_id(user.id) == user
assert User.by_id(33) is None
assert User.by_login(u'meh') == user
assert User.by_login(u'foobar') is None
assert user.has_password('foobar')
assert not user.has_password('abracadabra')
assert user.get_token()
assert user.get_uhmac()
示例13: create_test_model
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def create_test_model():
db = SQLAlchemy('sqlite://')
class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
class Part(db.Model):
id = db.Column(db.Integer, primary_key=True)
item_id = db.Column(db.Integer, db.ForeignKey(Item.id))
item = db.relationship('Item', backref='parts')
db.create_all()
for i in range(1, 26):
item = Item()
db.add(item)
db.commit()
item = db.query(Item).first()
for j in range(1, 26):
db.add(Part(item=item))
db.commit()
return db, Item, Part
示例14: test_missing_fields_obj
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_missing_fields_obj():
db = SQLAlchemy()
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.Unicode)
db.create_all()
class ContactForm(f.Form):
_model = Contact
subject = f.Text()
contact = Contact(subject=u'foobar')
db.add(contact)
db.commit()
form = ContactForm({}, contact)
new_contact = form.save()
assert new_contact.subject == u'foobar'
form = ContactForm({'subject': u''}, contact)
new_contact = form.save()
assert new_contact.subject == u''
示例15: test_role_model_methods
# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import commit [as 别名]
def test_role_model_methods():
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.by_id(role.id) == role
assert Role.by_id(33) is None
assert Role.by_name(u'admin') == role
assert Role.by_name(u'foobar') is None
assert Role.get_or_create(u'admin') == role
role2 = Role.get_or_create(u'owner')
db.commit()
assert role2 != role
assert db.query(Role).count() == 2
assert list(role.users) == []
assert list(role2.users) == []