Python sqlalchemy 模块,false() 实例源码
我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用sqlalchemy.false()。
def has_read_permission(self, qs):
return qs.filter(false())
def has_read_permission(self, qs):
if current_user.has_permission('view_product_config'):
return qs.filter(self.model.retail_shop_id.in_(current_user.retail_shop_ids))
return qs.filter(false())
def has_read_permission(self, qs):
if current_user.has_permission('view_registration_detail'):
return qs.filter(self.model.retail_shop_id.in_(current_user.retail_shop_ids))
return qs.filter(false())
def list_accounts(s: sqlalchemy.orm.session.Session,
*,
blacklisted: Optional[bool]=None) -> Sequence[Account]:
"""Get a list of all accounts.
If blacklisted is specified,only return accounts with that blacklisted
value.
"""
q = s.query(Account)
if blacklisted is not None:
q = q.filter(Account.blacklisted ==
(sqlalchemy.true()
if blacklisted else sqlalchemy.false()))
results = q.all()
return results
def _generic_char_type_lister(s: sqlalchemy.orm.session.Session, *,
cls: sqlalchemy.ext.declarative.api.DeclarativeMeta,
playable: Optional[bool]) \
-> Sequence:
q = s.query(cls)
if playable is not None:
# Type[Any] has no attribute "playable"
q = q.filter(cls.playable == (sqlalchemy.true()
if playable else sqlalchemy.false()))
return q.order_by(getattr(cls, 'name')).all()
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('email', sa.String(length=512), unique=True, nullable=True))
op.add_column('users', sa.Column('email_confirmed', sa.BOOLEAN(), nullable=False, server_default=sa.false()))
op.add_column('users', sa.Column('register_time', sa.TIMESTAMP(), server_default=sa.func.Now()))
op.add_column('users', sa.Column('update_time', server_default=sa.func.Now()))
### end Alembic commands ###
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('profile',
sa.Column('profile_name', sa.String(length=128), nullable=False),
sa.Column('properties', JSONB(),
sa.PrimaryKeyConstraint('profile_name', name=op.f('profile_pkey'))
)
op.create_table('configuration',
sa.Column('key',
sa.Column('value', sa.String(length=1024),
sa.PrimaryKeyConstraint('key', name=op.f('configuration_pkey'))
)
op.create_table('device',
sa.Column('device_id', sa.Integer(), autoincrement=True),
sa.Column('device_type', sa.String(length=64), nullable=True),
sa.Column('hostname', sa.String(length=256),
sa.Column('ip_address',
sa.Column('mac_address',
sa.Column('deleted', server_default=false_just_for_sqlalchemy(),
sa.PrimaryKeyConstraint('device_id', name=op.f('device_pkey')),
sa.ForeignKeyConstraint(['profile_name'], ['profile.profile_name'], name='device_profile',
match='SIMPLE', ondelete='NO ACTION', onupdate='NO ACTION')
)
op.create_table('log',
sa.Column('process',
sa.Column('timestamp', sa.DateTime(timezone=True), server_default=func.Now()),
sa.Column('level',
sa.Column('message', sa.Text(),
sa.ForeignKeyConstraint(['device_id'], ['device.device_id'], name='log_process', onupdate='NO ACTION'),
sa.CheckConstraint('level = ANY (ARRAY[0,10,15,20,30,40,50])', name=op.f('valid_log_levels'))
)
creating_functions()
# ### end Alembic commands ###
def _games(s: sqlalchemy.orm.session.Session,
*,
player: Optional[Player]=None,
account: Optional[Account]=None,
scored: Optional[bool]=None,
limit: Optional[int]=None,
gid: Optional[str]=None,
winning: Optional[bool]=None,
boring: Optional[bool]=None,
reverse_order: Optional[bool]=False) -> sqlalchemy.orm.query.Query:
"""Build a query to match games with certain conditions.
Parameters:
player: If specified,only games with a matching player
account: If specified,only games with a matching account
scored: If specified,only games with a matching scored
limit: If specified,up to limit games
gid: If specified,only game with matching gid
winning: If specified,only games where ktyp==/!='winning'
boring: If specifies,only games where ktyp not boring
reverse_order: Return games least->most recent
Returns:
query object you can call.
"""
q = s.query(Game)
if player is not None:
q = q.filter(Game.player_id == player.id)
if account is not None:
q = q.join(Game.account).filter(Account.id == account.id)
if scored is not None:
q = q.filter(Game.scored == (sqlalchemy.true()
if scored else sqlalchemy.false()))
if gid is not None:
q = q.filter(Game.gid == gid)
if winning is not None:
ktyp = get_ktyp(s, 'winning')
if winning:
q = q.filter(Game.ktyp_id == ktyp.id)
else:
q = q.filter(Game.ktyp_id != ktyp.id)
if boring is not None:
boring_ktyps = [
get_ktyp(s, ktyp).id for ktyp in ('quitting', 'leaving', 'wizmode')
]
if boring:
q = q.filter(Game.ktyp_id.in_(boring_ktyps))
else:
q = q.filter(Game.ktyp_id.notin_(boring_ktyps))
if reverse_order is not None:
q = q.order_by(Game.end.desc()
if not reverse_order else Game.end.asc())
if limit is not None:
q = q.limit(limit)
return q
def get_streaks(s: sqlalchemy.orm.session.Session,
active: Optional[bool]=None,
limit: Optional[int]=None,
max_age: Optional[int]=None,
) \
-> Sequence[Streak]:
"""Get streaks,ordered by length (longest first).
Parameters:
active: only return streaks with this active flag
limit: only return (up to) limit results
max_age: only return streaks with a win less than this many days old
Returns:
List of active streaks.
"""
# The following code is a translation of this basic sql:
# SELECT streaks.*,count(games.streak_id) as streak_length
# FROM streaks
# JOIN games ON (streaks.id = games.streak_id)
# GROUP BY streaks.id
# HAVING streak_length > 1
# ORDER BY streak_length DESC
streak_length = func.count(Game.streak_id).label('streak_length')
streak_last_activity = func.max(Game.end).label('streak_last_activity')
q = s.query(Streak, streak_length).join(Streak.games)
q = q.group_by(Streak.id)
q = q.having(streak_length > 1)
if max_age is not None:
q = q.having(
streak_last_activity > func.date('Now', '-%s day' % max_age))
q = q.order_by(streak_length.desc())
if active is not None:
q = q.filter(Streak.active == (sqlalchemy.true()
if active else sqlalchemy.false()))
if limit is not None:
q = q.limit(limit)
streaks = q.all()
# Since we added a column to the query,the result format is:
# ((Streak,length),(Streak,...)
# It's annoying to deal with a custom format,and recalculating the streak
# length for a few streaks is NBD,so just return a list of Streaks
return [t.Streak for t in streaks]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。