当前位置: 首页>>代码示例>>Python>>正文


Python SQLAlchemy.add方法代码示例

本文整理汇总了Python中sqlalchemy_wrapper.SQLAlchemy.add方法的典型用法代码示例。如果您正苦于以下问题:Python SQLAlchemy.add方法的具体用法?Python SQLAlchemy.add怎么用?Python SQLAlchemy.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy_wrapper.SQLAlchemy的用法示例。


在下文中一共展示了SQLAlchemy.add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_flask_app

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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
开发者ID:jpscaletti,项目名称:authcode,代码行数:28,代码来源:test_views.py

示例2: test_query

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [as 别名]
def test_query():
    db = SQLAlchemy(URI1)
    ToDo = create_test_model(db)
    db.create_all()

    db.add(ToDo('First', 'The text'))
    db.add(ToDo('Second', 'The text'))
    db.flush()

    titles = ' '.join(x.title for x in db.query(ToDo).all())
    assert titles == 'First Second'

    data = db.query(ToDo).filter(ToDo.title == 'First').all()
    assert len(data) == 1
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:16,代码来源:test_main.py

示例3: test_custom_templates

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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'
开发者ID:jpscaletti,项目名称:authcode,代码行数:52,代码来源:test_views.py

示例4: test_model_helpers

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [as 别名]
def test_model_helpers():
    db = SQLAlchemy()

    class Row(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.String(60), nullable=False)
        created_at = db.Column(db.DateTime, nullable=False,
                               default=datetime.utcnow)

    db.create_all()
    db.add(Row(name='a'))
    db.flush()
    row = db.query(Row).first()

    assert str(row) == '<Row>'
    assert dict(row)['name'] == 'a'
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:18,代码来源:test_main.py

示例5: test_formset_missing_objs

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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]
开发者ID:PuercoPop,项目名称:solution,代码行数:55,代码来源:test_forms.py

示例6: create_test_model

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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
开发者ID:glogiotatidis,项目名称:sqlalchemy-wrapper,代码行数:26,代码来源:test_pagination.py

示例7: test_aggregated_query

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:19,代码来源:test_main.py

示例8: test_missing_fields_obj

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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''
开发者ID:jpscaletti,项目名称:solution,代码行数:26,代码来源:test_forms.py

示例9: test_multiple_databases

# 需要导入模块: from sqlalchemy_wrapper import SQLAlchemy [as 别名]
# 或者: from sqlalchemy_wrapper.SQLAlchemy import add [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
开发者ID:aadu,项目名称:sqlalchemy-wrapper,代码行数:19,代码来源:test_main.py


注:本文中的sqlalchemy_wrapper.SQLAlchemy.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。