本文整理汇总了Python中mediadrop.model.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mediadrop.model.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def index(self, page=1, **kwargs):
"""List storage engines with pagination.
:rtype: Dict
:returns:
engines
The list of :class:`~mediadrop.lib.storage.StorageEngine`
instances for this page.
"""
engines = DBSession.query(StorageEngine)\
.options(orm.undefer('file_count'),
orm.undefer('file_size_sum'))\
.all()
engines = list(sort_engines(engines))
existing_types = set(ecls.engine_type for ecls in engines)
addable_engines = [
ecls
for ecls in StorageEngine
if not ecls.is_singleton or ecls.engine_type not in existing_types
]
return {
'engines': engines,
'addable_engines': addable_engines,
}
示例2: test_can_fake_logged_in_user
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def test_can_fake_logged_in_user(self):
admin = DBSession.query(User).filter(User.user_name == u"admin").one()
assert_true(admin.has_permission(u"admin"))
self.init_fake_request()
self.set_authenticated_user(admin)
assert_true(has_permission(u"admin"))
示例3: create_new_user
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def create_new_user(username, config):
from mediadrop.model import DBSession, User, fetch_row, Group
user = User.by_user_name(username)
if user is None:
try:
print "MIDDLEWARE"
print config
l = ldap.initialize(config['ldap_url'])
l.simple_bind_s(config['ldap_binddn'], config['ldap_pw'])
filter = '(samaccountname=%s)' % username
r = l.search_s(config['ldap_base'],ldap.SCOPE_SUBTREE, filter, ['displayname', 'mail'])
l.unbind_s()
user_attrs = {}
for dn, entry in r:
for attr, v in entry.iteritems():
user_attrs[attr] = v[0]
except ldap.LDAPError:
l.unbind_s()
new_user = fetch_row(User, "new")
new_user.display_name = user_attrs['displayName']
new_user.email_address = user_attrs['mail']
new_user.user_name = username
query = DBSession.query(Group).filter(Group.group_name.in_(['authenticated']))
new_user.groups = list(query.all())
DBSession.add(new_user)
DBSession.commit()
示例4: permissions_for_request
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def permissions_for_request(cls, environ, config):
identity = environ.get('repoze.who.identity', {})
user_id = identity.get('repoze.who.userid')
user = None
if user_id is not None:
user = DBSession.query(User).filter(User.id==user_id).first()
return cls.permissions_for_user(user, config)
示例5: test_can_restrict_query_if_user_does_not_have_the_required_permission
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def test_can_restrict_query_if_user_does_not_have_the_required_permission(self):
query = Media.query
permission = u'view'
perm = self.perm()
view_permission = DBSession.query(Permission).filter(Permission.permission_name == permission).one()
view_permission.groups = []
DBSession.flush()
assert_none(self.policy.access_condition_for_query(query, permission, perm))
示例6: _create_user_with_admin_permission_only
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def _create_user_with_admin_permission_only(self):
admin_perm = DBSession.query(Permission).filter(Permission.permission_name == u'admin').one()
second_admin_group = Group.example(name=u'Second admin group')
admin_perm.groups.append(second_admin_group)
admin = User.example(groups=[second_admin_group])
DBSession.commit()
perm = MediaDropPermissionSystem.permissions_for_user(admin, config)
assert_true(perm.contains_permission(u'admin'))
assert_false(perm.contains_permission(u'edit'))
return admin
示例7: _to_python
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def _to_python(self, value, state):
id = request.environ['pylons.routes_dict']['id']
query = DBSession.query(Group).filter_by(group_name=value)
if id != 'new':
query = query.filter(Group.group_id != id)
if query.count() != 0:
raise Invalid(_('Group name already exists'), value, state)
return value
示例8: update_enabled_players
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def update_enabled_players():
"""Ensure that the encoding status of all media is up to date with the new
set of enabled players.
The encoding status of Media objects is dependent on there being an
enabled player that supports that format. Call this method after changing
the set of enabled players, to ensure encoding statuses are up to date.
"""
from mediadrop.model import DBSession, Media
media = DBSession.query(Media)
for m in media:
m.update_status()
示例9: random
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def random(self, **kwargs):
"""Redirect to a randomly selected media item."""
# TODO: Implement something more efficient than ORDER BY RAND().
# This method does a full table scan every time.
random_query = Media.query.published().order_by(sql.func.random())
media = viewable_media(random_query).first()
if media is None:
redirect(action='explore')
if media.podcast_id:
podcast_slug = DBSession.query(Podcast.slug).get(media.podcast_id)
else:
podcast_slug = None
redirect(action='view', slug=media.slug, podcast_slug=podcast_slug)
示例10: setup_app
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def setup_app(command, conf, vars):
"""Called by ``paster setup-app``.
This script is responsible for:
* Creating the initial database schema and loading default data.
* Executing any migrations necessary to bring an existing database
up-to-date. Your data should be safe but, as always, be sure to
make backups before using this.
* Re-creating the default database for every run of the test suite.
XXX: All your data will be lost IF you run the test suite with a
config file named 'test.ini'. Make sure you have this configured
to a different database than in your usual deployment.ini or
development.ini file because all database tables are dropped a
and recreated every time this script runs.
XXX: If you are upgrading from MediaCore v0.7.2 or v0.8.0, run whichever
one of these that applies:
``python batch-scripts/upgrade/upgrade_from_v072.py deployment.ini``
``python batch-scripts/upgrade/upgrade_from_v080.py deployment.ini``
XXX: For search to work, we depend on a number of MySQL triggers which
copy the data from our InnoDB tables to a MyISAM table for its
fulltext indexing capability. Triggers can only be installed with
a mysql superuser like root, so you must run the setup_triggers.sql
script yourself.
"""
# paster just scans the source code for a "websetup.py". Due to our
# compatibility module for the old "mediacore" namespace it actually finds
# two modules (mediadrop.websetup and mediacore.websetup) even though both
# actually point to the same source code.
# Because of that "paster setup-app" actually runs "setup_app()" twice
# which causes some bad stuff to happen (e.g. duplicate metadata
# initialization. Until we get rid of the compat package we should make
# sure the following code is only run once.
global did_run_setup
if did_run_setup:
return
config = load_environment(conf.global_conf, conf.local_conf)
plugin_manager = config['pylons.app_globals'].plugin_mgr
mediadrop_migrator = MediaDropMigrator.from_config(conf, log=log)
engine = metadata.bind
db_connection = engine.connect()
# simplistic check to see if MediaDrop tables are present, just check for
# the media_files table and assume that all other tables are there as well
from mediadrop.model.media import media_files
mediadrop_tables_exist = engine.dialect.has_table(db_connection, media_files.name)
run_migrations = True
if not mediadrop_tables_exist:
head_revision = mediadrop_migrator.head_revision()
log.info('Initializing new database with version %r' % head_revision)
metadata.create_all(bind=DBSession.bind, checkfirst=True)
mediadrop_migrator.init_db(revision=head_revision)
run_migrations = False
add_default_data()
for migrator in plugin_manager.migrators():
migrator.init_db()
events.Environment.database_initialized()
elif not mediadrop_migrator.alembic_table_exists():
if not mediadrop_migrator.migrate_table_exists():
log.error('No migration table found, probably your MediaDrop install '
'is too old (< 0.9?). Please upgrade to MediaCore CE 0.9 first.')
raise AssertionError('no migration table found')
alembic_revision = mediadrop_migrator.map_migrate_version()
mediadrop_migrator.stamp(alembic_revision)
if run_migrations:
mediadrop_migrator.migrate_db()
for migrator in plugin_manager.migrators():
migrator.migrate_db()
events.Environment.database_migrated()
cleanup_players_table(enabled=True)
# Save everything, along with the dummy data if applicable
DBSession.commit()
events.Environment.database_ready()
log.info('Generating appearance.css from your current settings')
settings = DBSession.query(Setting.key, Setting.value)
if settings.count() == 0:
error_msg = (
u"Unable to find any settings in the database. This may happen if a previous\n"
u"setup did not complete successfully.\n"
u"Please inspect your database contents. If you don't have any valuable data in\n"
u"that db you can try to remove all tables and run the setup process again.\n"
u"BE CAREFUL: REMOVING ALL TABLES MIGHT CAUSE DATA LOSS!\n"
)
sys.stderr.write(error_msg)
log.error(error_msg.replace('\n', u' '))
sys.exit(99)
generate_appearance_css(settings, cache_dir=conf['cache_dir'])
did_run_setup = True
log.info('Successfully setup')
示例11: permissions
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def permissions(self):
db_permissions = DBSession.query(Permission).all()
return tuple([permission.permission_name for permission in db_permissions])
示例12: editor_group
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def editor_group(self):
return DBSession.query(Group).filter(Group.group_name == u'editors').one()
示例13: setup_app
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def setup_app(command, conf, vars):
"""Called by ``paster setup-app``.
This script is responsible for:
* Creating the initial database schema and loading default data.
* Executing any migrations necessary to bring an existing database
up-to-date. Your data should be safe but, as always, be sure to
make backups before using this.
* Re-creating the default database for every run of the test suite.
XXX: All your data will be lost IF you run the test suite with a
config file named 'test.ini'. Make sure you have this configured
to a different database than in your usual deployment.ini or
development.ini file because all database tables are dropped a
and recreated every time this script runs.
XXX: If you are upgrading from MediaCore v0.7.2 or v0.8.0, run whichever
one of these that applies:
``python batch-scripts/upgrade/upgrade_from_v072.py deployment.ini``
``python batch-scripts/upgrade/upgrade_from_v080.py deployment.ini``
XXX: For search to work, we depend on a number of MySQL triggers which
copy the data from our InnoDB tables to a MyISAM table for its
fulltext indexing capability. Triggers can only be installed with
a mysql superuser like root, so you must run the setup_triggers.sql
script yourself.
"""
config = load_environment(conf.global_conf, conf.local_conf)
plugin_manager = config['pylons.app_globals'].plugin_mgr
mediadrop_migrator = MediaDropMigrator.from_config(conf, log=log)
engine = metadata.bind
db_connection = engine.connect()
# simplistic check to see if MediaDrop tables are present, just check for
# the media_files table and assume that all other tables are there as well
from mediadrop.model.media import media_files
mediadrop_tables_exist = engine.dialect.has_table(db_connection, media_files.name)
run_migrations = True
if not mediadrop_tables_exist:
head_revision = mediadrop_migrator.head_revision()
log.info('Initializing new database with version %r' % head_revision)
metadata.create_all(bind=DBSession.bind, checkfirst=True)
mediadrop_migrator.init_db(revision=head_revision)
run_migrations = False
add_default_data()
for migrator in plugin_manager.migrators():
migrator.init_db()
events.Environment.database_initialized()
elif not mediadrop_migrator.migrate_table_exists():
log.error('No migration table found, probably your MediaDrop install '
'is too old (< 0.9?). Please upgrade to MediaCore CE 0.9 first.')
raise AssertionError('no migration table found')
elif not mediadrop_migrator.alembic_table_exists():
alembic_revision = mediadrop_migrator.map_migrate_version()
mediadrop_migrator.stamp(alembic_revision)
if run_migrations:
mediadrop_migrator.migrate_db()
for migrator in plugin_manager.migrators():
migrator.migrate_db()
events.Environment.database_migrated()
cleanup_players_table(enabled=True)
# Save everything, along with the dummy data if applicable
DBSession.commit()
events.Environment.database_ready()
log.info('Generating appearance.css from your current settings')
settings = DBSession.query(Setting.key, Setting.value)
generate_appearance_css(settings, cache_dir=conf['cache_dir'])
log.info('Successfully setup')
示例14: __before__
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def __before__(self, *args, **kwargs):
"""Load all our settings before each request."""
BaseController.__before__(self, *args, **kwargs)
from mediadrop.model import Setting
tmpl_context.settings = dict(DBSession.query(Setting.key, Setting))
示例15: enabled_engines
# 需要导入模块: from mediadrop.model import DBSession [as 别名]
# 或者: from mediadrop.model.DBSession import query [as 别名]
def enabled_engines():
from mediadrop.model import DBSession
engines = DBSession.query(StorageEngine)\
.filter(StorageEngine.enabled == True)\
.all()
return list(sort_engines(engines))