本文整理汇总了Python中mediadrop.model.meta.DBSession.query方法的典型用法代码示例。如果您正苦于以下问题:Python DBSession.query方法的具体用法?Python DBSession.query怎么用?Python DBSession.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mediadrop.model.meta.DBSession
的用法示例。
在下文中一共展示了DBSession.query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_available_slug
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def get_available_slug(mapped_class, string, ignore=None, slug_attr='slug', slug_length=SLUG_LENGTH):
"""Return a unique slug based on the provided string.
Works by appending an int in sequence starting with 2:
1. awesome-stuff
2. awesome-stuff-2
3. awesome-stuff-3
:param mapped_class: The ORM-controlled model that the slug is for
:param string: A title, name, etc
:type string: unicode
:param ignore: A record which doesn't count as a collision
:type ignore: Int ID, ``mapped_class`` instance or None
:returns: A unique slug
:rtype: unicode
"""
if isinstance(ignore, mapped_class):
ignore = ignore.id
elif ignore is not None:
ignore = int(ignore)
new_slug = slug = slugify(string)
appendix = 2
while DBSession.query(mapped_class.id)\
.filter(getattr(mapped_class, slug_attr) == new_slug)\
.filter(mapped_class.id != ignore)\
.first():
str_appendix = u'-%s' % appendix
max_substr_len = slug_length - len(str_appendix)
new_slug = slug[:max_substr_len] + str_appendix
appendix += 1
return new_slug
示例2: comments_save
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def comments_save(self, **kwargs):
"""Save :class:`~mediadrop.forms.admin.settings.CommentsForm`."""
old_vulgarity_filter = c.settings['vulgarity_filtered_words'].value
self._save(comments_form, values=kwargs)
# Run the filter now if it has changed
if old_vulgarity_filter != c.settings['vulgarity_filtered_words'].value:
for comment in DBSession.query(Comment):
comment.body = filter_vulgarity(comment.body)
redirect(action='comments')
示例3: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, page=1, search=None, media_filter=None, **kwargs):
"""List comments with pagination and filtering.
:param page: Page number, defaults to 1.
:type page: int
:param search: Optional search term to filter by
:type search: unicode or None
:param media_filter: Optional media ID to filter by
:type media_filter: int or None
:rtype: dict
:returns:
comments
The list of :class:`~mediadrop.model.comments.Comment` instances
for this page.
edit_form
The :class:`mediadrop.forms.admin.comments.EditCommentForm` instance,
to be rendered for each instance in ``comments``.
search
The given search term, if any
search_form
The :class:`~mediadrop.forms.admin.SearchForm` instance
media_filter
The given podcast ID to filter by, if any
media_filter_title
The media title for rendering if a ``media_filter`` was specified.
"""
comments = Comment.query.trash(False)\
.order_by(Comment.reviewed.asc(),
Comment.created_on.desc())
# This only works since we only have comments on one type of content.
# It will need re-evaluation if we ever add others.
comments = comments.options(orm.eagerload('media'))
if search is not None:
comments = comments.search(search)
media_filter_title = media_filter
if media_filter is not None:
comments = comments.filter(Comment.media.has(Media.id == media_filter))
media_filter_title = DBSession.query(Media.title).get(media_filter)
media_filter = int(media_filter)
return dict(
comments = comments,
edit_form = edit_form,
media_filter = media_filter,
media_filter_title = media_filter_title,
search = search,
search_form = search_form,
)
示例4: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, page=1, **kw):
"""List podcasts with pagination.
:param page: Page number, defaults to 1.
:type page: int
:rtype: Dict
:returns:
podcasts
The list of :class:`~mediadrop.model.podcasts.Podcast`
instances for this page.
"""
podcasts = DBSession.query(Podcast).options(orm.undefer("media_count")).order_by(Podcast.title)
return dict(podcasts=podcasts)
示例5: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, page=1, **kwargs):
"""List groups with pagination.
:param page: Page number, defaults to 1.
:type page: int
:rtype: Dict
:returns:
users
The list of :class:`~mediadrop.model.auth.Group`
instances for this page.
"""
groups = DBSession.query(Group).order_by(Group.display_name, Group.group_name)
return dict(groups=groups)
示例6: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, page=1, **kwargs):
"""List users with pagination.
:param page: Page number, defaults to 1.
:type page: int
:rtype: Dict
:returns:
users
The list of :class:`~mediadrop.model.auth.User`
instances for this page.
"""
users = DBSession.query(User).order_by(User.display_name,
User.email_address)
return dict(users=users)
示例7: fetch_row
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def fetch_row(mapped_class, pk=None, extra_filter=None, **kwargs):
"""Fetch a single row from the database or else trigger a 404.
Typical usage is to fetch a single row for display or editing::
class PageController(object):
@expose()
def index(self, id):
page = fetch_row(Page, id)
return page.name
@expose()
def works_with_slugs_too(self, slug):
page = fetch_row(Page, slug=slug)
return page.name
If the ``pk`` is string ``new`` then an empty instance of ``mapped_class``
is created and returned. This is helpful in admin controllers where you
may reuse your *edit* action for *adding* too.
:param mapped_class: An ORM-controlled model
:param pk: A particular primary key to filter by.
:type pk: int, ``None`` or ``"new"``
:param extra_filter: Extra filter arguments.
:param \*\*kwargs: Any extra args are treated as column names to filter by.
See :meth:`sqlalchemy.orm.Query.filter_by`.
:returns: An instance of ``mapped_class``.
:raises webob.exc.HTTPNotFound: If no result is found
"""
if pk == 'new':
inst = mapped_class()
return inst
query = DBSession.query(mapped_class)
if pk is not None:
mapper = class_mapper(mapped_class, compile=False)
query = query.filter(mapper.primary_key[0] == pk)
if kwargs:
query = query.filter_by(**kwargs)
if extra_filter is not None:
query = query.filter(extra_filter)
try:
return query.one()
except NoResultFound:
raise webob.exc.HTTPNotFound().exception
示例8: save
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def save(self, id, email_address, display_name, login_details,
delete=None, **kwargs):
"""Save changes or create a new :class:`~mediadrop.model.auth.User` instance.
:param id: User ID. If ``"new"`` a new user is created.
:type id: ``int`` or ``"new"``
:returns: Redirect back to :meth:`index` after successful save.
"""
user = fetch_row(User, id)
if delete:
DBSession.delete(user)
redirect(action='index', id=None)
user.display_name = display_name
user.email_address = email_address
user.user_name = login_details['user_name']
password = login_details['password']
if password is not None and password != '':
user.password = password
if login_details['groups']:
query = DBSession.query(Group).filter(Group.group_id.in_(login_details['groups']))
user.groups = list(query.all())
else:
user.groups = []
DBSession.add(user)
# Check if we're changing the logged in user's own password
if user.id == request.perm.user.id \
and password is not None and password != '':
DBSession.commit()
# repoze.who sees the Unauthorized response and clears the cookie,
# forcing a fresh login with the new password
raise webob.exc.HTTPUnauthorized().exception
redirect(action='index', id=None)
示例9: insert_settings
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def insert_settings(defaults):
"""Insert the given setting if they don't exist yet.
XXX: Does not include any support for MultiSetting. This approach
won't work for that. We'll need to use a migration script.
:type defaults: list
:param defaults: Key and value pairs
:rtype: list
:returns: Any settings that have just been created.
"""
inserted = []
try:
settings_query = DBSession.query(Setting.key)\
.filter(Setting.key.in_([key for key, value in defaults]))
existing_settings = set(x[0] for x in settings_query)
except ProgrammingError:
# If we are running paster setup-app on a fresh database with a
# plugin which tries to use this function every time the
# Environment.loaded event fires, the settings table will not
# exist and this exception will be thrown, but its safe to ignore.
# The settings will be created the next time the event fires,
# which will likely be the first time the app server starts up.
return inserted
for key, value in defaults:
if key in existing_settings:
continue
transaction = DBSession.begin_nested()
try:
s = Setting(key, value)
DBSession.add(s)
transaction.commit()
inserted.append(s)
except IntegrityError:
transaction.rollback()
if inserted:
DBSession.commit()
return inserted
示例10: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, page=1, **kwargs):
"""List tags with pagination.
:param page: Page number, defaults to 1.
:type page: int
:rtype: Dict
:returns:
tags
The list of :class:`~mediadrop.model.tags.Tag`
instances for this page.
tag_form
The :class:`~mediadrop.forms.admin.settings.tags.TagForm` instance.
"""
tags = DBSession.query(Tag)\
.options(orm.undefer('media_count'))\
.order_by(Tag.name)
return dict(
tags = tags,
tag_form = tag_form,
tag_row_form = tag_row_form,
)
示例11: save
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def save(self, id, display_name, group_name, permissions, delete=None, **kwargs):
"""Save changes or create a new :class:`~mediadrop.model.auth.Group` instance.
:param id: Group ID. If ``"new"`` a new group is created.
:type id: ``int`` or ``"new"``
:returns: Redirect back to :meth:`index` after successful save.
"""
group = fetch_row(Group, id)
if delete:
DBSession.delete(group)
redirect(action='index', id=None)
group.display_name = display_name
group.group_name = group_name
if permissions:
query = DBSession.query(Permission).filter(Permission.permission_id.in_(permissions))
group.permissions = list(query.all())
else:
group.permissions = []
DBSession.add(group)
redirect(action='index', id=None)
示例12: index
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def index(self, type=None, podcast=None, tag=None, category=None, search=None,
max_age=None, min_age=None, order=None, offset=0, limit=10,
published_after=None, published_before=None, featured=False,
id=None, slug=None, include_embed=False, format="json", **kwargs):
"""Query for a list of media.
:param type:
Filter by '%s' or '%s'. Defaults to any type.
:param podcast:
A podcast slug (or slugs) to filter by. Use 0 to include
only non-podcast media or 1 to include any podcast media.
For multiple podcasts, separate the slugs with commas.
:param tag:
A tag slug to filter by.
:param category:
A category slug to filter by.
:param search:
A boolean search query. See
http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html
:param published_after:
If given, only media published *on or after* this date is
returned. The expected format is 'YYYY-MM-DD HH:MM:SS'
(ISO 8601) and must include the year at a bare minimum.
:param published_before:
If given, only media published *on or before* this date is
returned. The expected format is 'YYYY-MM-DD HH:MM:SS'
(ISO 8601) and must include the year at a bare minimum.
:param max_age:
If given, only media published within this many days is
returned. This is a convenience shortcut for publish_after
and will override its value if both are given.
:type max_age: int
:param min_age:
If given, only media published prior to this number of days
ago will be returned. This is a convenience shortcut for
publish_before and will override its value if both are given.
:type min_age: int
:param order:
A column name and 'asc' or 'desc', seperated by a space.
The column name can be any one of the returned columns.
Defaults to newest media first (publish_on desc).
:param offset:
Where in the complete resultset to start returning results.
Defaults to 0, the very beginning. This is useful if you've
already fetched the first 50 results and want to fetch the
next 50 and so on.
:type offset: int
:param limit:
Number of results to return in each query. Defaults to 10.
The maximum allowed value defaults to 50 and is set via
:attr:`request.settings['api_media_max_results']`.
:type limit: int
:param featured:
If nonzero, the results will only include media from the
configured featured category, if there is one.
:type featured: bool
:param include_embed:
If nonzero, the HTML for the embeddable player is included
for all results.
:type include_embed: bool
:param id:
Filters the results to include the one item with the given ID.
Note that we still return a list.
:type id: int or None
:param slug:
Filters the results to include the one item with the given slug.
Note that we still return a list.
:type slug: unicode or None
:param api_key:
The api access key if required in settings
:type api_key: unicode or None
:raises APIException:
If there is an user error in the query params.
:rtype: JSON-ready dict
:returns: The returned dict has the following fields:
count (int)
The total number of results that match this query.
media (list of dicts)
A list of **media_info** dicts, as generated by the
:meth:`_info <mediadrop.controllers.api.media.MediaController._info>`
method. The number of dicts in this list will be the lesser
#.........这里部分代码省略.........
示例13: _info
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def _info(self, media, podcast_slugs=None, include_embed=False):
"""
Return a **media_info** dict--a JSON-ready dict for describing a media instance.
:rtype: JSON-ready dict
:returns: The returned dict has the following fields:
author (unicode)
The name of the
:attr:`author <mediadrop.model.media.Media.author>` of the
media instance.
categories (dict of unicode)
A JSON-ready dict representing the categories the media
instance is in. Keys are the unique
:attr:`slugs <mediadrop.model.podcasts.Podcast.slug>`
for each category, values are the human-readable
:attr:`title <mediadrop.model.podcasts.podcast.Title>`
of that category.
id (int)
The numeric unique :attr:`id <mediadrop.model.media.Media.id>` of
the media instance.
slug (unicode)
The more human readable unique identifier
(:attr:`slug <mediadrop.model.media.Media.slug>`)
of the media instance.
url (unicode)
A permalink (HTTP) to the MediaDrop view page for the media instance.
embed (unicode)
HTML code that can be used to embed the video in another site.
title (unicode)
The :attr:`title <mediadrop.model.media.Media.title>` of
the media instance.
type (string, one of ['%s', '%s'])
The :attr:`type <mediadrop.model.media.Media.type>` of
the media instance
podcast (unicode or None)
The :attr:`slug <mediadrop.model.podcasts.Podcast.slug>` of the
:class:`podcast <mediadrop.model.podcasts.Podcast>` that
the media instance has been published under, or None
description (unicode)
An XHTML
:attr:`description <mediadrop.model.media.Media.description>`
of the media instance.
description_plain (unicode)
A plain text
:attr:`description <mediadrop.model.media.Media.description_plain>`
of the media instance.
comment_count (int)
The number of published comments on the media instance.
publish_on (unicode)
The date of publishing in "YYYY-MM-DD HH:MM:SS" (ISO 8601) format.
e.g. "2010-02-16 15:06:49"
likes (int)
The number of :attr:`like votes <mediadrop.model.media.Media.likes>`
that the media instance has received.
views (int)
The number of :attr:`views <mediadrop.model.media.Media.views>`
that the media instance has received.
thumbs (dict)
A dict of dicts containing URLs, width and height of
different sizes of thumbnails. The default sizes
are 's', 'm' and 'l'. Using medium for example::
medium_url = thumbs['m']['url']
medium_width = thumbs['m']['x']
medium_height = thumbs['m']['y']
"""
if media.podcast_id:
media_url = url_for(controller='/media', action='view', slug=media.slug,
podcast_slug=media.podcast.slug, qualified=True)
else:
media_url = url_for_media(media, qualified=True)
if media.podcast_id is None:
podcast_slug = None
elif podcast_slugs:
podcast_slug = podcast_slugs[media.podcast_id]
else:
podcast_slug = DBSession.query(Podcast.slug)\
.filter_by(id=media.podcast_id).scalar()
thumbs = {}
for size in config['thumb_sizes'][media._thumb_dir].iterkeys():
thumbs[size] = thumb(media, size, qualified=True)
info = dict(
id = media.id,
slug = media.slug,
url = media_url,
title = media.title,
author = media.author.name,
type = media.type,
podcast = podcast_slug,
description = media.description,
description_plain = media.description_plain,
comment_count = media.comment_count_published,
publish_on = unicode(media.publish_on),
likes = media.likes,
views = media.views,
thumbs = thumbs,
#.........这里部分代码省略.........
示例14: by_user_name
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def by_user_name(cls, username):
# TODO: Move this function to User.query
return DBSession.query(cls).filter(cls.user_name==username).first()
示例15: by_email_address
# 需要导入模块: from mediadrop.model.meta import DBSession [as 别名]
# 或者: from mediadrop.model.meta.DBSession import query [as 别名]
def by_email_address(cls, email):
# TODO: Move this function to User.query
return DBSession.query(cls).filter(cls.email_address==email).first()